| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | const defaultViewWidth = 80 |
| 9 | |
| 10 | func viewWidth(width int) int { |
| 11 | if width <= 0 { |
| 12 | return defaultViewWidth |
| 13 | } |
| 14 | return width |
| 15 | } |
| 16 | |
| 17 | func viewHeader(format string, args ...any) string { |
| 18 | return accent(fmt.Sprintf(format, args...)) |
| 19 | } |
| 20 | |
| 21 | func viewSubhead(s string) string { |
| 22 | return dim(" " + s) |
| 23 | } |
| 24 | |
| 25 | func viewMeta(s string) string { |
| 26 | return dim(s) |
| 27 | } |
| 28 | |
| 29 | func viewStatus(s string) string { |
| 30 | return accent(s) |
| 31 | } |
| 32 | |
| 33 | func viewHint(s string) string { |
| 34 | return dim(" " + s) |
| 35 | } |
| 36 | |
| 37 | func viewMore(n int, noun string) string { |
| 38 | if n <= 0 { |
| 39 | return "" |
| 40 | } |
| 41 | return dim(fmt.Sprintf(" +%d more %s", n, noun)) |
| 42 | } |
| 43 | |
| 44 | func viewCompactPath(path string, width int) string { |
| 45 | path = oneLineText(path) |
| 46 | return compactMiddle(path, max(1, width)) |
| 47 | } |
| 48 | |
| 49 | func viewCompactText(s string, width int) string { |
| 50 | s = oneLineText(s) |
| 51 | return compactEnd(s, max(1, width)) |
| 52 | } |
| 53 | |
| 54 | func viewBodyPreview(body string, maxLines int) (string, int) { |
| 55 | body = strings.TrimRight(body, "\n") |
| 56 | if body == "" { |
| 57 | return "", 0 |
| 58 | } |
| 59 | lines := strings.Split(body, "\n") |
| 60 | if maxLines <= 0 || len(lines) <= maxLines { |
| 61 | return body, 0 |
| 62 | } |
| 63 | return strings.Join(lines[:maxLines], "\n"), len(lines) - maxLines |
| 64 | } |
| 65 | |
| 66 | func viewProtectLines(s string, width int) string { |
| 67 | width = viewWidth(width) |
| 68 | lines := strings.Split(s, "\n") |
| 69 | for i, line := range lines { |
| 70 | lines[i] = compactEnd(line, width) |
| 71 | } |
| 72 | return strings.Join(lines, "\n") |
| 73 | } |
| 74 | |
| 75 | func viewPadWidth(s string, minWidth int) int { |
| 76 | if w := visibleWidth(s); w > minWidth { |
| 77 | return w |
| 78 | } |
| 79 | return minWidth |
| 80 | } |
| 81 | |
| 82 | func viewBudget(width, used int) int { |
| 83 | return max(1, viewWidth(width)-used) |
| 84 | } |
| 85 |