From 5c598a30859fd058cc4f41c4459437d4f8d752b7 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 29 Jan 2020 07:12:12 +0300 Subject: [PATCH] term: reorganize the module --- vlib/term/can_show_color.v | 28 ------------ vlib/term/colors_nix.v | 5 -- vlib/term/colors_windows.v | 6 --- vlib/term/misc.v | 12 ----- vlib/term/misc_js.v | 6 --- vlib/term/term.v | 48 ++++++++++++++++++++ vlib/term/term_js.v | 8 ++++ vlib/term/{misc_nix.v => term_nix.v} | 6 +-- vlib/term/{misc_windows.v => term_windows.v} | 2 +- 9 files changed, 60 insertions(+), 61 deletions(-) delete mode 100644 vlib/term/can_show_color.v delete mode 100644 vlib/term/colors_nix.v delete mode 100644 vlib/term/colors_windows.v delete mode 100644 vlib/term/misc.v delete mode 100644 vlib/term/misc_js.v create mode 100644 vlib/term/term.v create mode 100644 vlib/term/term_js.v rename vlib/term/{misc_nix.v => term_nix.v} (68%) rename vlib/term/{misc_windows.v => term_windows.v} (94%) diff --git a/vlib/term/can_show_color.v b/vlib/term/can_show_color.v deleted file mode 100644 index 0ba138cdcc..0000000000 --- a/vlib/term/can_show_color.v +++ /dev/null @@ -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 } -} diff --git a/vlib/term/colors_nix.v b/vlib/term/colors_nix.v deleted file mode 100644 index c0989f34f8..0000000000 --- a/vlib/term/colors_nix.v +++ /dev/null @@ -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 - diff --git a/vlib/term/colors_windows.v b/vlib/term/colors_windows.v deleted file mode 100644 index 37274c1957..0000000000 --- a/vlib/term/colors_windows.v +++ /dev/null @@ -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 - diff --git a/vlib/term/misc.v b/vlib/term/misc.v deleted file mode 100644 index b1c845432d..0000000000 --- a/vlib/term/misc.v +++ /dev/null @@ -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] -} diff --git a/vlib/term/misc_js.v b/vlib/term/misc_js.v deleted file mode 100644 index a57d8b948d..0000000000 --- a/vlib/term/misc_js.v +++ /dev/null @@ -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 -} diff --git a/vlib/term/term.v b/vlib/term/term.v new file mode 100644 index 0000000000..501f63e328 --- /dev/null +++ b/vlib/term/term.v @@ -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' + } +} diff --git a/vlib/term/term_js.v b/vlib/term/term_js.v new file mode 100644 index 0000000000..b524e7ca86 --- /dev/null +++ b/vlib/term/term_js.v @@ -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 +} diff --git a/vlib/term/misc_nix.v b/vlib/term/term_nix.v similarity index 68% rename from vlib/term/misc_nix.v rename to vlib/term/term_nix.v index 39d0c2fbb4..d070f02500 100644 --- a/vlib/term/misc_nix.v +++ b/vlib/term/term_nix.v @@ -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) } diff --git a/vlib/term/misc_windows.v b/vlib/term/term_windows.v similarity index 94% rename from vlib/term/misc_windows.v rename to vlib/term/term_windows.v index 573fa9b9bf..fe489ee139 100644 --- a/vlib/term/misc_windows.v +++ b/vlib/term/term_windows.v @@ -31,5 +31,5 @@ pub fn get_terminal_size() (int, int) { } } - return 80, 25 + return default_columns_size, default_rows_size }