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)
| 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. |
| 42 | func 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 |
| 80 | func capitalizeFirst(s string) string { |
searching dependent graphs…