v
Зеркало из https://github.com/vlang/v
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) {
18print('\x1b[${c.y};${c.x}' + 'H')
19flush_stdout()
20}
21
22// n is number of cells
23// direction: A is up / North
24// direction: B is down / South
25// direction: C is forward / East
26// direction: D is backward / West
27pub fn move(n int, direction string) {
28print('\x1b[${n}${direction}')
29flush_stdout()
30}
31
32// cursor_up moves the cursor up `n` lines.
33pub fn cursor_up(n int) {
34move(n, 'A')
35}
36
37// cursor_down moves the cursor down `n` lines.
38pub fn cursor_down(n int) {
39move(n, 'B')
40}
41
42// cursor_forward moves the cursor forward `n` character positions.
43pub fn cursor_forward(n int) {
44move(n, 'C')
45}
46
47// cursor_back moves the cursor back `n` characters.
48pub fn cursor_back(n int) {
49move(n, 'D')
50}
51
52// erase_display erases display characters based on the given parameter, `t`.
53// `t` can be of the following values in string:
54// `0`: current cursor position to end of the terminal window.
55// `1`: current cursor position to beginning of the terminal
56// window.
57// `2`: clears the entire terminal window.
58// `3`: clears the entire terminal window and also deletes the scrollback buffer.
59pub fn erase_display(t string) {
60print('\x1b[' + t + 'J')
61flush_stdout()
62}
63
64// erase_to_end erases from the cursor to the end of the terminal window.
65pub fn erase_toend() {
66erase_display('0')
67}
68
69// erase_tobeg erases from the cursor to the beginning of the terminal window.
70pub fn erase_tobeg() {
71erase_display('1')
72}
73
74// erase_clear clears the entire terminal window and returns the cursor to the
75// top left corner.
76pub fn erase_clear() {
77print('\033[H\033[J')
78flush_stdout()
79}
80
81// erase_del_clear erases saved lines.
82pub fn erase_del_clear() {
83erase_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) {
93print('\x1b[' + t + 'K')
94flush_stdout()
95}
96
97// erase_line_toend erases from the cursor position to the end of the line.
98pub fn erase_line_toend() {
99erase_line('0')
100}
101
102// erase_line_tobeg erases from the start of the line to the cursor
103// position.
104pub fn erase_line_tobeg() {
105erase_line('1')
106}
107
108// erase_line_clear erases the entire line.
109pub fn erase_line_clear() {
110erase_line('2')
111}
112
113// show_cursor makes the cursor appear if it was not visible.
114pub fn show_cursor() {
115print('\x1b[?25h')
116flush_stdout()
117}
118
119// Will make cursor invisible
120pub fn hide_cursor() {
121print('\x1b[?25l')
122flush_stdout()
123}
124
125// clear_previous_line - useful for progressbars.
126// It moves the cursor to start of line, then 1 line above,
127// then erases the line. In effect the next println will overwrite
128// the previous content.
129pub fn clear_previous_line() {
130print('\r\x1b[1A\x1b[2K')
131flush_stdout()
132}
133