-
Notifications
You must be signed in to change notification settings - Fork 1
/
linereserver.go
159 lines (143 loc) · 3.64 KB
/
linereserver.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
package minterm
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"sync"
)
// Should be way easier to implement had it not been by the fact that
// os.Stdout/err aren't interfaces but *os.File. Hopefully Go 2 fixes it:
// https://github.com/golang/go/issues/13473
// Reserves a line at the bottom of the terminal, while normal stdout and
// stderr goes above it. It does not play well any other prints using
// carriage returns.
//
// A side-effect of the implementation is that anything printed that doesn't
// have a newline in it gets buffered instead of printed immediately.
type LineReserver struct {
line string
out, err *os.File
r, w *os.File
flushChan chan struct{}
wait, flushWait sync.WaitGroup
m sync.Mutex
}
// Takes control of stdout and stderr in order to reserve the last line of the terminal,
// which can be set with Set().
func NewLineReserver() (*LineReserver, error) {
// Make sure ahead of time nothing weird happens when we get terminal size.
if _, _, err := TerminalSize(); err != nil {
return nil, err
}
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
lr := &LineReserver{
r: r,
w: w,
out: os.Stdout,
err: os.Stderr,
flushChan: make(chan struct{}),
}
lr.wait.Add(1)
go lr.monitor()
os.Stdout = w
os.Stderr = w
return lr, nil
}
// Clears the reserved line and restores control to stdout and stderr.
func (lr *LineReserver) Release() {
lr.w.Close()
lr.wait.Wait()
os.Stdout = lr.out
os.Stderr = lr.err
lr.w = nil
}
// Sets the reserved line to the desired string.
func (lr *LineReserver) Set(line string) {
lr.m.Lock()
lr.line = line
lr.m.Unlock()
}
// Prints the reserved line again, updating the line if it was changed
// since last time. Note that if something was buffered (i.e. something
// printed without a newline in it), a newline will be appended to avoid
// erasing what was in the buffer.
func (lr *LineReserver) Refresh() {
if lr.w == nil {
return
}
lr.flushWait.Add(1)
lr.flushChan <- struct{}{}
lr.flushWait.Wait()
}
func (lr *LineReserver) monitor() {
defer lr.wait.Done()
c := make(chan []byte)
done := make(chan struct{})
go func() {
buf := make([]byte, 4096)
for {
n, err := lr.r.Read(buf)
if err == io.EOF {
done <- struct{}{}
lr.r.Close()
return
}
outbuf := make([]byte, n)
copy(outbuf, buf[:n])
c <- outbuf
}
}()
var buf bytes.Buffer
for {
select {
case b := <-c:
buf.Write(b)
// Only flush if we got a newline.
if i := bytes.IndexByte(b, '\n'); i != -1 {
lr.printLine(&buf)
}
case <-lr.flushChan:
// We were told to flush.
lr.printLine(&buf)
lr.flushWait.Done()
case <-done:
lr.clearLine()
buf.WriteTo(lr.out)
return
}
}
}
func (lr *LineReserver) printLine(b *bytes.Buffer) {
// We checked ahead that we can get the size, so we discard the error.
// Panic not really an option here, but perhaps some other way of handling
// potential errors should be done here. TODO.
cols, _, _ := TerminalSize()
// Check if the buffer has anything.
var bs string
if b.Len() != 0 {
// We'd end up erasing stuff on the terminal if it doesn't end
// on a newline, so we make sure to add one if there isn't.
bs = ensureSuffix(b.String(), "\n")
}
lr.m.Lock()
out := []byte(fmt.Sprintf("\r%s\r%s%s\r",
strings.Repeat(" ", cols-1), bs, lr.line))
lr.m.Unlock()
lr.out.Write(out)
b.Reset()
}
func (lr *LineReserver) clearLine() {
cols, _, _ := TerminalSize()
lr.out.Write([]byte(fmt.Sprintf("\r%s\r", strings.Repeat(" ", cols-1))))
}
func ensureSuffix(s, suffix string) string {
if !strings.HasSuffix(s, suffix) {
return s + suffix
}
return s
}