-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_line_wrap.go
230 lines (224 loc) · 6.05 KB
/
word_line_wrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2021 The CDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package memphis
import (
"github.com/go-curses/cdk/lib/paint"
)
// wrap the input lines on the nearest word to maxChars
func (w *CWordLine) applyTypographicWrapWord(maxChars int, input []WordLine) (output []WordLine) {
cid, wid, lid := 0, 0, 0
for _, line := range input {
if lid >= len(output) {
output = append(output, NewEmptyWordLine())
}
if maxChars > -1 && line.CharacterCount() > maxChars {
if !line.HasSpace() {
// nothing to break on, truncate on maxChars
for _, word := range line.Words() {
if wid >= output[lid].Len() {
output[lid].AppendWordCell(NewEmptyWordCell())
}
for _, c := range word.Characters() {
if cid >= maxChars {
lid = len(output) // don't append trailing NEWLs
break
}
_ = output[lid].AppendWordRune(wid, c.Value(), c.Style())
cid++
}
wid++
}
continue
}
}
for _, word := range line.Words() {
wordLen := word.Len()
if word.IsSpace() && wordLen > 1 {
wordLen = 1
}
if maxChars > -1 && cid+wordLen > maxChars {
output = append(output, NewEmptyWordLine())
lid = len(output) - 1
wid = -1
cid = 0
if !word.IsSpace() {
output[lid].AppendWordCell(word)
wid = output[lid].Len() - 1
cid += word.Len()
}
} else if word.IsSpace() && maxChars > -1 && cid+wordLen+1 > maxChars {
// continue
} else {
if word.IsSpace() {
if c := word.GetCharacter(0); c != nil {
wc := NewEmptyWordCell()
wc.AppendRune(c.Value(), c.Style())
output[lid].AppendWordCell(wc)
cid += wc.Len()
}
} else {
output[lid].AppendWordCell(word)
cid += word.Len()
}
}
wid++
}
lid++
cid = 0
wid = 0
}
return
}
// wrap the input lines on the nearest word to maxChars if the line has space,
// else, truncate at maxChars
func (w *CWordLine) applyTypographicWrapWordChar(maxChars int, input []WordLine) (output []WordLine) {
for lid, line := range input {
if lid >= len(output) {
output = append(output, NewEmptyWordLine())
}
if maxChars > -1 && line.CharacterCount() > maxChars {
if !line.HasSpace() {
wrapped := w.applyTypographicWrapChar(maxChars, []WordLine{line})
for wLid, wLine := range wrapped {
id := lid + wLid
if id >= len(output) {
output = append(output, NewEmptyWordLine())
}
for _, wWord := range wLine.Words() {
output[id].AppendWordCell(wWord)
}
}
continue
}
}
wrapped := w.applyTypographicWrapWord(maxChars, []WordLine{line})
for wLid, wLine := range wrapped {
id := lid + wLid
if id >= len(output) {
output = append(output, NewEmptyWordLine())
}
for _, wWord := range wLine.Words() {
output[id].AppendWordCell(wWord)
}
}
}
return
}
// wrap the input lines on the nearest character to maxChars
func (w *CWordLine) applyTypographicWrapChar(maxChars int, input []WordLine) (output []WordLine) {
cid, wid, lid := 0, 0, 0
for _, line := range input {
if lid >= len(output) {
output = append(output, NewEmptyWordLine())
}
for _, word := range line.Words() {
if word.IsSpace() {
if maxChars > -1 && cid+1 > maxChars {
output = append(output, NewEmptyWordLine())
lid = len(output) - 1
wid = 0
cid = 0
}
if c := word.GetCharacter(0); c != nil {
wc := NewEmptyWordCell()
wc.AppendRune(c.Value(), c.Style())
output[lid].AppendWordCell(wc)
cid += wc.Len()
}
} else {
if maxChars > -1 && cid+word.Len() > maxChars {
firstHalf, secondHalf := NewEmptyWordCell(), NewEmptyWordCell()
for _, c := range word.Characters() {
if cid < maxChars {
firstHalf.AppendRune(c.Value(), c.Style())
} else {
secondHalf.AppendRune(c.Value(), c.Style())
}
cid++
}
output[lid].AppendWordCell(firstHalf)
output = append(output, NewEmptyWordLine())
lid = len(output) - 1
output[lid].AppendWordCell(secondHalf)
wid = 0
cid = 0
} else {
output[lid].AppendWordCell(word)
cid += word.Len()
}
}
wid++
}
lid++
cid = 0
}
return
}
// truncate the input lines on the nearest character to maxChars
func (w *CWordLine) applyTypographicWrapNone(ellipsize bool, maxChars int, input []WordLine) (output []WordLine) {
cid, lid := 0, 0
for _, line := range input {
if lid >= len(output) {
output = append(output, NewEmptyWordLine())
}
for _, word := range line.Words() {
if word.IsSpace() {
if maxChars > -1 && cid+1 > maxChars {
if ellipsize {
// ellipsize here
eStartIndex := output[lid].CharacterCount() - 1
if eStartIndex > 0 {
output[lid].SetCharacter(eStartIndex, paint.RuneEllipsis)
}
}
break
}
if c := word.GetCharacter(0); c != nil {
wc := NewEmptyWordCell()
wc.AppendRune(c.Value(), c.Style())
output[lid].AppendWordCell(wc)
cid += wc.Len()
}
} else {
if maxChars > -1 && cid+word.Len() > maxChars {
wc := NewEmptyWordCell()
for _, c := range word.Characters() {
if cid+c.Width() > maxChars {
break
}
wc.AppendRune(c.Value(), c.Style())
cid += c.Width()
}
if wc.Len() > 0 {
output[lid].AppendWordCell(wc)
if ellipsize {
// ellipsize here
eStartIndex := output[lid].CharacterCount() - 1
if eStartIndex > 0 {
output[lid].SetCharacter(eStartIndex, paint.RuneEllipsis)
}
}
break
}
} else {
output[lid].AppendWordCell(word)
cid += word.Len()
}
}
}
lid++
cid = 0
}
return
}