-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
37 lines (32 loc) · 821 Bytes
/
view.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
package tui
import (
"fmt"
"github.com/shreve/tui/ansi"
)
type View []string
type Renderable interface {
Render(int, int) View
}
// Draw all the lines in this view.
func (v View) Render() {
for i := 0; i < len(v); i++ {
v.drawLine(i)
}
}
// Only draw lines that differ from a provided view. This is an important
// trade-off. This massively speeds up rendering in most cases, but may cause
// errors when terminal output has changed without our knowledge.
func (v View) RenderFrom(o View) {
for i := 0; i < len(v); i++ {
if i >= len(o) || i >= len(v) || v[i] != o[i] {
v.drawLine(i)
}
}
}
// Draw the ith line of the view to the ith line on the screen
func (v View) drawLine(i int) {
ansi.MoveCursor(i, 0)
ansi.ClearLine()
fmt.Print(v[i])
ansi.ResetDisplay() // Don't allow lines to bleed over
}