2020-01-29 07:12:12 +03:00
|
|
|
module term
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
const (
|
|
|
|
default_columns_size = 80
|
2020-10-15 13:32:28 +03:00
|
|
|
default_rows_size = 25
|
2020-01-29 07:12:12 +03:00
|
|
|
)
|
2020-09-08 22:00:10 +03:00
|
|
|
|
|
|
|
// Coord - used by term.get_cursor_position and term.set_cursor_position
|
|
|
|
pub struct Coord {
|
|
|
|
pub mut:
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2020-01-29 07:12:12 +03:00
|
|
|
// 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 {
|
2021-02-08 02:28:46 +03:00
|
|
|
return if can_show_color_on_stdout() { green(' $s ') } else { s }
|
2020-01-29 07:12:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-02-08 02:28:46 +03:00
|
|
|
return if can_show_color_on_stdout() { inverse(bg_white(bold(red(' $s ')))) } else { s }
|
2020-01-29 07:12:12 +03:00
|
|
|
}
|
|
|
|
|
2020-04-08 17:52:40 +03:00
|
|
|
// warn_message returns a colored string with yellow color.
|
|
|
|
// If colors are not allowed, returns a given string.
|
|
|
|
pub fn warn_message(s string) string {
|
2021-02-08 02:28:46 +03:00
|
|
|
return if can_show_color_on_stdout() { bright_yellow(' $s ') } else { s }
|
2020-04-08 17:52:40 +03:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:09:29 +03:00
|
|
|
// colorize returns a colored string by running the specified `cfn` over
|
|
|
|
// the message `s`, only if colored output is supported by the terminal.
|
|
|
|
// Example: term.colorize(term.yellow, 'the message')
|
|
|
|
pub fn colorize(cfn fn (string) string, s string) string {
|
|
|
|
if can_show_color_on_stdout() {
|
|
|
|
return cfn(s)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-01-29 07:12:12 +03:00
|
|
|
// h_divider returns a horizontal divider line with a dynamic width,
|
|
|
|
// that depends on the current terminal settings.
|
2020-03-23 22:05:08 +03:00
|
|
|
// If an empty string is passed in, print enough spaces to make a new line
|
2020-01-29 07:12:12 +03:00
|
|
|
pub fn h_divider(divider string) string {
|
2020-10-15 13:32:28 +03:00
|
|
|
cols, _ := get_terminal_size()
|
2021-01-23 11:33:22 +03:00
|
|
|
result := if divider.len > 0 {
|
|
|
|
divider.repeat(1 + (cols / divider.len))
|
|
|
|
} else {
|
|
|
|
' '.repeat(1 + cols)
|
|
|
|
}
|
2020-01-29 07:12:12 +03:00
|
|
|
return result[0..cols]
|
|
|
|
}
|
|
|
|
|
2020-02-07 20:46:20 +03:00
|
|
|
// header returns a horizontal divider line with a centered text in the middle.
|
|
|
|
// e.g: term.header('TEXT', '=')
|
|
|
|
// =============== TEXT ===============
|
2020-10-15 13:32:28 +03:00
|
|
|
pub fn header(text string, divider string) string {
|
2020-07-24 08:34:39 +03:00
|
|
|
if text.len == 0 {
|
2020-02-07 20:46:20 +03:00
|
|
|
return h_divider(divider)
|
|
|
|
}
|
2020-10-15 13:32:28 +03:00
|
|
|
xcols, _ := get_terminal_size()
|
|
|
|
cols := imax(1, xcols)
|
2021-01-23 11:33:22 +03:00
|
|
|
tlimit := imax(1, if cols > text.len + 2 + 2 * divider.len {
|
|
|
|
text.len
|
|
|
|
} else {
|
|
|
|
cols - 3 - 2 * divider.len
|
|
|
|
})
|
2020-02-07 20:46:20 +03:00
|
|
|
tlimit_alligned := if (tlimit % 2) != (cols % 2) { tlimit + 1 } else { tlimit }
|
2020-07-24 08:34:39 +03:00
|
|
|
tstart := imax(0, (cols - tlimit_alligned) / 2)
|
2021-01-23 11:33:22 +03:00
|
|
|
ln := if divider.len > 0 {
|
|
|
|
divider.repeat(1 + cols / divider.len)[0..cols]
|
|
|
|
} else {
|
|
|
|
' '.repeat(1 + cols)
|
|
|
|
}
|
2020-07-24 08:34:39 +03:00
|
|
|
if ln.len == 1 {
|
|
|
|
return ln + ' ' + text[0..tlimit] + ' ' + ln
|
|
|
|
}
|
2020-02-07 20:46:20 +03:00
|
|
|
return ln[0..tstart] + ' ' + text[0..tlimit] + ' ' + ln[tstart + tlimit + 2..cols]
|
|
|
|
}
|
|
|
|
|
2020-10-15 13:32:28 +03:00
|
|
|
fn imax(x int, y int) int {
|
2021-02-08 02:28:46 +03:00
|
|
|
return if x > y { x } else { y }
|
2020-07-24 08:34:39 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 07:12:12 +03:00
|
|
|
fn supports_escape_sequences(fd int) bool {
|
2020-05-08 19:04:24 +03:00
|
|
|
vcolors_override := os.getenv('VCOLORS')
|
|
|
|
if vcolors_override == 'always' {
|
|
|
|
return true
|
2020-05-22 18:36:09 +03:00
|
|
|
}
|
2020-07-24 08:34:39 +03:00
|
|
|
if vcolors_override == 'never' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if os.getenv('TERM') == 'dumb' {
|
|
|
|
return false
|
|
|
|
}
|
2020-01-29 07:12:12 +03:00
|
|
|
$if windows {
|
2021-01-17 20:09:29 +03:00
|
|
|
if os.getenv('ConEmuANSI') == 'ON' {
|
|
|
|
return true
|
|
|
|
}
|
2020-07-24 08:34:39 +03:00
|
|
|
// 4 is enable_virtual_terminal_processing
|
|
|
|
return (is_atty(fd) & 0x0004) > 0
|
2020-01-29 07:12:12 +03:00
|
|
|
} $else {
|
2020-07-24 08:34:39 +03:00
|
|
|
return is_atty(fd) > 0
|
2020-01-29 07:12:12 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 18:15:41 +03:00
|
|
|
|
|
|
|
// clear clears current terminal screen.
|
|
|
|
pub fn clear() {
|
|
|
|
$if !windows {
|
2020-12-15 10:11:17 +03:00
|
|
|
print('\x1b[2J')
|
|
|
|
print('\x1b[H')
|
2020-06-19 18:15:41 +03:00
|
|
|
}
|
|
|
|
}
|