Debian Codegraph @

lib/ui/string.go

raw
1 package ui
2
3 import (
4 "go.rockorager.dev/vaxis"
5 )
6
7 func StyledString(s string) *vaxis.StyledString {
8 if state.vx == nil {
9 return nil
10 }
11 return state.vx.NewStyledString(s, vaxis.Style{})
12 }
13
14 // Applies a style to a string. Any currently applied styles will not be overwritten
15 func ApplyStyle(style vaxis.Style, str string) string {
16 ss := StyledString(str)
17 if ss == nil {
18 return str
19 }
20 d := vaxis.Style{}
21 for i, sr := range ss.Cells {
22 if sr.Style == d {
23 sr.Style = style
24 ss.Cells[i] = sr
25 }
26 }
27 return ss.Encode()
28 }
29
30 // PadLeft inserts blank spaces at the beginning of the StyledString to produce
31 // a string of the provided width
32 func PadLeft(ss *vaxis.StyledString, width int) {
33 w := ss.Len()
34 if w >= width {
35 return
36 }
37 cell := vaxis.Cell{
38 Character: vaxis.Character{
39 Grapheme: " ",
40 Width: 1,
41 },
42 }
43 w = width - w
44 cells := make([]vaxis.Cell, 0, len(ss.Cells)+w)
45 for w > 0 {
46 cells = append(cells, cell)
47 w -= 1
48 }
49 cells = append(cells, ss.Cells...)
50 ss.Cells = cells
51 }
52
53 // PadLeft inserts blank spaces at the end of the StyledString to produce
54 // a string of the provided width
55 func PadRight(ss *vaxis.StyledString, width int) {
56 w := ss.Len()
57 if w >= width {
58 return
59 }
60 cell := vaxis.Cell{
61 Character: vaxis.Character{
62 Grapheme: " ",
63 Width: 1,
64 },
65 }
66 w = width - w
67 for w > 0 {
68 w -= 1
69 ss.Cells = append(ss.Cells, cell)
70 }
71 }
72
73 // ApplyAttrs applies the style, and if another style is present ORs the
74 // attributes
75 func ApplyAttrs(ss *vaxis.StyledString, style vaxis.Style) {
76 for i, cell := range ss.Cells {
77 if style.Foreground != 0 {
78 cell.Style.Foreground = style.Foreground
79 }
80 if style.Background != 0 {
81 cell.Style.Background = style.Background
82 }
83 cell.Style.Attribute |= style.Attribute
84 if style.UnderlineColor != 0 {
85 cell.Style.UnderlineColor = style.UnderlineColor
86 }
87 if style.UnderlineStyle != vaxis.UnderlineOff {
88 cell.Style.UnderlineStyle = style.UnderlineStyle
89 }
90 ss.Cells[i] = cell
91 }
92 }
93
94 // Truncates the styled string on the right and inserts a '…' as the last
95 // character
96 func Truncate(ss *vaxis.StyledString, width int) {
97 if ss.Len() <= width {
98 return
99 }
100 cells := make([]vaxis.Cell, 0, len(ss.Cells))
101 total := 0
102 for _, cell := range ss.Cells {
103 if total+cell.Width >= width {
104 // we can't fit this cell so put in our truncator
105 cells = append(cells, vaxis.Cell{
106 Character: vaxis.Character{
107 Grapheme: "…",
108 Width: 1,
109 },
110 Style: cell.Style,
111 })
112 break
113 }
114 total += cell.Width
115 cells = append(cells, cell)
116 }
117 ss.Cells = cells
118 }
119
120 // TruncateHead truncates the left side of the string and inserts '…' as the
121 // first character
122 func TruncateHead(ss *vaxis.StyledString, width int) {
123 l := ss.Len()
124 if l <= width {
125 return
126 }
127 offset := l - width
128 cells := make([]vaxis.Cell, 0, len(ss.Cells))
129 cells = append(cells, vaxis.Cell{
130 Character: vaxis.Character{
131 Grapheme: "…",
132 Width: 1,
133 },
134 })
135 total := 0
136 for _, cell := range ss.Cells {
137 total += cell.Width
138 if total < offset {
139 // we always have at least one for our truncator. We
140 // copy this cells style to it so that it retains the
141 // style information from the first printed cell
142 cells[0].Style = cell.Style
143 continue
144 }
145 cells = append(cells, cell)
146 }
147 ss.Cells = cells
148 }