term: get_terminal_size()

pull/3586/head
Mateo Pidal 2020-01-28 01:18:19 -03:00 committed by Alexander Medvednikov
parent 08d3401092
commit 78c96fe989
7 changed files with 50 additions and 18 deletions

View File

@ -95,7 +95,7 @@ pub fn (ts mut TestSession) test() {
} }
ts.waitgroup.wait() ts.waitgroup.wait()
ts.benchmark.stop() 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}')) eprintln(bmark.step_message_ok('command: ${cmd}'))
} }
bmark.stop() bmark.stop()
eprintln(term.h_divider()) eprintln(term.h_divider('-'))
eprintln(bmark.total_message('building v binaries')) eprintln(bmark.total_message('building v binaries'))
return failed return failed
} }

View File

@ -36,7 +36,7 @@ enum Action {
fn C.tcgetattr() int fn C.tcgetattr() int
fn C.tcsetattr() int fn C.tcsetattr() int
fn C.ioctl() int //fn C.ioctl() int
fn C.raise() fn C.raise()
// Toggle raw mode of the terminal by changing its attributes // Toggle raw mode of the terminal by changing its attributes

View File

@ -26,18 +26,3 @@ pub fn ok_message(s string) string {
pub fn fail_message(s string) string { pub fn fail_message(s string) string {
return if can_show_color_on_stdout() { red(s) } else { s } 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)
}

15
vlib/term/misc.v 100644
View File

@ -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]
}

View File

@ -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
}

View File

@ -0,0 +1,20 @@
module term
#include <sys/ioctl.h>
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
}

View File

@ -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
}