From 12ecd0f3dc54084a9c4ca5ae27f9a6262e707bb6 Mon Sep 17 00:00:00 2001 From: Felix Scholz <4zure.contact@gmail.com> Date: Wed, 31 Jul 2019 19:22:38 +0200 Subject: [PATCH] term: allow more colors to be used (bright styled ansi, rgb and hex) * introduce bright versions of standard ansi colors * allow developer to colorize messages with rgb * allow developer to colorize messages using hex * fix broken formatting --- vlib/term/colors.v | 94 +++++++++++++++++++++++++++++++++++++++++- vlib/term/colors_nix.v | 4 ++ vlib/term/colors_win.v | 5 +++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/vlib/term/colors.v b/vlib/term/colors.v index 8ccf81edbf..e5ff44d894 100644 --- a/vlib/term/colors.v +++ b/vlib/term/colors.v @@ -10,46 +10,114 @@ fn _format(msg, open, close string) string { return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm' } +fn _format_rgb(r, g, b int, msg, open, close string) string { + return '\x1b[' + open + ';2;' + r.str() + ';' + g.str() + ';' + b.str() + 'm' + msg + '\x1b[' + close + 'm' +} + +pub fn rgb(r, g, b int, msg string) string { + return format_rgb(r, g, b, msg, '38', '39') +} + +pub fn bg_rgb(r, g, b int, msg string) string { + return format_rgb(r, g, b, msg, '48', '49') +} + +pub fn hex(hex int, msg string) string { + return format_rgb( + hex >> 16, + hex >> 8 & 0xFF, + hex & 0xFF, + msg, '38', '39') +} + +pub fn bg_hex(hex int, msg string) string { + return format_rgb( + hex >> 16, + hex >> 8 & 0xFF, + hex & 0xFF, + msg, '48', '49') +} + pub fn bg_black(msg string) string { return format(msg, '40', '49') } +pub fn bright_bg_black(msg string) string { + return format(msg, '100', '49') +} + pub fn bg_blue(msg string) string { return format(msg, '44', '49') } +pub fn bright_bg_blue(msg string) string { + return format(msg, '104', '49') +} + pub fn bg_cyan(msg string) string { return format(msg, '46', '49') } +pub fn bright_bg_cyan(msg string) string { + return format(msg, '106', '49') +} + pub fn bg_green(msg string) string { return format(msg, '42', '49') } +pub fn bright_bg_green(msg string) string { + return format(msg, '102', '49') +} + pub fn bg_magenta(msg string) string { return format(msg, '45', '49') } +pub fn bright_bg_magenta(msg string) string { + return format(msg, '105', '49') +} + pub fn bg_red(msg string) string { return format(msg, '41', '49') } +pub fn bright_bg_red(msg string) string { + return format(msg, '101', '49') +} + pub fn bg_white(msg string) string { return format(msg, '47', '49') } +pub fn bright_bg_white(msg string) string { + return format(msg, '107', '49') +} + pub fn bg_yellow(msg string) string { return format(msg, '43', '49') } +pub fn bright_bg_yellow(msg string) string { + return format(msg, '103', '49') +} + pub fn black(msg string) string { return format(msg, '30', '39') } +pub fn bright_black(msg string) string { + return format(msg, '90', '39') +} + pub fn blue(msg string) string { return format(msg, '34', '39') } +pub fn bright_blue(msg string) string { + return format(msg, '94', '39') +} + pub fn bold(msg string) string { return format(msg, '1', '22') } @@ -58,6 +126,10 @@ pub fn cyan(msg string) string { return format(msg, '36', '39') } +pub fn bright_cyan(msg string) string { + return format(msg, '96', '39') +} + pub fn dim(msg string) string { return format(msg, '2', '22') } @@ -66,8 +138,12 @@ pub fn green(msg string) string { return format(msg, '32', '39') } +pub fn bright_green(msg string) string { + return format(msg, '92', '39') +} + pub fn gray(msg string) string { - return format(msg, '90', '39') + return bright_black(msg) } pub fn hidden(msg string) string { @@ -86,6 +162,10 @@ pub fn magenta(msg string) string { return format(msg, '35', '39') } +pub fn bright_magenta(msg string) string { + return format(msg, '95', '39') +} + pub fn reset(msg string) string { return format(msg, '0', '0') } @@ -94,6 +174,10 @@ pub fn red(msg string) string { return format(msg, '31', '39') } +pub fn bright_red(msg string) string { + return format(msg, '91', '39') +} + pub fn strikethrough(msg string) string { return format(msg, '9', '29') } @@ -106,6 +190,14 @@ pub fn white(msg string) string { return format(msg, '37', '39') } +pub fn bright_white(msg string) string { + return format(msg, '97', '39') +} + pub fn yellow(msg string) string { return format(msg, '33', '39') } + +pub fn bright_yellow(msg string) string { + return format(msg, '93', '39') +} \ No newline at end of file diff --git a/vlib/term/colors_nix.v b/vlib/term/colors_nix.v index 30e6bafb2a..ce77340f24 100644 --- a/vlib/term/colors_nix.v +++ b/vlib/term/colors_nix.v @@ -7,3 +7,7 @@ module term pub fn format(msg, open, close string) string { return _format(msg, open, close) } + +pub fn format_rgb(r, g, b int, msg, open, close string) string { + return _format_rgb(r, g, b, msg, open, close) +} \ No newline at end of file diff --git a/vlib/term/colors_win.v b/vlib/term/colors_win.v index c26c38ab63..4171c57604 100644 --- a/vlib/term/colors_win.v +++ b/vlib/term/colors_win.v @@ -9,3 +9,8 @@ import os pub fn format(msg, open, close string) string { return _format(msg, open, close) } + +pub fn format_rgb(r, g, b int, msg, open, close string) string { + enable_term_color_win() + return _format_rgb(r, g, b, msg, open, close) +}