2020-01-29 05:12:12 +01:00
|
|
|
module term
|
|
|
|
|
|
|
|
import os
|
2021-05-20 10:24:40 +02:00
|
|
|
import strings.textscanner
|
2020-01-29 05:12:12 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
default_columns_size = 80
|
2020-10-15 12:32:28 +02:00
|
|
|
default_rows_size = 25
|
2020-01-29 05:12:12 +01:00
|
|
|
)
|
2020-09-08 21:00:10 +02: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 05:12:12 +01: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)
|
|
|
|
}
|
|
|
|
|
2021-07-15 08:52:22 +02:00
|
|
|
// failed returns a bold white on red version of the string `s`
|
|
|
|
// If colors are not allowed, returns the string `s`
|
|
|
|
pub fn failed(s string) string {
|
|
|
|
if can_show_color_on_stdout() {
|
|
|
|
return bg_red(bold(white(s)))
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-01-29 05:12:12 +01:00
|
|
|
// 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-03-22 15:45:29 +01:00
|
|
|
if can_show_color_on_stdout() {
|
|
|
|
return green(' $s ')
|
|
|
|
}
|
|
|
|
return s
|
2020-01-29 05:12:12 +01: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-07-15 08:52:22 +02:00
|
|
|
return failed(' $s ')
|
2020-01-29 05:12:12 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:52:40 +02: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-03-22 15:45:29 +01:00
|
|
|
if can_show_color_on_stdout() {
|
|
|
|
return bright_yellow(' $s ')
|
|
|
|
}
|
|
|
|
return s
|
2020-04-08 16:52:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-17 18:09:29 +01:00
|
|
|
// colorize returns a colored string by running the specified `cfn` over
|
2022-03-04 11:28:11 +01:00
|
|
|
// the message `s`, only if colored stdout is supported by the terminal.
|
2021-01-17 18:09:29 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:28:11 +01:00
|
|
|
// ecolorize returns a colored string by running the specified `cfn` over
|
|
|
|
// the message `s`, only if colored stderr is supported by the terminal.
|
|
|
|
// Example: term.ecolorize(term.bright_red, 'the message')
|
|
|
|
pub fn ecolorize(cfn fn (string) string, s string) string {
|
|
|
|
if can_show_color_on_stderr() {
|
|
|
|
return cfn(s)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-05-20 09:02:38 +02:00
|
|
|
// strip_ansi removes any ANSI sequences in the `text`
|
|
|
|
pub fn strip_ansi(text string) string {
|
|
|
|
// This is a port of https://github.com/kilobyte/colorized-logs/blob/master/ansi2txt.c
|
|
|
|
// \e, [, 1, m, a, b, c, \e, [, 2, 2, m => abc
|
2021-05-20 10:24:40 +02:00
|
|
|
mut input := textscanner.new(text)
|
2022-04-15 13:45:52 +02:00
|
|
|
mut output := []u8{cap: text.len}
|
2021-05-20 09:02:38 +02:00
|
|
|
mut ch := 0
|
|
|
|
for ch != -1 {
|
|
|
|
ch = input.next()
|
|
|
|
if ch == 27 {
|
|
|
|
ch = input.next()
|
|
|
|
if ch == `[` {
|
|
|
|
for {
|
|
|
|
ch = input.next()
|
|
|
|
if ch in [`;`, `?`] || (ch >= `0` && ch <= `9`) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else if ch == `]` {
|
|
|
|
ch = input.next()
|
|
|
|
if ch >= `0` && ch <= `9` {
|
|
|
|
for {
|
|
|
|
ch = input.next()
|
|
|
|
if ch == -1 || ch == 7 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if ch == 27 {
|
|
|
|
ch = input.next()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ch == `%` {
|
|
|
|
ch = input.next()
|
|
|
|
}
|
|
|
|
} else if ch != -1 {
|
2022-04-15 13:45:52 +02:00
|
|
|
output << u8(ch)
|
2021-05-20 09:02:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output.bytestr()
|
|
|
|
}
|
|
|
|
|
2020-01-29 05:12:12 +01:00
|
|
|
// h_divider returns a horizontal divider line with a dynamic width,
|
|
|
|
// that depends on the current terminal settings.
|
2020-03-23 20:05:08 +01:00
|
|
|
// If an empty string is passed in, print enough spaces to make a new line
|
2020-01-29 05:12:12 +01:00
|
|
|
pub fn h_divider(divider string) string {
|
2020-10-15 12:32:28 +02:00
|
|
|
cols, _ := get_terminal_size()
|
2021-03-22 15:45:29 +01:00
|
|
|
mut result := ''
|
|
|
|
if divider.len > 0 {
|
|
|
|
result = divider.repeat(1 + (cols / divider.len))
|
2021-01-23 09:33:22 +01:00
|
|
|
} else {
|
2021-03-22 15:45:29 +01:00
|
|
|
result = ' '.repeat(1 + cols)
|
2021-01-23 09:33:22 +01:00
|
|
|
}
|
2020-01-29 05:12:12 +01:00
|
|
|
return result[0..cols]
|
|
|
|
}
|
|
|
|
|
2021-05-20 09:02:38 +02:00
|
|
|
// header_left returns a horizontal divider line with a title text on the left.
|
|
|
|
// e.g: term.header_left('TITLE', '=')
|
|
|
|
// ==== TITLE =========================
|
|
|
|
pub fn header_left(text string, divider string) string {
|
|
|
|
plain_text := strip_ansi(text)
|
2022-04-09 12:03:52 +02:00
|
|
|
xcols, _ := get_terminal_size() // can get 0 in lldb/gdb
|
2021-05-20 09:02:38 +02:00
|
|
|
cols := imax(1, xcols)
|
|
|
|
relement := if divider.len > 0 { divider } else { ' ' }
|
|
|
|
hstart := relement.repeat(4)[0..4]
|
2022-04-09 12:03:52 +02:00
|
|
|
remaining_cols := imax(0, (cols - (hstart.len + 1 + plain_text.len + 1)))
|
2021-05-20 09:02:38 +02:00
|
|
|
hend := relement.repeat((remaining_cols + 1) / relement.len)[0..remaining_cols]
|
|
|
|
return '$hstart $text $hend'
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:46:20 +01:00
|
|
|
// header returns a horizontal divider line with a centered text in the middle.
|
|
|
|
// e.g: term.header('TEXT', '=')
|
|
|
|
// =============== TEXT ===============
|
2020-10-15 12:32:28 +02:00
|
|
|
pub fn header(text string, divider string) string {
|
2020-07-24 07:34:39 +02:00
|
|
|
if text.len == 0 {
|
2020-02-07 18:46:20 +01:00
|
|
|
return h_divider(divider)
|
|
|
|
}
|
2020-10-15 12:32:28 +02:00
|
|
|
xcols, _ := get_terminal_size()
|
|
|
|
cols := imax(1, xcols)
|
2021-01-23 09:33:22 +01:00
|
|
|
tlimit := imax(1, if cols > text.len + 2 + 2 * divider.len {
|
|
|
|
text.len
|
|
|
|
} else {
|
|
|
|
cols - 3 - 2 * divider.len
|
|
|
|
})
|
2020-02-07 18:46:20 +01:00
|
|
|
tlimit_alligned := if (tlimit % 2) != (cols % 2) { tlimit + 1 } else { tlimit }
|
2020-07-24 07:34:39 +02:00
|
|
|
tstart := imax(0, (cols - tlimit_alligned) / 2)
|
2021-03-22 15:45:29 +01:00
|
|
|
mut ln := ''
|
|
|
|
if divider.len > 0 {
|
|
|
|
ln = divider.repeat(1 + cols / divider.len)[0..cols]
|
2021-01-23 09:33:22 +01:00
|
|
|
} else {
|
2021-03-22 15:45:29 +01:00
|
|
|
ln = ' '.repeat(1 + cols)
|
2021-01-23 09:33:22 +01:00
|
|
|
}
|
2020-07-24 07:34:39 +02:00
|
|
|
if ln.len == 1 {
|
|
|
|
return ln + ' ' + text[0..tlimit] + ' ' + ln
|
|
|
|
}
|
2020-02-07 18:46:20 +01:00
|
|
|
return ln[0..tstart] + ' ' + text[0..tlimit] + ' ' + ln[tstart + tlimit + 2..cols]
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:32:28 +02:00
|
|
|
fn imax(x int, y int) int {
|
2021-02-08 00:28:46 +01:00
|
|
|
return if x > y { x } else { y }
|
2020-07-24 07:34:39 +02:00
|
|
|
}
|
|
|
|
|
2021-11-17 19:39:08 +01:00
|
|
|
[manualfree]
|
2020-01-29 05:12:12 +01:00
|
|
|
fn supports_escape_sequences(fd int) bool {
|
2020-05-08 18:04:24 +02:00
|
|
|
vcolors_override := os.getenv('VCOLORS')
|
2021-11-17 19:39:08 +01:00
|
|
|
defer {
|
|
|
|
unsafe { vcolors_override.free() }
|
|
|
|
}
|
2020-05-08 18:04:24 +02:00
|
|
|
if vcolors_override == 'always' {
|
|
|
|
return true
|
2020-05-22 17:36:09 +02:00
|
|
|
}
|
2020-07-24 07:34:39 +02:00
|
|
|
if vcolors_override == 'never' {
|
|
|
|
return false
|
|
|
|
}
|
2021-08-13 17:37:34 +02:00
|
|
|
env_term := os.getenv('TERM')
|
2021-11-17 19:39:08 +01:00
|
|
|
defer {
|
|
|
|
unsafe { env_term.free() }
|
|
|
|
}
|
2021-08-13 17:37:34 +02:00
|
|
|
if env_term == 'dumb' {
|
2020-07-24 07:34:39 +02:00
|
|
|
return false
|
|
|
|
}
|
2020-01-29 05:12:12 +01:00
|
|
|
$if windows {
|
2021-08-13 17:37:34 +02:00
|
|
|
env_conemu := os.getenv('ConEmuANSI')
|
2021-11-17 19:39:08 +01:00
|
|
|
defer {
|
|
|
|
unsafe { env_conemu.free() }
|
|
|
|
}
|
2021-08-13 17:37:34 +02:00
|
|
|
if env_conemu == 'ON' {
|
2021-01-17 18:09:29 +01:00
|
|
|
return true
|
|
|
|
}
|
2020-07-24 07:34:39 +02:00
|
|
|
// 4 is enable_virtual_terminal_processing
|
2021-04-14 07:50:50 +02:00
|
|
|
return (os.is_atty(fd) & 0x0004) > 0
|
2020-01-29 05:12:12 +01:00
|
|
|
} $else {
|
2021-04-14 07:50:50 +02:00
|
|
|
return os.is_atty(fd) > 0
|
2020-01-29 05:12:12 +01:00
|
|
|
}
|
|
|
|
}
|