v

Зеркало из https://github.com/vlang/v
Форк
0
/
control.v 
132 строки · 3.5 Кб
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.
4
module 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
17
pub fn set_cursor_position(c Coord) {
18
	print('\x1b[${c.y};${c.x}' + 'H')
19
	flush_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
27
pub fn move(n int, direction string) {
28
	print('\x1b[${n}${direction}')
29
	flush_stdout()
30
}
31

32
// cursor_up moves the cursor up `n` lines.
33
pub fn cursor_up(n int) {
34
	move(n, 'A')
35
}
36

37
// cursor_down moves the cursor down `n` lines.
38
pub fn cursor_down(n int) {
39
	move(n, 'B')
40
}
41

42
// cursor_forward moves the cursor forward `n` character positions.
43
pub fn cursor_forward(n int) {
44
	move(n, 'C')
45
}
46

47
// cursor_back moves the cursor back `n` characters.
48
pub fn cursor_back(n int) {
49
	move(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.
59
pub fn erase_display(t string) {
60
	print('\x1b[' + t + 'J')
61
	flush_stdout()
62
}
63

64
// erase_to_end erases from the cursor to the end of the terminal window.
65
pub fn erase_toend() {
66
	erase_display('0')
67
}
68

69
// erase_tobeg erases from the cursor to the beginning of the terminal window.
70
pub fn erase_tobeg() {
71
	erase_display('1')
72
}
73

74
// erase_clear clears the entire terminal window and returns the cursor to the
75
// top left corner.
76
pub fn erase_clear() {
77
	print('\033[H\033[J')
78
	flush_stdout()
79
}
80

81
// erase_del_clear erases saved lines.
82
pub 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.
92
pub 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.
98
pub fn erase_line_toend() {
99
	erase_line('0')
100
}
101

102
// erase_line_tobeg erases from the start of the line to the cursor
103
// position.
104
pub fn erase_line_tobeg() {
105
	erase_line('1')
106
}
107

108
// erase_line_clear erases the entire line.
109
pub fn erase_line_clear() {
110
	erase_line('2')
111
}
112

113
// show_cursor makes the cursor appear if it was not visible.
114
pub fn show_cursor() {
115
	print('\x1b[?25h')
116
	flush_stdout()
117
}
118

119
// Will make cursor invisible
120
pub fn hide_cursor() {
121
	print('\x1b[?25l')
122
	flush_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.
129
pub fn clear_previous_line() {
130
	print('\r\x1b[1A\x1b[2K')
131
	flush_stdout()
132
}
133

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.