From 78c96fe9890fbccd2c8ec42a06a4b4171b6f34e0 Mon Sep 17 00:00:00 2001 From: Mateo Pidal Date: Tue, 28 Jan 2020 01:18:19 -0300 Subject: [PATCH] term: get_terminal_size() --- tools/modules/testing/common.v | 4 ++-- vlib/readline/readline_linux.v | 2 +- vlib/term/can_show_color.v | 15 --------------- vlib/term/misc.v | 15 +++++++++++++++ vlib/term/misc_js.v | 6 ++++++ vlib/term/misc_nix.v | 20 ++++++++++++++++++++ vlib/term/misc_windows.v | 6 ++++++ 7 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 vlib/term/misc.v create mode 100644 vlib/term/misc_js.v create mode 100644 vlib/term/misc_nix.v create mode 100644 vlib/term/misc_windows.v diff --git a/tools/modules/testing/common.v b/tools/modules/testing/common.v index 4921929845..884c8aa45a 100644 --- a/tools/modules/testing/common.v +++ b/tools/modules/testing/common.v @@ -95,7 +95,7 @@ pub fn (ts mut TestSession) test() { } ts.waitgroup.wait() ts.benchmark.stop() - eprintln(term.h_divider()) + eprintln(term.h_divider('-')) } @@ -258,7 +258,7 @@ pub fn building_any_v_binaries_failed() bool { eprintln(bmark.step_message_ok('command: ${cmd}')) } bmark.stop() - eprintln(term.h_divider()) + eprintln(term.h_divider('-')) eprintln(bmark.total_message('building v binaries')) return failed } diff --git a/vlib/readline/readline_linux.v b/vlib/readline/readline_linux.v index 9d0e291200..df3217c6d0 100644 --- a/vlib/readline/readline_linux.v +++ b/vlib/readline/readline_linux.v @@ -36,7 +36,7 @@ enum Action { fn C.tcgetattr() int fn C.tcsetattr() int -fn C.ioctl() int +//fn C.ioctl() int fn C.raise() // Toggle raw mode of the terminal by changing its attributes diff --git a/vlib/term/can_show_color.v b/vlib/term/can_show_color.v index ae060c9cc2..0ba138cdcc 100644 --- a/vlib/term/can_show_color.v +++ b/vlib/term/can_show_color.v @@ -26,18 +26,3 @@ pub fn ok_message(s string) string { pub fn fail_message(s string) string { return if can_show_color_on_stdout() { red(s) } else { s } } - -// h_divider will return a horizontal divider line with a dynamic width, -// that depends on the current terminal settings -pub fn h_divider() string { - mut cols := 76 - if term_size := os.exec('stty size') { - if term_size.exit_code == 0 { - term_cols := term_size.output.split(' ')[1].int() - if term_cols > 0 { - cols = term_cols - } - } - } - return '-'.repeat(cols) -} diff --git a/vlib/term/misc.v b/vlib/term/misc.v new file mode 100644 index 0000000000..b7cc3c8382 --- /dev/null +++ b/vlib/term/misc.v @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000000..a57d8b948d --- /dev/null +++ b/vlib/term/misc_js.v @@ -0,0 +1,6 @@ +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/misc_nix.v b/vlib/term/misc_nix.v new file mode 100644 index 0000000000..c72e914b92 --- /dev/null +++ b/vlib/term/misc_nix.v @@ -0,0 +1,20 @@ +module term + +#include + +struct C.winsize{ + pub: + ws_row int + ws_col int +} + +fn C.ioctl() int + +pub fn get_terminal_size() (int, int) { + // TODO: check for resize events + + mut w := C.winsize{} + C.ioctl(0, C.TIOCGWINSZ, &w) + + return w.ws_col, w.ws_row +} diff --git a/vlib/term/misc_windows.v b/vlib/term/misc_windows.v new file mode 100644 index 0000000000..e3c67ea906 --- /dev/null +++ b/vlib/term/misc_windows.v @@ -0,0 +1,6 @@ +module term + +pub fn get_terminal_size() (int, int) { + // TODO: find a way to get proper width&height of the terminal on windows, probably using Winapis + return 80, 25 +}