/
wrap.go
409 lines (353 loc) · 10.6 KB
/
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this 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 wrap implements line wrapping at character or word boundaries.
package wrap
import (
"errors"
"fmt"
"strings"
"unicode"
"github.com/mum4k/termdash/private/canvas/buffer"
"github.com/mum4k/termdash/private/runewidth"
)
// Mode sets the wrapping mode.
type Mode int
// String implements fmt.Stringer()
func (m Mode) String() string {
if n, ok := modeNames[m]; ok {
return n
}
return "ModeUnknown"
}
// modeNames maps Mode values to human readable names.
var modeNames = map[Mode]string{
Never: "WrapModeNever",
AtRunes: "WrapModeAtRunes",
AtWords: "WrapModeAtWords",
}
const (
// Never is the default wrapping mode, which disables line wrapping.
Never Mode = iota
// AtRunes is a wrapping mode where if the width of the text crosses the
// width of the canvas, wrapping is performed at rune boundaries.
AtRunes
// AtWords is a wrapping mode where if the width of the text crosses the
// width of the canvas, wrapping is performed at word boundaries. The
// wrapping still switches back to the AtRunes mode for any words that are
// longer than the width.
AtWords
)
// ValidText validates the provided text for wrapping.
// The text must not be empty, contain any control or
// space characters other than '\n' and ' '.
func ValidText(text string) error {
if text == "" {
return errors.New("the text cannot be empty")
}
for _, c := range text {
if c == ' ' || c == '\n' { // Allowed space and control runes.
continue
}
if unicode.IsControl(c) {
return fmt.Errorf("the provided text %q cannot contain control characters, found: %q", text, c)
}
if unicode.IsSpace(c) {
return fmt.Errorf("the provided text %q cannot contain space character %q", text, c)
}
}
return nil
}
// ValidCells validates the provided cells for wrapping.
// The text in the cells must follow the same rules as described for ValidText.
func ValidCells(cells []*buffer.Cell) error {
var b strings.Builder
for _, c := range cells {
b.WriteRune(c.Rune)
}
return ValidText(b.String())
}
// Cells returns the cells wrapped into individual lines according to the
// specified width and wrapping mode.
//
// This function consumes any cells that contain newline characters and uses
// them to start new lines.
//
// If the mode is AtWords, this function also drops cells with leading space
// character before a word at which the wrap occurs.
func Cells(cells []*buffer.Cell, width int, m Mode) ([][]*buffer.Cell, error) {
if err := ValidCells(cells); err != nil {
return nil, err
}
switch m {
case Never:
case AtRunes:
case AtWords:
default:
return nil, fmt.Errorf("unsupported wrapping mode %v(%d)", m, m)
}
if width <= 0 {
return nil, nil
}
cs := newCellScanner(cells, width, m)
for state := scanCellRunes; state != nil; state = state(cs) {
}
return cs.lines, nil
}
// cellScannerState is a state in the FSM that scans the input text and identifies
// newlines.
type cellScannerState func(*cellScanner) cellScannerState
// cellScanner tracks the progress of scanning the input cells when finding
// lines.
type cellScanner struct {
// cells are the cells being scanned.
cells []*buffer.Cell
// nextIdx is the index of the cell that will be returned by next.
nextIdx int
// wordStartIdx stores the starting index of the current word.
// A starting position of a word includes any leading space characters.
// E.g.: hello world
// ^
// lastWordIdx
wordStartIdx int
// wordEndIdx stores the ending index of the current word.
// The word consists of all indexes that are
// wordStartIdx <= idx < wordEndIdx.
// A word also includes any punctuation after it.
wordEndIdx int
// width is the width of the canvas the text will be drawn on.
width int
// posX tracks the horizontal position of the current cell on the canvas.
posX int
// mode is the wrapping mode.
mode Mode
// atRunesInWord overrides the mode back to AtRunes.
atRunesInWord bool
// lines are the identified lines.
lines [][]*buffer.Cell
// line is the current line.
line []*buffer.Cell
}
// newCellScanner returns a scanner of the provided cells.
func newCellScanner(cells []*buffer.Cell, width int, m Mode) *cellScanner {
return &cellScanner{
cells: cells,
width: width,
mode: m,
}
}
// next returns the next cell and advances the scanner.
// Returns nil when there are no more cells to scan.
func (cs *cellScanner) next() *buffer.Cell {
c := cs.peek()
if c != nil {
cs.nextIdx++
}
return c
}
// peek returns the next cell without advancing the scanner's position.
// Returns nil when there are no more cells to peek at.
func (cs *cellScanner) peek() *buffer.Cell {
if cs.nextIdx >= len(cs.cells) {
return nil
}
return cs.cells[cs.nextIdx]
}
// peekPrev returns the previous cell without changing the scanner's position.
// Returns nil if the scanner is at the first cell.
func (cs *cellScanner) peekPrev() *buffer.Cell {
if cs.nextIdx == 0 {
return nil
}
return cs.cells[cs.nextIdx-1]
}
// wordCells returns all the cells that belong to the current word.
func (cs *cellScanner) wordCells() []*buffer.Cell {
return cs.cells[cs.wordStartIdx:cs.wordEndIdx]
}
// wordWidth returns the width of the current word in cells when printed on the
// terminal.
func (cs *cellScanner) wordWidth() int {
var b strings.Builder
for _, wc := range cs.wordCells() {
b.WriteRune(wc.Rune)
}
return runewidth.StringWidth(b.String())
}
// isWordStart determines if the scanner is at the beginning of a word.
func (cs *cellScanner) isWordStart() bool {
if cs.mode != AtWords {
return false
}
current := cs.peekPrev()
next := cs.peek()
if current == nil || next == nil {
return false
}
switch nr := next.Rune; {
case nr == '\n':
case nr == ' ':
default:
return true
}
return false
}
// scanCellRunes scans the cells a rune at a time.
func scanCellRunes(cs *cellScanner) cellScannerState {
for {
cell := cs.next()
if cell == nil {
return scanEOF
}
r := cell.Rune
if r == '\n' {
return newLineForLineBreak
}
if cs.mode == Never {
return runeToCurrentLine
}
if cs.atRunesInWord && !isWordCell(cell) {
cs.atRunesInWord = false
}
if !cs.atRunesInWord && cs.isWordStart() {
return markWordStart
}
if runeWrapNeeded(r, cs.posX, cs.width) {
return newLineForAtRunes
}
return runeToCurrentLine
}
}
// runeToCurrentLine scans a single cell rune onto the current line.
func runeToCurrentLine(cs *cellScanner) cellScannerState {
cell := cs.peekPrev()
// Move horizontally within the line for each scanned cell.
cs.posX += runewidth.RuneWidth(cell.Rune)
// Copy the cell into the current line.
cs.line = append(cs.line, cell)
return scanCellRunes
}
// newLineForLineBreak processes a newline character cell.
func newLineForLineBreak(cs *cellScanner) cellScannerState {
cs.lines = append(cs.lines, cs.line)
cs.posX = 0
cs.line = nil
return scanCellRunes
}
// newLineForAtRunes processes a line wrap at rune boundaries due to canvas width.
func newLineForAtRunes(cs *cellScanner) cellScannerState {
// The character on which we wrapped will be printed and is the start of
// new line.
cs.lines = append(cs.lines, cs.line)
cs.posX = runewidth.RuneWidth(cs.peekPrev().Rune)
cs.line = []*buffer.Cell{cs.peekPrev()}
return scanCellRunes
}
// scanEOF terminates the scanning.
func scanEOF(cs *cellScanner) cellScannerState {
// Need to add the current line if it isn't empty, or if the previous rune
// was a newline.
// Newlines aren't copied onto the lines so just checking for emptiness
// isn't enough. We still want to include trailing empty newlines if
// they are in the input text.
if len(cs.line) > 0 || cs.peekPrev().Rune == '\n' {
cs.lines = append(cs.lines, cs.line)
}
return nil
}
// markWordStart stores the starting position of the current word.
func markWordStart(cs *cellScanner) cellScannerState {
cs.wordStartIdx = cs.nextIdx - 1
cs.wordEndIdx = cs.nextIdx
return scanWord
}
// scanWord scans the entire word until it finds its end.
func scanWord(cs *cellScanner) cellScannerState {
for {
if isWordCell(cs.peek()) {
cs.next()
cs.wordEndIdx++
continue
}
return wordToCurrentLine
}
}
// wordToCurrentLine decides how to place the word into the output.
func wordToCurrentLine(cs *cellScanner) cellScannerState {
wordCells := cs.wordCells()
wordWidth := cs.wordWidth()
if cs.posX+wordWidth <= cs.width {
// Place the word onto the current line.
cs.posX += wordWidth
cs.line = append(cs.line, wordCells...)
return scanCellRunes
}
return wrapWord
}
// wrapWord wraps the word onto the next line or lines.
func wrapWord(cs *cellScanner) cellScannerState {
// Edge-case - the word starts the line and immediately doesn't fit.
if cs.posX > 0 {
cs.lines = append(cs.lines, cs.line)
cs.posX = 0
cs.line = nil
}
for i, wc := range cs.wordCells() {
if i == 0 && wc.Rune == ' ' {
// Skip the leading space when word wrapping.
continue
}
if !runeWrapNeeded(wc.Rune, cs.posX, cs.width) {
cs.posX += runewidth.RuneWidth(wc.Rune)
cs.line = append(cs.line, wc)
continue
}
// Replace the last placed rune with a dash indicating we wrapped the
// word. Only do this for half-width runes.
lastIdx := len(cs.line) - 1
last := cs.line[lastIdx]
lastRW := runewidth.RuneWidth(last.Rune)
if cs.width > 1 && lastRW == 1 {
cs.line[lastIdx] = buffer.NewCell('-', last.Opts)
// Reset the scanner's position back to start scanning at the first
// rune of this word that wasn't placed.
cs.nextIdx = cs.wordStartIdx + i - 1
} else {
// Edge-case width is one, no space to put the dash rune.
cs.nextIdx = cs.wordStartIdx + i
}
cs.atRunesInWord = true
return scanCellRunes
}
cs.nextIdx = cs.wordEndIdx
return scanCellRunes
}
// isWordCell determines if the cell contains a rune that belongs to a word.
func isWordCell(c *buffer.Cell) bool {
if c == nil {
return false
}
switch r := c.Rune; {
case r == '\n':
case r == ' ':
default:
return true
}
return false
}
// runeWrapNeeded returns true if wrapping is needed for the rune at the horizontal
// position on the canvas that has the specified width.
func runeWrapNeeded(r rune, posX, width int) bool {
rw := runewidth.RuneWidth(r)
return posX > width-rw
}