Debian Codegraph @

ui/messages/tstring/string.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package tstring
18
19 import (
20 "strings"
21 "unicode"
22
23 "github.com/mattn/go-runewidth"
24
25 "go.mau.fi/mauview"
•26 "github.com/gdamore/tcell/v2"
27 )
28
29 type TString []Cell
30
31 func NewBlankTString() TString {
32 return make(TString, 0)
33 }
34
35 func NewTString(str string) TString {
36 newStr := make(TString, len(str))
37 for i, char := range str {
38 newStr[i] = NewCell(char)
39 }
40 return newStr
41 }
42
43 func NewColorTString(str string, color tcell.Color) TString {
44 newStr := make(TString, len(str))
45 for i, char := range str {
46 newStr[i] = NewColorCell(char, color)
47 }
48 return newStr
49 }
50
51 func NewStyleTString(str string, style tcell.Style) TString {
52 newStr := make(TString, len(str))
53 for i, char := range str {
54 newStr[i] = NewStyleCell(char, style)
55 }
56 return newStr
57 }
58
59 func Join(strings []TString, separator string) TString {
60 if len(strings) == 0 {
61 return NewBlankTString()
62 }
63
64 out := strings[0]
65 strings = strings[1:]
66
67 if len(separator) == 0 {
68 return out.AppendTString(strings...)
69 }
70
71 for _, str := range strings {
72 out = append(out, str.Prepend(separator)...)
73 }
74 return out
75 }
76
77 func (str TString) Clone() TString {
78 newStr := make(TString, len(str))
79 copy(newStr, str)
80 return newStr
81 }
82
83 func (str TString) AppendTString(dataList ...TString) TString {
84 newStr := str
85 for _, data := range dataList {
86 newStr = append(newStr, data...)
87 }
88 return newStr
89 }
90
91 func (str TString) PrependTString(data TString) TString {
92 return append(data, str...)
93 }
94
95 func (str TString) Append(data string) TString {
96 return str.AppendCustom(data, func(r rune) Cell {
97 return NewCell(r)
98 })
99 }
100
101 func (str TString) TrimSpace() TString {
102 return str.Trim(unicode.IsSpace)
103 }
104
105 func (str TString) Trim(fn func(rune) bool) TString {
106 return str.TrimLeft(fn).TrimRight(fn)
107 }
108
109 func (str TString) TrimLeft(fn func(rune) bool) TString {
110 for index, cell := range str {
111 if !fn(cell.Char) {
112 return append(NewBlankTString(), str[index:]...)
113 }
114 }
115 return NewBlankTString()
116 }
117
118 func (str TString) TrimRight(fn func(rune) bool) TString {
119 for i := len(str) - 1; i >= 0; i-- {
120 if !fn(str[i].Char) {
121 return append(NewBlankTString(), str[:i+1]...)
122 }
123 }
124 return NewBlankTString()
125 }
126
127 func (str TString) AppendColor(data string, color tcell.Color) TString {
128 return str.AppendCustom(data, func(r rune) Cell {
129 return NewColorCell(r, color)
130 })
131 }
132
133 func (str TString) AppendStyle(data string, style tcell.Style) TString {
134 return str.AppendCustom(data, func(r rune) Cell {
135 return NewStyleCell(r, style)
136 })
137 }
138
139 func (str TString) AppendCustom(data string, cellCreator func(rune) Cell) TString {
140 newStr := make(TString, len(str)+len(data))
141 copy(newStr, str)
142 for i, char := range data {
143 newStr[i+len(str)] = cellCreator(char)
144 }
145 return newStr
146 }
147
148 func (str TString) Prepend(data string) TString {
149 return str.PrependCustom(data, func(r rune) Cell {
150 return NewCell(r)
151 })
152 }
153
154 func (str TString) PrependColor(data string, color tcell.Color) TString {
155 return str.PrependCustom(data, func(r rune) Cell {
156 return NewColorCell(r, color)
157 })
158 }
159
160 func (str TString) PrependStyle(data string, style tcell.Style) TString {
161 return str.PrependCustom(data, func(r rune) Cell {
162 return NewStyleCell(r, style)
163 })
164 }
165
166 func (str TString) PrependCustom(data string, cellCreator func(rune) Cell) TString {
167 newStr := make(TString, len(str)+len(data))
168 copy(newStr[len(data):], str)
169 for i, char := range data {
170 newStr[i] = cellCreator(char)
171 }
172 return newStr
173 }
174
175 func (str TString) Colorize(from, length int, color tcell.Color) {
176 str.AdjustStyle(from, length, func(style tcell.Style) tcell.Style {
177 return style.Foreground(color)
178 })
179 }
180
181 func (str TString) AdjustStyle(from, length int, fn func(tcell.Style) tcell.Style) {
182 for i := from; i < from+length; i++ {
183 str[i].Style = fn(str[i].Style)
184 }
185 }
186
187 func (str TString) AdjustStyleFull(fn func(tcell.Style) tcell.Style) {
188 str.AdjustStyle(0, len(str), fn)
189 }
190
191 func (str TString) Draw(screen mauview.Screen, x, y int) {
192 for _, cell := range str {
193 x += cell.Draw(screen, x, y)
194 }
195 }
196
197 func (str TString) RuneWidth() (width int) {
198 for _, cell := range str {
199 width += runewidth.RuneWidth(cell.Char)
200 }
201 return width
202 }
203
204 func (str TString) String() string {
205 var buf strings.Builder
206 for _, cell := range str {
207 buf.WriteRune(cell.Char)
208 }
209 return buf.String()
210 }
211
212 // Truncate return string truncated with w cells
213 func (str TString) Truncate(w int) TString {
214 if str.RuneWidth() <= w {
215 return str[:]
216 }
217 width := 0
218 i := 0
219 for ; i < len(str); i++ {
220 cw := runewidth.RuneWidth(str[i].Char)
221 if width+cw > w {
222 break
223 }
224 width += cw
225 }
226 return str[0:i]
227 }
228
229 func (str TString) IndexFrom(r rune, from int) int {
230 for i := from; i < len(str); i++ {
231 if str[i].Char == r {
232 return i
233 }
234 }
235 return -1
236 }
237
238 func (str TString) Index(r rune) int {
239 return str.IndexFrom(r, 0)
240 }
241
242 func (str TString) Count(r rune) (counter int) {
243 index := 0
244 for {
245 index = str.IndexFrom(r, index)
246 if index < 0 {
247 break
248 }
249 index++
250 counter++
251 }
252 return
253 }
254
255 func (str TString) Split(sep rune) []TString {
256 a := make([]TString, str.Count(sep)+1)
257 i := 0
258 orig := str
259 for {
260 m := orig.Index(sep)
261 if m < 0 {
262 break
263 }
264 a[i] = orig[:m]
265 orig = orig[m+1:]
266 i++
267 }
268 a[i] = orig
269 return a[:i+1]
270 }