1 package uiterm // import "layeh.com/barnard/uiterm"
6 "github.com/nsf/termbox-go"
20 func (t *Textview) uiInitialize(ui *Ui) {
24 func (t *Textview) uiSetActive(active bool) {
27 func (t *Textview) uiSetBounds(x0, y0, x1, y1 int) {
36 func (t *Textview) ScrollUp() {
37 if newLine := t.CurrentLine + 1; newLine < len(t.parsedLines) {
38 t.CurrentLine = newLine
43 func (t *Textview) ScrollDown() {
44 if newLine := t.CurrentLine - 1; newLine >= 0 {
45 t.CurrentLine = newLine
50 func (t *Textview) ScrollTop() {
51 if newLine := len(t.parsedLines) - 1; newLine > 0 {
52 t.CurrentLine = newLine
59 func (t *Textview) ScrollBottom() {
64 func (t *Textview) updateParsedLines() {
65 width := t.x1 - t.x0 - 3
67 if t.Lines == nil || width <= 0 {
72 parsed := make([]string, 0, len(t.Lines))
73 for _, line := range t.Lines {
76 reader := strings.NewReader(line)
79 parsed = append(parsed, current)
83 if reader.Len() <= 0 {
85 parsed = append(parsed, current)
89 if ch, _, err := reader.ReadRune(); err == nil {
90 current = current + string(ch)
95 t.parsedLines = parsed
98 func (t *Textview) AddLine(line string) {
99 t.Lines = append(t.Lines, line)
100 t.updateParsedLines()
104 func (t *Textview) Clear() {
111 func (t *Textview) uiDraw() {
115 var reader *strings.Reader
116 line := len(t.parsedLines) - 1 - t.CurrentLine
120 totalLines := len(t.parsedLines)
124 currentScrollLine := t.y1 - 1 - int((float32(t.CurrentLine)/float32(totalLines))*float32(t.y1-t.y0))
125 for y := t.y1 - 1; y >= t.y0; y-- {
126 if t.parsedLines != nil && line >= 0 {
127 reader = strings.NewReader(t.parsedLines[line])
131 for x := t.x0; x < t.x1; x++ {
133 if x == t.x1-1 { // scrollbar
134 if y == currentScrollLine {
139 } else if x < t.x1-3 {
141 if ch, _, err := reader.ReadRune(); err == nil {
146 termbox.SetCell(x, y, chr, termbox.Attribute(t.Fg), termbox.Attribute(t.Bg))
152 func (t *Textview) uiKeyEvent(mod Modifier, key Key) {
155 func (t *Textview) uiCharacterEvent(chr rune) {