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

Restructured termcolor to term module

This commit is contained in:
archanpatkar
2019-07-01 20:39:22 +05:30
committed by Alexander Medvednikov
parent 6ddc57c190
commit 235a7ecd7f
4 changed files with 148 additions and 8 deletions

109
vlib/term/colors.v Normal file
View File

@ -0,0 +1,109 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module term
pub fn format(msg, open, close string) string {
return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm'
}
pub fn bg_black(msg string) string {
return format(msg, '40', '49')
}
pub fn bg_blue(msg string) string {
return format(msg, '44', '49')
}
pub fn bg_cyan(msg string) string {
return format(msg, '46', '49')
}
pub fn bg_green(msg string) string {
return format(msg, '42', '49')
}
pub fn bg_magenta(msg string) string {
return format(msg, '45', '49')
}
pub fn bg_red(msg string) string {
return format(msg, '41', '49')
}
pub fn bg_white(msg string) string {
return format(msg, '47', '49')
}
pub fn bg_yellow(msg string) string {
return format(msg, '43', '49')
}
pub fn black(msg string) string {
return format(msg, '30', '39')
}
pub fn blue(msg string) string {
return format(msg, '34', '39')
}
pub fn bold(msg string) string {
return format(msg, '1', '22')
}
pub fn cyan(msg string) string {
return format(msg, '36', '39')
}
pub fn dim(msg string) string {
return format(msg, '2', '22')
}
pub fn green(msg string) string {
return format(msg, '32', '39')
}
pub fn gray(msg string) string {
return format(msg, '90', '39')
}
pub fn hidden(msg string) string {
return format(msg, '8', '28')
}
pub fn italic(msg string) string {
return format(msg, '3', '23')
}
pub fn inverse(msg string) string {
return format(msg, '7', '27')
}
pub fn magenta(msg string) string {
return format(msg, '35', '39')
}
pub fn reset(msg string) string {
return format(msg, '0', '0')
}
pub fn red(msg string) string {
return format(msg, '31', '39')
}
pub fn strikethrough(msg string) string {
return format(msg, '9', '29')
}
pub fn underline(msg string) string {
return format(msg, '4', '24')
}
pub fn white(msg string) string {
return format(msg, '37', '39')
}
pub fn yellow(msg string) string {
return format(msg, '33', '39')
}

110
vlib/term/control.v Normal file
View File

@ -0,0 +1,110 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module term
// 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>
// C.SetConsoleMode(ENABLE_VIRTUAL_TERMINAL_INPUT)
// Setting cursor to the given position
// x is the x coordinate
// y is the y coordinate
pub fn set_cursor_position(x int,y int) {
print('\x1b[$y;$x;H')
}
// 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
pub fn move(n int,direction string) string {
return '\x1b[$n$direction'
}
pub fn cursor_up(n int) {
print(move(n,'A'))
}
pub fn cursor_down(n int) {
print(move(n,'B'))
}
pub fn cursor_forward(n int) {
print(move(n,'C'))
}
pub fn cursor_back(n int) {
print(move(n,'D'))
}
// type: 0 -> current cursor postion to end of the screen
// type: 1 -> current cursor postion to beginning of the screen
// type: 2 -> clears entire screen
// type: 3 -> clears entire screen and also delete scrollback buffer
pub fn erase_display(t string) {
print('\x1b[' + t + 'J')
}
pub fn erase_toend()
{
erase_display('0')
}
pub fn erase_tobeg()
{
erase_display('1')
}
pub fn erase_clear()
{
erase_display('2')
}
pub fn erase_del_clear()
{
erase_display('3')
}
// type: 0 -> current cursor postion to end of the line
// type: 1 -> current cursor postion to beginning of the line
// type: 2 -> clears entire line
// Note: Cursor position does not change
pub fn erase_line(t string) {
print('\x1b[' + t + 'K')
}
pub fn erase_line_toend()
{
erase_line('0')
}
pub fn erase_line_tobeg()
{
erase_line('1')
}
pub fn erase_line_clear()
{
erase_line('2')
}
// Will make cursor appear if not visible
pub fn show_cursor()
{
print('\x1b[?25h')
}
// Will make cursor invisible
pub fn hide_cursor()
{
print('\x1b[?25l')
}