-
Notifications
You must be signed in to change notification settings - Fork 1
/
termio.go
63 lines (49 loc) · 1.41 KB
/
termio.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
package ns
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"syscall"
"golang.org/x/sys/unix"
)
var CODE_RESET = "\033[0m"
// getCursorPos returns the current cursor position (row, col). row and col start from 1
func getCursorPos() (int, int) {
r := bufio.NewReader(os.Stdin)
// Request the cursor position
fmt.Printf("\033[6n")
b, err := r.ReadString('R')
if err != nil {
panic(fmt.Sprintf("getCursorPos: %s", err.Error()))
}
// len(b) should be _at least_ 6, since the shortest possible valid response
// would be `\033[1;1R`, and `\033` (the escape char) counts as 1.
section := b[2 : len(b)-1]
parts := strings.Split(section, ";")
row, _ := strconv.Atoi(parts[0])
col, _ := strconv.Atoi(parts[1])
return row, col
}
// setCursorPos sets the current cursor position. row and col start from 1
func setCursorPos(row int, col int) {
os.Stdout.WriteString(fmt.Sprintf("\x1B[%d;%dH", row, col))
}
// getWindowDimensions returns the size of the window (row, col)
func getWindowDimensions() (int, int) {
winsize, _ := unix.IoctlGetWinsize(int(os.Stdout.Fd()), syscall.TIOCGWINSZ)
return int(winsize.Row), int(winsize.Col)
}
// clear clears the terminal
func clear() {
os.Stdout.WriteString("\033[H\033[2J")
}
// hideCursor makes the cursor disappear
func hideCursor() {
os.Stdout.WriteString("\x1B[?25l")
}
// showCursor makes the cursor appear
func showCursor() {
os.Stdout.WriteString("\x1B[?25h")
}