@@ -4,12 +4,14 @@ import os
4
4
5
5
[typedef ]
6
6
struct C.COORD {
7
+ mut :
7
8
X i16
8
9
Y i16
9
10
}
10
11
11
12
[typedef ]
12
13
struct C.SMALL_RECT {
14
+ mut :
13
15
Left u16
14
16
Top u16
15
17
Right u16
@@ -20,19 +22,39 @@ struct C.SMALL_RECT {
20
22
// https://docs.microsoft.com/en-us/windows/console/console-screen-buffer-info-str
21
23
[typedef ]
22
24
struct C.CONSOLE_SCREEN_BUFFER_INFO {
25
+ mut :
23
26
dwSize C.COORD
24
27
dwCursorPosition C.COORD
25
28
wAttributes u16
26
29
srWindow C.SMALL_RECT
27
30
dwMaximumWindowSize C.COORD
28
31
}
29
32
33
+ union C.uChar {
34
+ mut :
35
+ UnicodeChar rune
36
+ AsciiChar byte
37
+ }
38
+
39
+ [typedef ]
40
+ struct C.CHAR_INFO {
41
+ mut :
42
+ Char C.uChar
43
+ Attributes u16
44
+ }
45
+
30
46
// ref - https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo
31
47
fn C.GetConsoleScreenBufferInfo (handle os.HANDLE, info & C.CONSOLE_SCREEN_BUFFER_INFO) bool
32
48
33
49
// ref - https://docs.microsoft.com/en-us/windows/console/setconsoletitle
34
50
fn C.SetConsoleTitle (title & u16 ) bool
35
51
52
+ // ref - https://docs.microsoft.com/en-us/windows/console/setconsolecursorposition
53
+ fn C.SetConsoleCursorPosition (handle os.HANDLE, coord C.COORD) bool
54
+
55
+ // ref - https://docs.microsoft.com/en-us/windows/console/scrollconsolescreenbuffer
56
+ fn C.ScrollConsoleScreenBuffer (output os.HANDLE, scroll_rect & C.SMALL_RECT, clip_rect & C.SMALL_RECT, des C.COORD, fill C.CHAR_INFO) bool
57
+
36
58
// get_terminal_size returns a number of colums and rows of terminal window.
37
59
pub fn get_terminal_size () (int , int ) {
38
60
if is_atty (1 ) > 0 && os.getenv ('TERM' ) != 'dumb' {
@@ -64,3 +86,40 @@ pub fn set_terminal_title(title string) bool {
64
86
title_change := C.SetConsoleTitle (title.to_wide ())
65
87
return title_change
66
88
}
89
+
90
+ // clear clears current terminal screen.
91
+ // Implementation taken from https://docs.microsoft.com/en-us/windows/console/clearing-the-screen#example-2.
92
+ pub fn clear () {
93
+ hconsole := C.GetStdHandle (C.STD_OUTPUT_HANDLE)
94
+ mut csbi := C.CONSOLE_SCREEN_BUFFER_INFO{}
95
+ mut scrollrect := C.SMALL_RECT{}
96
+ mut scrolltarget := C.COORD{}
97
+ mut fill := C.CHAR_INFO{}
98
+
99
+ // Get the number of character cells in the current buffer.
100
+ if ! C.GetConsoleScreenBufferInfo (hconsole, & csbi) {
101
+ return
102
+ }
103
+ // Scroll the rectangle of the entire buffer.
104
+ scrollrect.Left = 0
105
+ scrollrect.Top = 0
106
+ scrollrect.Right = u16 (csbi.dwSize.X)
107
+ scrollrect.Bottom = u16 (csbi.dwSize.Y)
108
+
109
+ // Scroll it upwards off the top of the buffer with a magnitude of the entire height.
110
+ scrolltarget.X = 0
111
+ scrolltarget.Y = (0 - csbi.dwSize.Y)
112
+
113
+ // Fill with empty spaces with the buffer's default text attribute.
114
+ fill.Char.UnicodeChar = rune (` ` )
115
+ fill.Attributes = csbi.wAttributes
116
+
117
+ // Do the scroll
118
+ C.ScrollConsoleScreenBuffer (hconsole, & scrollrect, C.NULL, scrolltarget, & fill)
119
+
120
+ // Move the cursor to the top left corner too.
121
+ csbi.dwCursorPosition.X = 0
122
+ csbi.dwCursorPosition.Y = 0
123
+
124
+ C.SetConsoleCursorPosition (hconsole, csbi.dwCursorPosition)
125
+ }
0 commit comments