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
|
package tk
import (
"sync"
"src.elv.sh/pkg/cli/term"
"src.elv.sh/pkg/ui"
"src.elv.sh/pkg/wcwidth"
)
// TextView is a Widget for displaying text, with support for vertical
// scrolling.
//
// NOTE: This widget now always crops long lines. In future it should support
// wrapping and horizontal scrolling.
type TextView interface {
Widget
// ScrollBy scrolls the widget by the given delta. Positive values scroll
// down, and negative values scroll up.
ScrollBy(delta int)
// MutateState mutates the state.
MutateState(f func(*TextViewState))
// CopyState returns a copy of the State.
CopyState() TextViewState
}
// TextViewSpec specifies the configuration and initial state for a Widget.
type TextViewSpec struct {
// Key bindings.
Bindings Bindings
// If true, a vertical scrollbar will be shown when there are more lines
// that can be displayed, and the widget responds to Up and Down keys.
Scrollable bool
// State. Specifies the initial state if used in New.
State TextViewState
}
// TextViewState keeps mutable state of TextView.
type TextViewState struct {
Lines []string
First int
}
type textView struct {
// Mutex for synchronizing access to the state.
StateMutex sync.RWMutex
TextViewSpec
}
// NewTextView builds a TextView from the given spec.
func NewTextView(spec TextViewSpec) TextView {
if spec.Bindings == nil {
spec.Bindings = DummyBindings{}
}
return &textView{TextViewSpec: spec}
}
func (w *textView) Render(width, height int) *term.Buffer {
lines, first := w.getStateForRender(height)
needScrollbar := w.Scrollable && (first > 0 || first+height < len(lines))
textWidth := width
if needScrollbar {
textWidth--
}
bb := term.NewBufferBuilder(textWidth)
for i := first; i < first+height && i < len(lines); i++ {
if i > first {
bb.Newline()
}
bb.Write(wcwidth.Trim(lines[i], textWidth))
}
buf := bb.Buffer()
if needScrollbar {
scrollbar := VScrollbar{
Total: len(lines), Low: first, High: first + height}
buf.ExtendRight(scrollbar.Render(1, height))
}
return buf
}
func (w *textView) MaxHeight(width, height int) int {
return len(w.CopyState().Lines)
}
func (w *textView) getStateForRender(height int) (lines []string, first int) {
w.MutateState(func(s *TextViewState) {
if s.First > len(s.Lines)-height && len(s.Lines)-height >= 0 {
s.First = len(s.Lines) - height
}
lines, first = s.Lines, s.First
})
return
}
func (w *textView) Handle(event term.Event) bool {
if w.Bindings.Handle(w, event) {
return true
}
if w.Scrollable {
switch event {
case term.K(ui.Up):
w.ScrollBy(-1)
return true
case term.K(ui.Down):
w.ScrollBy(1)
return true
}
}
return false
}
func (w *textView) ScrollBy(delta int) {
w.MutateState(func(s *TextViewState) {
s.First += delta
if s.First < 0 {
s.First = 0
}
if s.First >= len(s.Lines) {
s.First = len(s.Lines) - 1
}
})
}
func (w *textView) MutateState(f func(*TextViewState)) {
w.StateMutex.Lock()
defer w.StateMutex.Unlock()
f(&w.State)
}
// CopyState returns a copy of the State while r-locking the StateMutex.
func (w *textView) CopyState() TextViewState {
w.StateMutex.RLock()
defer w.StateMutex.RUnlock()
return w.State
}
|