Skip to content

Commit 1cef686

Browse files
Merge pull request #25 from debeando/terminal-refresh
Terminal refresh
2 parents ee10a80 + 543300b commit 1cef686

File tree

1 file changed

+99
-16
lines changed

1 file changed

+99
-16
lines changed

terminal/main.go

+99-16
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,58 @@ import (
77
"fmt"
88
"math"
99
"os"
10+
"time"
1011

1112
"golang.org/x/sys/unix"
1213
)
1314

1415
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.
2017
RESET = "\033[0m"
21-
// Reset to default color
18+
// Reset to default color.
2219
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 = "\x1B7"
44+
// Restore the cursor position.
45+
CURSOR_RESTORE_POSITION = "\x1B8"
2546
)
2647

2748
var Output *bufio.Writer = bufio.NewWriter(os.Stdout)
2849
var Screen *bytes.Buffer = new(bytes.Buffer)
2950

3051
func Clear() {
3152
// Clear screen
32-
Output.WriteString(CLEAR)
53+
Output.WriteString(SCREEN_CLEAR)
3354
// Return cursor to top left
34-
Output.WriteString(RESET_CURSOR)
55+
Output.WriteString(CURSOR_RESET)
3556
}
3657

3758
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)
4162
}
4263

4364
func Flush() {
@@ -65,12 +86,74 @@ func Height() int {
6586
return int(ws.Row)
6687
}
6788

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)
7095
}
7196

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+
}
73155

156+
CursorShow()
74157
}
75158

76159
func getWinsize() (*unix.Winsize, error) {

0 commit comments

Comments
 (0)