term: fix panic in term.header, when cols,rows from get_terminal_size are < 2

pull/5965/head
Delyan Angelov 2020-07-24 08:34:39 +03:00
parent c2083203b3
commit 346cc5ca2c
1 changed files with 21 additions and 10 deletions

View File

@ -49,30 +49,41 @@ pub fn h_divider(divider string) string {
// e.g: term.header('TEXT', '=') // e.g: term.header('TEXT', '=')
// =============== TEXT =============== // =============== TEXT ===============
pub fn header(text, divider string) string { pub fn header(text, divider string) string {
if text.len == 0 || divider.len < 2 { if text.len == 0 {
return h_divider(divider) return h_divider(divider)
} }
cols,_ := get_terminal_size() xcols,_ := get_terminal_size()
tlimit := if cols > text.len + 2 + 2 * divider.len { text.len } else { cols - 3 - 2 * divider.len } cols := imax(1, xcols)
tlimit := imax(1, 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 } tlimit_alligned := if (tlimit % 2) != (cols % 2) { tlimit + 1 } else { tlimit }
tstart := (cols - tlimit_alligned) / 2 tstart := imax(0, (cols - tlimit_alligned) / 2)
ln := if divider.len > 0 { divider.repeat(1 + cols / divider.len)[0..cols] } else { " ".repeat(1 + cols) } ln := if divider.len > 0 { divider.repeat(1 + cols / divider.len)[0..cols] } else { " ".repeat(1 + cols) }
if ln.len == 1 {
return ln + ' ' + text[0..tlimit] + ' ' + ln
}
return ln[0..tstart] + ' ' + text[0..tlimit] + ' ' + ln[tstart + tlimit + 2..cols] return ln[0..tstart] + ' ' + text[0..tlimit] + ' ' + ln[tstart + tlimit + 2..cols]
} }
fn supports_escape_sequences(fd int) bool { fn imax(x,y int) int {
//println('TERM=' + os.getenv('TERM')) return if x > y { x } else { y }
if os.getenv('TERM') == 'dumb' {
return false
} }
fn supports_escape_sequences(fd int) bool {
vcolors_override := os.getenv('VCOLORS') vcolors_override := os.getenv('VCOLORS')
if vcolors_override == 'always' { if vcolors_override == 'always' {
return true return true
} }
if vcolors_override == 'never' {
return false
}
if os.getenv('TERM') == 'dumb' {
return false
}
$if windows { $if windows {
return (is_atty(fd) & 0x0004) > 0 && os.getenv('TERM') != 'dumb' // enable_virtual_terminal_processing // 4 is enable_virtual_terminal_processing
return (is_atty(fd) & 0x0004) > 0
} $else { } $else {
return is_atty(fd) > 0 && os.getenv('TERM') != 'dumb' return is_atty(fd) > 0
} }
} }