term: reorganize the module

pull/3599/head
Alexey 2020-01-29 07:12:12 +03:00 committed by GitHub
parent a14a5fbf95
commit 5c598a3085
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 61 deletions

View File

@ -1,28 +0,0 @@
module term
import os
pub fn can_show_color_on_stdout() bool {
return supports_escape_sequences(1)
}
pub fn can_show_color_on_stderr() bool {
return supports_escape_sequences(2)
}
fn supports_escape_sequences(fd int) bool {
$if windows {
return (is_atty(fd) & 0x0004) > 0 && os.getenv('TERM') != 'dumb' // ENABLE_VIRTUAL_TERMINAL_PROCESSING
} $else {
return is_atty(fd) > 0 && os.getenv('TERM') != 'dumb'
}
}
// ////////////////////////////////////////////
pub fn ok_message(s string) string {
return if can_show_color_on_stdout() { green(s) } else { s }
}
pub fn fail_message(s string) string {
return if can_show_color_on_stdout() { red(s) } else { s }
}

View File

@ -1,5 +0,0 @@
// 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

View File

@ -1,6 +0,0 @@
// 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

View File

@ -1,12 +0,0 @@
module term
// h_divider will return a horizontal divider line with a dynamic width,
// that depends on the current terminal settings
pub fn h_divider(divider string) string {
mut cols := 76
term_cols,_ := get_terminal_size()
if term_cols > 0 {
cols = term_cols
}
result := divider.repeat(1 + (cols / divider.len))
return result[0..cols]
}

View File

@ -1,6 +0,0 @@
module term
pub fn get_terminal_size() (int, int) {
// TODO: find a way to get proper width&height of the terminal on a Javascript environment
return 80, 25
}

48
vlib/term/term.v 100644
View File

@ -0,0 +1,48 @@
module term
import os
const (
default_columns_size = 80
default_rows_size = 25
)
// can_show_color_on_stdout returns true if colors are allowed in stdout;
// returns false otherwise.
pub fn can_show_color_on_stdout() bool {
return supports_escape_sequences(1)
}
// can_show_color_on_stderr returns true if colors are allowed in stderr;
// returns false otherwise.
pub fn can_show_color_on_stderr() bool {
return supports_escape_sequences(2)
}
// ok_message returns a colored string with green color.
// If colors are not allowed, returns a given string.
pub fn ok_message(s string) string {
return if can_show_color_on_stdout() { green(s) } else { s }
}
// fail_message returns a colored string with red color.
// If colors are not allowed, returns a given string.
pub fn fail_message(s string) string {
return if can_show_color_on_stdout() { red(s) } else { s }
}
// h_divider returns a horizontal divider line with a dynamic width,
// that depends on the current terminal settings.
pub fn h_divider(divider string) string {
cols, _ := get_terminal_size()
result := divider.repeat(1 + (cols / divider.len))
return result[0..cols]
}
fn supports_escape_sequences(fd int) bool {
$if windows {
return (is_atty(fd) & 0x0004) > 0 && os.getenv('TERM') != 'dumb' // ENABLE_VIRTUAL_TERMINAL_PROCESSING
} $else {
return is_atty(fd) > 0 && os.getenv('TERM') != 'dumb'
}
}

View File

@ -0,0 +1,8 @@
module term
// get_terminal_size returns a number of colums and rows of terminal window.
pub fn get_terminal_size() (int, int) {
// TODO Find a way to get proper width & height of the terminal
// on a Javascript environment
return default_columns_size, default_rows_size
}

View File

@ -13,12 +13,12 @@ pub:
fn C.ioctl(fd int, request u64, arg voidptr) int
// get_terminal_size returns a number of colums and rows of terminal window.
pub fn get_terminal_size() (int,int) {
if is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
return 80,25
return default_columns_size, default_rows_size
}
w := C.winsize{}
C.ioctl(0, C.TIOCGWINSZ, &w)
C.ioctl(1, C.TIOCGWINSZ, &w)
return int(w.ws_col),int(w.ws_row)
}

View File

@ -31,5 +31,5 @@ pub fn get_terminal_size() (int, int) {
}
}
return 80, 25
return default_columns_size, default_rows_size
}