v/vlib/term/control.v

117 lines
2.5 KiB
V
Raw Normal View History

2022-01-04 10:21:08 +01:00
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
2019-07-01 17:09:22 +02: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 17:09:22 +02: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 17:36:09 +02:00
// C.SetConsoleMode(C.ENABLE_VIRTUAL_TERMINAL_INPUT)
2019-07-01 17:09:22 +02: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 17:09:22 +02: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-19 22:29:37 +01:00
pub fn move(n int, direction string) {
print('\x1b[$n$direction')
flush_stdout()
2019-07-01 17:09:22 +02:00
}
pub fn cursor_up(n int) {
2019-12-19 22:29:37 +01:00
move(n, 'A')
2019-07-01 17:09:22 +02:00
}
pub fn cursor_down(n int) {
2019-12-19 22:29:37 +01:00
move(n, 'B')
2019-07-01 17:09:22 +02:00
}
pub fn cursor_forward(n int) {
2019-12-19 22:29:37 +01:00
move(n, 'C')
2019-07-01 17:09:22 +02:00
}
pub fn cursor_back(n int) {
2019-12-19 22:29:37 +01:00
move(n, 'D')
2019-07-01 17:09:22 +02:00
}
2019-09-14 22:54:14 +02:00
// type: 0 -> current cursor position to end of the screen
// type: 1 -> current cursor position to beginning of the screen
2019-07-01 17:09:22 +02:00
// type: 2 -> clears entire screen
// type: 3 -> clears entire screen and also delete scrollback buffer
2019-07-01 17:09:22 +02:00
pub fn erase_display(t string) {
2019-12-19 22:29:37 +01:00
print('\x1b[' + t + 'J')
flush_stdout()
2019-07-01 17:09:22 +02:00
}
2019-12-19 22:29:37 +01:00
pub fn erase_toend() {
erase_display('0')
2019-07-01 17:09:22 +02:00
}
2019-12-19 22:29:37 +01:00
pub fn erase_tobeg() {
erase_display('1')
2019-07-01 17:09:22 +02:00
}
// clears entire screen and returns cursor to top left-corner
2019-12-19 22:29:37 +01:00
pub fn erase_clear() {
print('\033[H\033[J')
flush_stdout()
2019-07-01 17:09:22 +02:00
}
2019-12-19 22:29:37 +01:00
pub fn erase_del_clear() {
erase_display('3')
2019-07-01 17:09:22 +02:00
}
2019-09-14 22:54:14 +02:00
// type: 0 -> current cursor position to end of the line
// type: 1 -> current cursor position to beginning of the line
2019-07-01 17:09:22 +02:00
// type: 2 -> clears entire line
// Note: Cursor position does not change
pub fn erase_line(t string) {
2019-12-19 22:29:37 +01:00
print('\x1b[' + t + 'K')
flush_stdout()
2019-07-01 17:09:22 +02:00
}
2019-12-19 22:29:37 +01:00
pub fn erase_line_toend() {
erase_line('0')
2019-07-01 17:09:22 +02:00
}
2019-12-19 22:29:37 +01:00
pub fn erase_line_tobeg() {
erase_line('1')
2019-07-01 17:09:22 +02:00
}
2019-12-19 22:29:37 +01:00
pub fn erase_line_clear() {
erase_line('2')
2019-07-01 17:09:22 +02:00
}
// Will make cursor appear if not visible
2019-12-19 22:29:37 +01:00
pub fn show_cursor() {
2019-07-01 17:09:22 +02:00
print('\x1b[?25h')
flush_stdout()
2019-07-01 17:09:22 +02:00
}
// Will make cursor invisible
2019-12-19 22:29:37 +01:00
pub fn hide_cursor() {
2019-07-01 17:09:22 +02:00
print('\x1b[?25l')
flush_stdout()
2019-07-16 17:59:07 +02: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()
}