v / vlib / term / control.v
131 lines · 113 sloc · 3.52 KB · 37255767290c243b71f0e78d77c3bd5e875748e6
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module term
5
6// Sources for ANSI Control Sequences
7// https://github.com/RajeshPatkarInstitute/Panim
8// https://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html
9// https://en.wikipedia.org/wiki/ANSI_escape_code
10// Support for Windows
11// https://en.wikipedia.org/wiki/ANSI.SYS
12// #include <windows.h>
13// C.SetConsoleMode(C.ENABLE_VIRTUAL_TERMINAL_INPUT)
14// Setting cursor to the given position
15// x is the x coordinate
16// y is the y coordinate
17pub fn set_cursor_position(c Coord) {
18 print('\x1b[${c.y};${c.x}' + 'H')
19 flush_stdout()
20}
21
22// move the cursor relative to its current position.
23// n is number of cells.
24// direction: A is up / North
25// direction: B is down / South
26// direction: C is forward / East
27// direction: D is backward / West
28pub fn move(n int, direction string) {
29 print('\x1b[${n}${direction}')
30 flush_stdout()
31}
32
33// cursor_up moves the cursor up `n` lines.
34pub fn cursor_up(n int) {
35 move(n, 'A')
36}
37
38// cursor_down moves the cursor down `n` lines.
39pub fn cursor_down(n int) {
40 move(n, 'B')
41}
42
43// cursor_forward moves the cursor forward `n` character positions.
44pub fn cursor_forward(n int) {
45 move(n, 'C')
46}
47
48// cursor_back moves the cursor back `n` characters.
49pub fn cursor_back(n int) {
50 move(n, 'D')
51}
52
53// erase_display erases display characters based on the given parameter, `t`.
54// `t` can be of the following values in string:
55// `0`: current cursor position to end of the terminal window.
56// `1`: current cursor position to beginning of the terminal
57// window.
58// `2`: clears the entire terminal window.
59// `3`: clears the entire terminal window and also deletes the scrollback buffer.
60pub fn erase_display(t string) {
61 print('\x1b[' + t + 'J')
62 flush_stdout()
63}
64
65// erase_to_end erases from the cursor to the end of the terminal window.
66pub fn erase_toend() {
67 erase_display('0')
68}
69
70// erase_tobeg erases from the cursor to the beginning of the terminal window.
71pub fn erase_tobeg() {
72 erase_display('1')
73}
74
75// erase_clear clears the entire terminal window and returns the cursor to the top left corner.
76pub fn erase_clear() {
77 print('\033[H\033[J')
78 flush_stdout()
79}
80
81// erase_del_clear erases saved lines.
82pub fn erase_del_clear() {
83 erase_display('3')
84}
85
86// erase_line erases the current line based on the given parameter, `t`.
87// `t` can be of the following values in string:
88// `0`: current cursor position to the end of the line.
89// `1`: current cursor position to the beginning of the line.
90// `2`: clears the entire line.
91// Note: Cursor position does not change.
92pub fn erase_line(t string) {
93 print('\x1b[' + t + 'K')
94 flush_stdout()
95}
96
97// erase_line_toend erases from the cursor position to the end of the line.
98pub fn erase_line_toend() {
99 erase_line('0')
100}
101
102// erase_line_tobeg erases from the start of the line to the cursor position.
103pub fn erase_line_tobeg() {
104 erase_line('1')
105}
106
107// erase_line_clear erases the entire line.
108pub fn erase_line_clear() {
109 erase_line('2')
110}
111
112// show_cursor makes the cursor appear if it was not visible.
113pub fn show_cursor() {
114 print('\x1b[?25h')
115 flush_stdout()
116}
117
118// Will make cursor invisible
119pub fn hide_cursor() {
120 print('\x1b[?25l')
121 flush_stdout()
122}
123
124// clear_previous_line - useful for progressbars.
125// It moves the cursor to start of line, then 1 line above,
126// then erases the line. In effect the next println will overwrite
127// the previous content.
128pub fn clear_previous_line() {
129 print('\r\x1b[1A\x1b[2K')
130 flush_stdout()
131}
132