mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
term: header()
This commit is contained in:
parent
0510bc7744
commit
36e636743b
@ -6,7 +6,6 @@ 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 {
|
||||
@ -39,6 +38,21 @@ pub fn h_divider(divider string) string {
|
||||
return result[0..cols]
|
||||
}
|
||||
|
||||
// header returns a horizontal divider line with a centered text in the middle.
|
||||
// e.g: term.header('TEXT', '=')
|
||||
// =============== TEXT ===============
|
||||
pub fn header(text, divider string) string {
|
||||
if text.len == 0 {
|
||||
return h_divider(divider)
|
||||
}
|
||||
cols,_ := get_terminal_size()
|
||||
tlimit := if cols > text.len + 2 + 2 * divider.len { text.len } else { cols - 3 - 2 * divider.len }
|
||||
tlimit_alligned := if (tlimit % 2) != (cols % 2) { tlimit + 1 } else { tlimit }
|
||||
tstart := (cols - tlimit_alligned) / 2
|
||||
ln := divider.repeat(1 + cols / divider.len)[0..cols]
|
||||
return ln[0..tstart] + ' ' + text[0..tlimit] + ' ' + ln[tstart + tlimit + 2..cols]
|
||||
}
|
||||
|
||||
fn supports_escape_sequences(fd int) bool {
|
||||
$if windows {
|
||||
return (is_atty(fd) & 0x0004) > 0 && os.getenv('TERM') != 'dumb' // ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
||||
|
57
vlib/term/term_test.v
Normal file
57
vlib/term/term_test.v
Normal file
@ -0,0 +1,57 @@
|
||||
import term
|
||||
|
||||
fn test_get_terminal_size() {
|
||||
cols,_ := term.get_terminal_size()
|
||||
assert cols > 0
|
||||
}
|
||||
|
||||
fn test_h_divider() {
|
||||
divider := term.h_divider('-')
|
||||
assert divider.len > 0
|
||||
assert divider[0] == `-`
|
||||
assert divider[divider.len - 1] == `-`
|
||||
}
|
||||
|
||||
fn test_h_divider_multiple_characters() {
|
||||
xdivider := term.h_divider('abc')
|
||||
assert xdivider.len > 0
|
||||
assert xdivider.contains('abcabc')
|
||||
}
|
||||
|
||||
fn test_header() {
|
||||
divider := term.h_divider('-')
|
||||
term_width := divider.len
|
||||
assert term_width > 0
|
||||
empty_header := term.header('', '-')
|
||||
short_header := term.header('reasonable header', '-')
|
||||
very_long_header := term.header(['abc'].repeat(500).join(' '), '-')
|
||||
very_long_header_2 := term.header(['abcd'].repeat(500).join(' '), '-')
|
||||
/*
|
||||
eprintln(divider)
|
||||
eprintln(empty_header)
|
||||
eprintln(short_header)
|
||||
eprintln(term.header('another longer header', '_-/\\'))
|
||||
eprintln(term.header('another longer header', '-'))
|
||||
eprintln(term.header('short', '-'))
|
||||
eprintln(term.header('12345', '-'))
|
||||
eprintln(term.header('1234', '-'))
|
||||
eprintln(term.header('123', '-'))
|
||||
eprintln(term.header('12', '-'))
|
||||
eprintln(term.header('1', '-'))
|
||||
eprintln(very_long_header)
|
||||
eprintln(divider)
|
||||
eprintln(very_long_header_2)
|
||||
eprintln(term.header(['abcd'].repeat(500).join(' '), '_-/\\'))
|
||||
eprintln(term.header(['abcd'].repeat(500).join(' '), '_-//'))
|
||||
eprintln(term.header('1', '_-/\\\/'))
|
||||
eprintln(term.header('12', '_-/\\\/'))
|
||||
eprintln(term.header('123', '_-/\\\/'))
|
||||
eprintln(term.header('1234', '_-/\\/\\'))
|
||||
eprintln(term.header('', '-'))
|
||||
*/
|
||||
assert term_width == empty_header.len
|
||||
assert term_width == short_header.len
|
||||
assert term_width == very_long_header.len
|
||||
assert term_width == very_long_header_2.len
|
||||
assert term_width == term.header('1234', '_-/\\/\\').len
|
||||
}
|
Loading…
Reference in New Issue
Block a user