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
|
// Copyright 2021 Graham Clark. All rights reserved. Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.
// Package paragraph provides a simple text widget that neatly splits
// over multiple lines.
package paragraph
import (
"strings"
"github.com/gcla/gowid"
"github.com/gcla/gowid/gwutil"
)
//======================================================================
// Widget can be used to display text on the screen, with words broken
// cleanly as the output width changes.
type Widget struct {
words []string
lastcols int
lastrows int
gowid.RejectUserInput
gowid.NotSelectable
}
var _ gowid.IWidget = (*Widget)(nil)
func New(text string) *Widget {
return &Widget{
words: strings.Fields(text),
}
}
func NewWithWords(words ...string) *Widget {
return &Widget{
words: words,
}
}
func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
switch size := size.(type) {
case gowid.IRenderFlowWith:
res := gowid.NewCanvas()
var lc gowid.ICanvas
var pos int
var curWord string
i := 0
space := false
for {
if i >= len(w.words) {
if lc != nil {
res.AppendBelow(lc, false, false)
}
break
}
if curWord == "" {
curWord = w.words[i]
}
if lc == nil {
lc = gowid.NewCanvasOfSize(size.FlowColumns(), 1)
pos = 0
}
if space {
if pos < size.FlowColumns()-1 {
pos++
} else {
if lc != nil {
res.AppendBelow(lc, false, false)
lc = gowid.NewCanvasOfSize(size.FlowColumns(), 1)
pos = 0
}
}
space = false
} else {
// No space left in current line
if len(curWord) > size.FlowColumns()-pos {
// No space on this line, but it will fit on next line. If it
// doesn't even fit on a line by itself, well just split it
if pos > 0 && lc != nil && len(curWord) <= size.FlowColumns() {
res.AppendBelow(lc, false, false)
lc = gowid.NewCanvasOfSize(size.FlowColumns(), 1)
pos = 0
}
take := gwutil.Min(size.FlowColumns()-pos, len(curWord))
for j := 0; j < take; j++ {
lc.SetCellAt(pos, 0, gowid.CellFromRune(rune(curWord[j])))
pos++
}
if pos >= size.FlowColumns() {
res.AppendBelow(lc, false, false)
lc = nil
}
if take == len(curWord) {
i++
curWord = ""
space = true
} else {
// Must be less
curWord = curWord[take:]
}
} else {
for j := 0; j < len(curWord); j++ {
lc.SetCellAt(pos, 0, gowid.CellFromRune(rune(curWord[j])))
pos++
}
i++
curWord = ""
space = true
}
}
}
return res
default:
panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IRenderFlow"})
}
}
func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
if w.lastcols != 0 {
return gowid.RenderBox{C: w.lastcols, R: w.lastrows}
}
res := gowid.CalculateRenderSizeFallback(w, size, focus, app)
w.lastcols = res.C
w.lastrows = res.R
return res
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End:
|