@@ -7,37 +7,58 @@ import (
7
7
"fmt"
8
8
"math"
9
9
"os"
10
+ "time"
10
11
11
12
"golang.org/x/sys/unix"
12
13
)
13
14
14
15
const (
15
- // Clear screen
16
- CLEAR = "\033 [2J"
17
- // Return cursor to top left
18
- RESET_CURSOR = "\033 [H"
19
- // Reset all custom styles
16
+ // Reset all custom styles.
20
17
RESET = "\033 [0m"
21
- // Reset to default color
18
+ // Reset to default color.
22
19
RESET_COLOR = "\033 [39;49m"
23
- // Return cursor to start of line and clean it
24
- RESET_LINE = "\r \033 [K"
20
+ // Return cursor to start of line and clean it.
21
+ LINE_RESET = "\r \033 [K"
22
+ // Erase the entire line.
23
+ LINE_ERASE = "\x1B [2K"
24
+ // Restore screen.
25
+ SCREEN_RESTORE = "\x1B [?47l"
26
+ // Save screen.
27
+ SCREEN_SAVE = "\x1B [?47h"
28
+ // Clear complete screen.
29
+ SCREEN_CLEAR = "\033 [2J"
30
+ // Return cursor to top left.
31
+ CURSOR_RESET = "\033 [H"
32
+ // Hide cursor.
33
+ CURSOR_HIDE = "\x1b [?25l"
34
+ // Show cursor.
35
+ CURSOR_SHOW = "\x1b [?25h"
36
+ // Erase from cursor to beginning of screen.
37
+ CURSOR_ERASE_FBS = "\x1B [1J"
38
+ // Erase from cursor to end of screen.
39
+ CURSOR_ERASE_FES = "\x1B [0J"
40
+ // Set new cursor position on screen.
41
+ CURSOR_SET_POSITION = "\x1B [%d;%dH"
42
+ // Save the cursor position.
43
+ CURSOR_SAVE_POSITION = "\x1B 7"
44
+ // Restore the cursor position.
45
+ CURSOR_RESTORE_POSITION = "\x1B 8"
25
46
)
26
47
27
48
var Output * bufio.Writer = bufio .NewWriter (os .Stdout )
28
49
var Screen * bytes.Buffer = new (bytes.Buffer )
29
50
30
51
func Clear () {
31
52
// Clear screen
32
- Output .WriteString (CLEAR )
53
+ Output .WriteString (SCREEN_CLEAR )
33
54
// Return cursor to top left
34
- Output .WriteString (RESET_CURSOR )
55
+ Output .WriteString (CURSOR_RESET )
35
56
}
36
57
37
58
func Reset () {
38
- fmt . Print (RESET )
39
- fmt . Print (RESET_COLOR )
40
- fmt . Print ( RESET_LINE )
59
+ Output . WriteString (RESET )
60
+ Output . WriteString (RESET_COLOR )
61
+ Output . WriteString ( LINE_RESET )
41
62
}
42
63
43
64
func Flush () {
@@ -65,12 +86,74 @@ func Height() int {
65
86
return int (ws .Row )
66
87
}
67
88
68
- func Cursor (x , y int ) {
69
- fmt .Printf ("\x1B [%d;%dH" , x , y )
89
+ func ScreenSave () {
90
+ Output .WriteString (SCREEN_SAVE )
91
+ }
92
+
93
+ func ScreenRestore () {
94
+ Output .WriteString (SCREEN_RESTORE )
70
95
}
71
96
72
- func Refresh (f func ()) {
97
+ func LineReset () {
98
+ Output .WriteString (LINE_RESET )
99
+ }
100
+
101
+ func LineErase () {
102
+ Output .WriteString (LINE_ERASE )
103
+ }
104
+
105
+ func CursorHide () {
106
+ Output .WriteString (CURSOR_HIDE )
107
+ }
108
+
109
+ func CursorShow () {
110
+ Output .WriteString (CURSOR_SHOW )
111
+ Flush ()
112
+ }
113
+
114
+ func CursorRestore () {
115
+ Output .WriteString (CURSOR_RESTORE_POSITION )
116
+ Flush ()
117
+ }
118
+
119
+ func CursorSave () {
120
+ Output .WriteString (CURSOR_SAVE_POSITION )
121
+ }
122
+
123
+ func CursorSet (x , y int ) {
124
+ Output .WriteString (fmt .Sprintf (CURSOR_SET_POSITION , x , y ))
125
+ }
126
+
127
+ func CursorEraseToEndScreen () {
128
+ Output .WriteString (CURSOR_ERASE_FES )
129
+ }
130
+
131
+ func CursorEraseToBeginningScreen () {
132
+ Output .WriteString (CURSOR_ERASE_FBS )
133
+ }
134
+
135
+ func Refresh (wait int , f func ()) {
136
+ Reset ()
137
+ Clear ()
138
+ CursorHide ()
139
+ Flush ()
140
+
141
+ for {
142
+ CursorSave ()
143
+ LineErase ()
144
+ CursorEraseToEndScreen ()
145
+ ScreenSave ()
146
+ CursorEraseToBeginningScreen ()
147
+ ScreenRestore ()
148
+ defer CursorRestore ()
149
+
150
+ CursorSet (0 ,0 )
151
+ Flush ()
152
+ f ()
153
+ time .Sleep (time .Duration (wait ) * time .Second )
154
+ }
73
155
156
+ CursorShow ()
74
157
}
75
158
76
159
func getWinsize () (* unix.Winsize , error ) {
0 commit comments