1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/term/control.v

133 lines
3.5 KiB
V
Raw Normal View History

2023-03-28 23:55:57 +03:00
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
2019-07-01 18:09:22 +03:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module term
2019-07-01 18:09:22 +03:00
// Sources for ANSI Control Sequences
// https://github.com/RajeshPatkarInstitute/Panim
// https://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html
// https://en.wikipedia.org/wiki/ANSI_escape_code
// Support for Windows
// https://en.wikipedia.org/wiki/ANSI.SYS
// #include <windows.h>
2020-05-22 18:36:09 +03:00
// C.SetConsoleMode(C.ENABLE_VIRTUAL_TERMINAL_INPUT)
2019-07-01 18:09:22 +03:00
// Setting cursor to the given position
// x is the x coordinate
// y is the y coordinate
pub fn set_cursor_position(c Coord) {
print('\x1b[${c.y};${c.x}' + 'H')
flush_stdout()
2019-07-01 18:09:22 +03:00
}
// n is number of cells
// direction: A is up / North
// direction: B is down / South
// direction: C is forward / East
// direction: D is backward / West
2019-12-20 00:29:37 +03:00
pub fn move(n int, direction string) {
print('\x1b[${n}${direction}')
flush_stdout()
2019-07-01 18:09:22 +03:00
}
// cursor_up moves the cursor up `n` lines.
2019-07-01 18:09:22 +03:00
pub fn cursor_up(n int) {
2019-12-20 00:29:37 +03:00
move(n, 'A')
2019-07-01 18:09:22 +03:00
}
// cursor_down moves the cursor down `n` lines.
2019-07-01 18:09:22 +03:00
pub fn cursor_down(n int) {
2019-12-20 00:29:37 +03:00
move(n, 'B')
2019-07-01 18:09:22 +03:00
}
// cursor_forward moves the cursor forward `n` character positions.
2019-07-01 18:09:22 +03:00
pub fn cursor_forward(n int) {
2019-12-20 00:29:37 +03:00
move(n, 'C')
2019-07-01 18:09:22 +03:00
}
// cursor_back moves the cursor back `n` characters.
2019-07-01 18:09:22 +03:00
pub fn cursor_back(n int) {
2019-12-20 00:29:37 +03:00
move(n, 'D')
2019-07-01 18:09:22 +03:00
}
// erase_display erases display characters based on the given parameter, `t`.
// `t` can be of the following values in string:
// `0`: current cursor position to end of the terminal window.
// `1`: current cursor position to beginning of the terminal
// window.
// `2`: clears the entire terminal window.
// `3`: clears the entire terminal window and also deletes the scrollback buffer.
2019-07-01 18:09:22 +03:00
pub fn erase_display(t string) {
2019-12-20 00:29:37 +03:00
print('\x1b[' + t + 'J')
flush_stdout()
2019-07-01 18:09:22 +03:00
}
// erase_to_end erases from the cursor to the end of the terminal window.
2019-12-20 00:29:37 +03:00
pub fn erase_toend() {
erase_display('0')
2019-07-01 18:09:22 +03:00
}
// erase_tobeg erases from the cursor to the beginning of the terminal window.
2019-12-20 00:29:37 +03:00
pub fn erase_tobeg() {
erase_display('1')
2019-07-01 18:09:22 +03:00
}
// erase_clear clears the entire terminal window and returns the cursor to the
// top left corner.
2019-12-20 00:29:37 +03:00
pub fn erase_clear() {
print('\033[H\033[J')
flush_stdout()
2019-07-01 18:09:22 +03:00
}
// erase_del_clear erases saved lines.
2019-12-20 00:29:37 +03:00
pub fn erase_del_clear() {
erase_display('3')
2019-07-01 18:09:22 +03:00
}
// erase_line erases the current line based on the given parameter, `t`.
// `t` can be of the following values in string:
// `0`: current cursor position to the end of the line.
// `1`: current cursor position to the beginning of the line.
// `2`: clears the entire line.
// Note: Cursor position does not change.
2019-07-01 18:09:22 +03:00
pub fn erase_line(t string) {
2019-12-20 00:29:37 +03:00
print('\x1b[' + t + 'K')
flush_stdout()
2019-07-01 18:09:22 +03:00
}
// erase_line_toend erases from the cursor position to the end of the line.
2019-12-20 00:29:37 +03:00
pub fn erase_line_toend() {
erase_line('0')
2019-07-01 18:09:22 +03:00
}
// erase_line_tobeg erases from the start of the line to the cursor
// position.
2019-12-20 00:29:37 +03:00
pub fn erase_line_tobeg() {
erase_line('1')
2019-07-01 18:09:22 +03:00
}
// erase_line_clear erases the entire line.
2019-12-20 00:29:37 +03:00
pub fn erase_line_clear() {
erase_line('2')
2019-07-01 18:09:22 +03:00
}
// show_cursor makes the cursor appear if it was not visible.
2019-12-20 00:29:37 +03:00
pub fn show_cursor() {
2019-07-01 18:09:22 +03:00
print('\x1b[?25h')
flush_stdout()
2019-07-01 18:09:22 +03:00
}
// Will make cursor invisible
2019-12-20 00:29:37 +03:00
pub fn hide_cursor() {
2019-07-01 18:09:22 +03:00
print('\x1b[?25l')
flush_stdout()
2019-07-16 18:59:07 +03:00
}
// clear_previous_line - useful for progressbars.
// It moves the cursor to start of line, then 1 line above,
// then erases the line. In effect the next println will overwrite
// the previous content.
pub fn clear_previous_line() {
print('\r\x1b[1A\x1b[2K')
flush_stdout()
}