-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
217 lines (193 loc) · 3.65 KB
/
io.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
208
209
210
211
212
213
214
215
216
217
package main
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"golang.org/x/crypto/ssh/terminal"
)
type ioError struct{ error }
func isTerminal(stream interface{}) bool {
f, ok := stream.(*os.File)
if !ok {
return false
}
return terminal.IsTerminal(int(f.Fd()))
}
func isInteractive(stream interface{}) bool {
f, ok := stream.(*os.File)
if !ok {
// Likely a buffer used during tests.
return true
}
return terminal.IsTerminal(int(f.Fd()))
}
type screen interface {
io.Writer
clear()
}
func newSelectionScreen(r io.Reader, w io.Writer) screen {
for _, fd := range []interface{}{r, w} {
if f, ok := fd.(*os.File); !ok || !terminal.IsTerminal(int(f.Fd())) {
return nopScreen{w}
}
}
s := &selectionScreen{
cursor: newCursor(r, w),
lineCounter: &lineCounter{w: w},
}
return s
}
type nopScreen struct {
io.Writer
}
func (s nopScreen) Write(p []byte) (int, error) {
return s.Writer.Write(p)
}
func (s nopScreen) clear() {}
var (
escSetCursorPosition = "\x1b[%d;%dH"
escCursorPosition = []byte("\x1b[6n")
escEraseTillBottom = []byte("\x1b[J")
)
type selectionScreen struct {
*lineCounter
cursor *cursor
}
func (s *selectionScreen) clear() {
l, c := s.cursor.line, s.cursor.col // previous line/col
L, _ := s.cursor.position()
if L == l && s.lines == 0 { // no lines were written in the meantime
return
}
if s.lines == 0 {
s.lines = -1
}
l = L - s.lines - 1
s.lines = 0
s.cursor.move(l, c)
if _, err := s.Write(escEraseTillBottom); err != nil {
panic(ioError{err})
}
}
type cursor struct {
io.Reader
io.Writer
line, col int
}
func newCursor(r io.Reader, w io.Writer) *cursor {
c := &cursor{
Reader: r,
Writer: w,
}
c.line, c.col = c.position()
return c
}
func (c *cursor) move(line, col int) {
if _, err := fmt.Fprintf(c.Writer, escSetCursorPosition, line, col); err != nil {
panic(ioError{err})
}
c.line, c.col = line, col
}
func (c *cursor) position() (line, col int) {
if _, err := c.Write(escCursorPosition); err != nil {
panic(ioError{err})
}
state, err := terminal.MakeRaw(0)
if err != nil {
panic(ioError{err})
}
defer terminal.Restore(0, state)
var (
x, y []byte
split bool
buf = make([]byte, 1)
)
LOOP:
for {
switch n, err := c.Read(buf); err {
case nil:
case io.EOF:
if n == 0 {
break LOOP
}
default:
panic(ioError{err})
}
b := buf[0]
switch {
case b == 0:
panic(ioError{errors.New("NUL cursor position")})
case b == 0x1b, b == '[':
// skip
case b == ';':
split = true
case b == 'R':
break LOOP
case !split:
x = append(x, b)
case split:
y = append(y, b)
}
buf[0] = 0
}
line, _ = strconv.Atoi(string(x))
col, _ = strconv.Atoi(string(y))
return
}
func readRawByte(r io.Reader) byte {
state, err := terminal.MakeRaw(0)
if err != nil {
panic(ioError{err})
}
defer terminal.Restore(0, state)
return readByte(r)
}
func readByte(r io.Reader) byte {
p := make([]byte, 1)
switch _, err := r.Read(p); err {
case nil, io.EOF:
default:
panic(ioError{err})
}
return p[0]
}
func readLine(r io.Reader) string {
var buf strings.Builder
p := make([]byte, 1)
LOOP:
for {
_, err := r.Read(p)
switch err {
case nil:
case io.EOF:
break LOOP
default:
panic(ioError{err})
}
switch p[0] {
case '\n', '\r':
break LOOP
default:
buf.Write(p)
}
}
return buf.String()
}
type lineCounter struct {
w io.Writer
lines int
bytes int
hasEmptyLine bool
}
var newline = []byte{'\n'}
func (w *lineCounter) Write(p []byte) (int, error) {
n, err := w.w.Write(p)
w.bytes += n
w.lines += bytes.Count(p[:n], newline)
w.hasEmptyLine = len(p) == 1 && p[0] == '\n'
return n, err
}