generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
153 lines (127 loc) · 3.31 KB
/
utils.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
package cursor
import (
"io"
"os"
)
//
// Helpers for global cursor handling on os.Stdout
//
var (
autoheight int //nolint:gochecknoglobals
cursor = &Cursor{writer: os.Stdout} //nolint:gochecknoglobals
)
// Writer is an expanded io.Writer interface with a file descriptor.
type Writer interface {
io.Writer
Fd() uintptr
}
// SetTarget sets to output target of the default curser to the
// provided cursor.Writer (wrapping io.Writer).
func SetTarget(w Writer) {
cursor = cursor.WithWriter(w)
}
// Up moves the cursor n lines up relative to the current position.
func Up(n int) {
cursor.Up(n)
autoheight += n
}
// Down moves the cursor n lines down relative to the current position.
func Down(n int) {
cursor.Down(n)
if autoheight > 0 {
autoheight -= n
}
}
// Right moves the cursor n characters to the right relative to the current position.
func Right(n int) {
cursor.Right(n)
}
// Left moves the cursor n characters to the left relative to the current position.
func Left(n int) {
cursor.Left(n)
}
// HorizontalAbsolute moves the cursor to n horizontally.
// The position n is absolute to the start of the line.
func HorizontalAbsolute(n int) {
cursor.HorizontalAbsolute(n)
}
// Show the cursor if it was hidden previously.
// Don't forget to show the cursor at least at the end of your application.
// Otherwise the user might have a terminal with a permanently hidden cursor, until they reopen the terminal.
func Show() {
cursor.Show()
}
// Hide the cursor.
// Don't forget to show the cursor at least at the end of your application with Show.
// Otherwise the user might have a terminal with a permanently hidden cursor, until they reopen the terminal.
func Hide() {
cursor.Hide()
}
// ClearLine clears the current line and moves the cursor to it's start position.
func ClearLine() {
cursor.ClearLine()
}
// Clear clears the current position and moves the cursor to the left.
func Clear() {
cursor.Clear()
}
// Bottom moves the cursor to the bottom of the terminal.
// This is done by calculating how many lines were moved by Up and Down.
func Bottom() {
if autoheight > 0 {
Down(autoheight)
StartOfLine()
autoheight = 0
}
}
// StartOfLine moves the cursor to the start of the current line.
func StartOfLine() {
HorizontalAbsolute(0)
}
// StartOfLineDown moves the cursor down by n lines, then moves to cursor to the start of the line.
func StartOfLineDown(n int) {
Down(n)
StartOfLine()
}
// StartOfLineUp moves the cursor up by n lines, then moves to cursor to the start of the line.
func StartOfLineUp(n int) {
Up(n)
StartOfLine()
}
// UpAndClear moves the cursor up by n lines, then clears the line.
func UpAndClear(n int) {
Up(n)
ClearLine()
}
// DownAndClear moves the cursor down by n lines, then clears the line.
func DownAndClear(n int) {
Down(n)
ClearLine()
}
// Move moves the cursor relative by x and y.
func Move(x, y int) {
if x > 0 {
Right(x)
} else if x < 0 {
x *= -1
Left(x)
}
if y > 0 {
Up(y)
} else if y < 0 {
y *= -1
Down(y)
}
}
// ClearLinesUp clears n lines upwards from the current position and moves the cursor.
func ClearLinesUp(n int) {
for i := 0; i < n; i++ {
UpAndClear(1)
}
}
// ClearLinesDown clears n lines downwards from the current position and moves the cursor.
func ClearLinesDown(n int) {
for i := 0; i < n; i++ {
DownAndClear(1)
}
}