/
ansi.go
70 lines (58 loc) · 1.54 KB
/
ansi.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
package fineline
import (
"fmt"
"strings"
"syscall"
)
type LineReader struct {
lineReader
origTerm termios
}
// enable raw mode and gather metrics, like number of columns
func (l *LineReader) raw() {
tcgetattr(0, &l.origTerm)
// Modify the original mode
raw := l.origTerm
// Input modes - no break, no CR to NL, no parity check, no strip char,
// no start/stop output control
raw.Iflag &^= BRKINT | ICRNL | INPCK | ISTRIP | IXON
// Output modes - Disable post processing
raw.Oflag &^= OPOST
// Control modes - set 8 bit chars
raw.Cflag |= CS8
// Local modes - Echo off, canonical off, no extended functions, no signal chars
raw.Lflag &^= ECHO | ICANON | IEXTEN | ISIG
raw.Cc[VMIN] = 1
raw.Cc[VTIME] = 0
tcsetattr(0, TCSAFLUSH, &raw)
var win winsize
winIoctl(1, syscall.TIOCGWINSZ, &win)
l.cols = int(win.Col)
}
func (l *LineReader) restore() {
tcsetattr(0, TCSAFLUSH, &l.origTerm)
}
// x is absolute, y is relative
func (l *LineReader) setCursor(x, y int) {
fmt.Printf("\x1b[%dG", x + 1)
// positive is down, negative is up
if y > 0 {
fmt.Printf("\x1b[%dB", y)
} else if y < 0 {
fmt.Printf("\x1b[%dA", -y)
}
}
// erase everything from the cursor to the end of the screen
func (l *LineReader) eraseToEnd() {
// erase to right
fmt.Print("\x1b[0J")
}
func (l *LineReader) printCandidates() {
str := "\n\x1b[0G" + strings.Join(l.candidates, "\n\x1b[0G") + "\n"
fmt.Print(str)
l.refreshLine()
}
func (l *LineReader) clearScreen() {
// move to upper left corner, then clear entire screen
fmt.Print("\x1b[H\x1b[2J")
}