forked from wagoodman/bashful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.go
207 lines (179 loc) · 4.41 KB
/
screen.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"fmt"
"sync"
"sync/atomic"
"github.com/k0kubun/go-ansi"
terminal "github.com/wayneashleyberry/terminal-dimensions"
)
var (
instance *screen
once sync.Once
terminalWidth = terminal.Width
)
// Once is an object that will perform exactly one action.
type Once struct {
m sync.Mutex
done uint32
}
// Do calls the function f if and only if Do is being called for the
// first time for this instance of Once. In other words, given
// var once Once
// if once.Do(f) is called multiple times, only the first call will invoke f,
// even if f has a different value in each invocation. A new instance of
// Once is required for each function to execute.
//
// Do is intended for initialization that must be run exactly once. Since f
// is niladic, it may be necessary to use a function literal to capture the
// arguments to a function to be invoked by Do:
// config.once.Do(func() { config.init(filename) })
//
// Because no call to Do returns until the one call to f returns, if f causes
// Do to be called, it will deadlock.
//
// If f panics, Do considers it to have returned; future calls of Do return
// without calling f.
//
func (o *Once) Do(f func()) {
if atomic.LoadUint32(&o.done) == 1 {
return
}
o.m.Lock()
defer o.m.Unlock()
if o.done == 0 {
defer atomic.StoreUint32(&o.done, 1)
f()
}
}
type screen struct {
numLines int
curLine int
hasHeader bool
hasFooter bool
}
func Screen() *screen {
once.Do(func() {
instance = &screen{}
})
return instance
}
func visualLength(str string) int {
inEscapeSeq := false
length := 0
for _, r := range str {
switch {
case inEscapeSeq:
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') {
inEscapeSeq = false
}
case r == '\x1b':
inEscapeSeq = true
default:
length++
}
}
return length
}
func trimToVisualLength(message string, length int) string {
for visualLength(message) > length && len(message) > 1 {
message = message[:len(message)-1]
}
return message
}
func (scr *screen) ResetFrame(numLines int, hasHeader, hasFooter bool) {
scr.curLine = 0
scr.numLines = numLines
scr.hasFooter = hasFooter
scr.hasHeader = hasHeader
if hasHeader {
// note: this index doesn't count!
fmt.Println("")
}
for idx := 0; idx < numLines; idx++ {
scr.printLn("")
}
if hasFooter {
scr.printLn("")
}
scr.MoveCursorToFirstLine()
}
func (scr *screen) MoveCursor(index int) {
// move to the first possible line (first line or header) if asked to move beyond defined frame
if index < 0 && !scr.hasHeader {
index = 0
}
if index < -1 && scr.hasHeader {
index = -1
}
// move to the last possible line (last line or footer) if asked to move beyond defined frame
if index > scr.numLines-1 && !scr.hasFooter {
index = scr.numLines - 1
}
if index > scr.numLines && scr.hasFooter {
index = scr.numLines
}
moves := scr.curLine - index
if moves != 0 {
if moves < 0 {
ansi.CursorDown(moves * -1)
} else {
ansi.CursorUp(moves)
}
scr.curLine -= moves
}
}
func (scr *screen) MoveCursorToFirstLine() {
scr.MoveCursor(0)
}
func (scr *screen) MoveCursorToLastLine() {
scr.MoveCursor(scr.numLines - 1)
}
func (scr *screen) MoveCursorToHeader() {
scr.MoveCursor(-1)
}
func (scr *screen) MoveCursorToFooter() {
scr.MoveCursor(scr.numLines)
}
func (scr *screen) DisplayFooter(message string) {
scr.MoveCursorToFooter()
scr.printLn(message)
}
func (scr *screen) DisplayHeader(message string) {
scr.MoveCursorToHeader()
scr.printLn(message)
}
func (scr *screen) EraseBelowHeader() {
// erase from the first to the last line
scr.MoveCursorToFirstLine()
for idx := 0; idx < scr.numLines; idx++ {
scr.printLn("")
}
// additionally delete footer
if scr.hasFooter {
scr.DisplayFooter("")
}
}
func (scr *screen) MovePastFrame(keepFooter bool) {
scr.MoveCursorToFooter()
if scr.hasFooter && keepFooter || !scr.hasFooter {
ansi.CursorDown(1)
scr.curLine++
}
}
func (scr *screen) Display(message string, index int) {
scr.MoveCursor(index)
// trim message length if it won't fit on the screen
width, err := terminalWidth()
CheckError(err, "Unable to determine screen width.")
for visualLength(message) > int(width) {
message = trimToVisualLength(message, int(width)-3) + "..."
}
scr.printLn(message)
}
func (scr *screen) printLn(message string) {
ansi.EraseInLine(2)
ansi.CursorHorizontalAbsolute(0)
// note: ansi cursor down cannot be used as this may be the last row
fmt.Println(message)
scr.curLine++
}