-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
110 lines (94 loc) · 1.53 KB
/
term.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
package main
import (
"github.com/gdamore/tcell"
)
// this file contains terminal setup and user input/output handling
type pos struct {
x, y int
}
type size struct {
w, h int
}
type obj struct {
pos
size
}
type term struct {
obj
style tcell.Style
screen tcell.Screen
modified bool
}
func (t *term) init() (err error) {
if t.screen, err = tcell.NewScreen(); err != nil {
return
}
if err = t.screen.Init(); err != nil {
return
}
t.reset()
return
}
func (t *term) reset() {
t.style = t.style.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)
t.screen.SetStyle(t.style)
t.screen.Clear()
t.w, t.h = t.screen.Size()
t.setCursor(pos{0, 0})
}
func (t *term) flush() {
if t.modified {
t.screen.Sync()
t.w, t.h = t.screen.Size()
t.setCursor(t.pos)
t.modified = false
}
}
func (t *term) setCursor(p pos) {
t.pos = p
}
func (t *term) showCursor() {
t.screen.ShowCursor(t.x, t.y)
}
func (t *term) hideCursor() {
t.screen.HideCursor()
}
func (t *term) writeRune(c rune) {
if c == '\n' {
t.y++
} else if c == '\r' {
t.x = 0
} else {
t.screen.SetContent(t.x, t.y, c, nil, t.style)
t.x++
}
t.setCursor(t.pos)
}
func (t *term) writeWrap(s string) {
if len(s) == 0 {
return
}
t.modified = true
for _, c := range s {
if t.x >= t.w {
t.x = 0
t.y++
}
if t.y >= t.h {
t.x, t.y = 0, 0
}
t.writeRune(c)
}
}
func (t *term) writeOverflow(s string) {
if len(s) == 0 {
return
}
t.modified = true
for _, c := range s {
t.writeRune(c)
}
}
func (t *term) close() {
t.screen.Fini()
}