MCPcopy Index your code
hub / github.com/docker/cli / Ellipsis

Function Ellipsis

cli/command/formatter/displayutils.go:42–77  ·  view source on GitHub ↗

Ellipsis truncates a string to fit within maxDisplayWidth, and appends ellipsis (…). For maxDisplayWidth of 1 and lower, no ellipsis is appended. For maxDisplayWidth of 1, first char of string will return even if its width > 1.

(s string, maxDisplayWidth int)

Source from the content-addressed store, hash-verified

40// For maxDisplayWidth of 1 and lower, no ellipsis is appended.
41// For maxDisplayWidth of 1, first char of string will return even if its width > 1.
42func Ellipsis(s string, maxDisplayWidth int) string {
43 if maxDisplayWidth <= 0 {
44 return ""
45 }
46 rs := []rune(s)
47 if maxDisplayWidth == 1 {
48 return string(rs[0])
49 }
50
51 byteLen := len(s)
52 if byteLen == utf8.RuneCountInString(s) {
53 if byteLen <= maxDisplayWidth {
54 return s
55 }
56 return string(rs[:maxDisplayWidth-1]) + "…"
57 }
58
59 var (
60 display = make([]int, 0, len(rs))
61 displayWidth int
62 )
63 for _, r := range rs {
64 cw := charWidth(r)
65 displayWidth += cw
66 display = append(display, displayWidth)
67 }
68 if displayWidth <= maxDisplayWidth {
69 return s
70 }
71 for i := range display {
72 if display[i] <= maxDisplayWidth-1 && display[i+1] > maxDisplayWidth-1 {
73 return string(rs[:i+1]) + "…"
74 }
75 }
76 return s
77}
78
79// capitalizeFirst capitalizes the first character of string
80func capitalizeFirst(s string) string {

Callers 4

TestEllipsisFunction · 0.70
CommandMethod · 0.70
MountsMethod · 0.70
ErrorMethod · 0.70

Calls 1

charWidthFunction · 0.85

Tested by 1

TestEllipsisFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…