1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

term: add term.colorize/2, use it in the tests. Support ConEmuANSI = ON too.

This commit is contained in:
Delyan Angelov
2021-01-17 19:09:29 +02:00
parent 75af639721
commit 3ee78dc961
4 changed files with 28 additions and 17 deletions

View File

@@ -30,8 +30,7 @@ pub fn can_show_color_on_stderr() bool {
// If colors are not allowed, returns a given string.
pub fn ok_message(s string) string {
return if can_show_color_on_stdout() {
msg := ' $s '
green(msg)
green(' $s ')
} else {
s
}
@@ -41,8 +40,7 @@ pub fn ok_message(s string) string {
// If colors are not allowed, returns a given string.
pub fn fail_message(s string) string {
return if can_show_color_on_stdout() {
msg := ' $s '
inverse(bg_white(bold(red(msg))))
inverse(bg_white(bold(red(' $s '))))
} else {
s
}
@@ -58,6 +56,16 @@ pub fn warn_message(s string) string {
}
}
// 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
}
// h_divider returns a horizontal divider line with a dynamic width,
// that depends on the current terminal settings.
// If an empty string is passed in, print enough spaces to make a new line
@@ -109,6 +117,9 @@ fn supports_escape_sequences(fd int) bool {
return false
}
$if windows {
if os.getenv('ConEmuANSI') == 'ON' {
return true
}
// 4 is enable_virtual_terminal_processing
return (is_atty(fd) & 0x0004) > 0
} $else {