From 91a01bdcbd380e750bf50127f07f76c029f4310e Mon Sep 17 00:00:00 2001 From: RGBCube Date: Fri, 18 Nov 2022 20:34:24 +0300 Subject: [PATCH] Add utility methods and refactor stuff --- builder_error1.v | 4 ++-- builder_error2.v | 2 +- color/basic_color.v | 49 ++++++++++++++++++++++++++------------------- color/color.v | 27 ++++++++++++++++++++----- color/constants.v | 37 ++++++++++++++++++++++++++++++++++ color/paintbrush.v | 18 +++++++++++------ color/style.v | 13 ------------ color/true_color.v | 38 +++++++++++++++++++++++++---------- 8 files changed, 129 insertions(+), 59 deletions(-) create mode 100644 color/constants.v diff --git a/builder_error1.v b/builder_error1.v index cc39d8b..dcd2a46 100644 --- a/builder_error1.v +++ b/builder_error1.v @@ -3,6 +3,6 @@ module main import color fn main() { - println(color.red.color('Hello World')) - println(color.bold.color('Hello World')) + color.red.cprintln('Hello World') + color.bold.cprintln('Hello World') } diff --git a/builder_error2.v b/builder_error2.v index 96697e3..37ea936 100644 --- a/builder_error2.v +++ b/builder_error2.v @@ -9,5 +9,5 @@ fn main() { styles: [color.bold, color.underline, color.italic] } - print(p.color('Hello World')) + p.cprintln('Hello World') } diff --git a/color/basic_color.v b/color/basic_color.v index 250ea7b..ae8161a 100644 --- a/color/basic_color.v +++ b/color/basic_color.v @@ -2,25 +2,6 @@ module color import term -pub const ( - black = BasicColor.black - red = BasicColor.red - green = BasicColor.green - yellow = BasicColor.yellow - blue = BasicColor.blue - magenta = BasicColor.magenta - cyan = BasicColor.cyan - white = BasicColor.white - bright_black = BasicColor.bright_black - bright_red = BasicColor.bright_red - bright_green = BasicColor.bright_green - bright_yellow = BasicColor.bright_yellow - bright_blue = BasicColor.bright_blue - bright_magenta = BasicColor.bright_magenta - bright_cyan = BasicColor.bright_cyan - bright_white = BasicColor.bright_white -) - enum BasicColor { black red @@ -40,7 +21,11 @@ enum BasicColor { bright_white } -pub fn (c BasicColor) color(msg string) string { +pub fn (c BasicColor) render(msg string) string { + if no_color { + return msg + } + func := match c { .black { term.black } .red { term.red } @@ -59,10 +44,15 @@ pub fn (c BasicColor) color(msg string) string { .bright_cyan { term.bright_cyan } .bright_white { term.bright_white } } + return func(msg) } -pub fn (c BasicColor) color_bg(msg string) string { +pub fn (c BasicColor) render_bg(msg string) string { + if no_color { + return msg + } + func := match c { .black { term.bg_black } .red { term.bg_red } @@ -81,5 +71,22 @@ pub fn (c BasicColor) color_bg(msg string) string { .bright_cyan { term.bright_bg_cyan } .bright_white { term.bright_bg_white } } + return func(msg) } + +pub fn (c BasicColor) cprint(msg string) { + print(c.render(msg)) +} + +pub fn (c BasicColor) cprintln(msg string) { + println(c.render(msg)) +} + +pub fn (c BasicColor) cprint_bg(msg string) { + print(c.render_bg(msg)) +} + +pub fn (c BasicColor) cprintln_bg(msg string) { + println(c.render_bg(msg)) +} diff --git a/color/color.v b/color/color.v index b5fc76c..dca9f51 100644 --- a/color/color.v +++ b/color/color.v @@ -2,11 +2,28 @@ module color type Color = BasicColor | TrueColor -// I have no idea why this is needed -fn (c Color) color(msg string) string { - return c.color(msg) +// I have no idea why these are needed + +fn (c Color) render(msg string) string { + return c.render(msg) } -fn (c Color) color_bg(msg string) string { - return c.color_bg(msg) +fn (c Color) render_bg(msg string) string { + return c.render_bg(msg) +} + +fn (c Color) cprint(msg string) string { + return c.cprint(msg) +} + +fn (c Color) cprintln(msg string) string { + return c.cprintln(msg) +} + +fn (c Color) cprintln_bg(msg string) string { + return c.cprintln_bg(msg) +} + +fn (c Color) cprint_bg(msg string) string { + return c.cprint_bg(msg) } diff --git a/color/constants.v b/color/constants.v new file mode 100644 index 0000000..2e830c7 --- /dev/null +++ b/color/constants.v @@ -0,0 +1,37 @@ +module color + +import term + +pub const ( + black = BasicColor.black + red = BasicColor.red + green = BasicColor.green + yellow = BasicColor.yellow + blue = BasicColor.blue + magenta = BasicColor.magenta + cyan = BasicColor.cyan + white = BasicColor.white + bright_black = BasicColor.bright_black + bright_red = BasicColor.bright_red + bright_green = BasicColor.bright_green + bright_yellow = BasicColor.bright_yellow + bright_blue = BasicColor.bright_blue + bright_magenta = BasicColor.bright_magenta + bright_cyan = BasicColor.bright_cyan + bright_white = BasicColor.bright_white +) + +pub const ( + reset = Style.reset + bold = Style.bold + dim = Style.dim + italic = Style.italic + underline = Style.underline + slow_blink = Style.slow_blink + rapid_blink = Style.rapid_blink + inverse = Style.inverse + hidden = Style.hidden + strikethrough = Style.strikethrough +) + +const no_color = !term.can_show_color_on_stdout() diff --git a/color/paintbrush.v b/color/paintbrush.v index 9ef29e5..6138fac 100644 --- a/color/paintbrush.v +++ b/color/paintbrush.v @@ -2,8 +2,6 @@ module color import term -const can_show_color = term.can_show_color_on_stdout() - pub struct PaintBrush { pub: fg ?Color @@ -11,18 +9,18 @@ pub: styles []Style } -pub fn (p &PaintBrush) color(msg string) string { - if !color.can_show_color { +pub fn (p &PaintBrush) render(msg string) string { + if no_color { return msg } mut result := msg if fg := p.fg { - result = fg.color(result) + result = fg.render(result) } if bg := p.bg { - result = bg.color(result) + result = bg.render(result) } for style in p.styles { @@ -31,3 +29,11 @@ pub fn (p &PaintBrush) color(msg string) string { return result } + +pub fn (p &PaintBrush) cprint(msg string) { + print(p.render(msg)) +} + +pub fn (p &PaintBrush) cprintln(msg string) { + println(p.render(msg)) +} diff --git a/color/style.v b/color/style.v index 432d281..97f4067 100644 --- a/color/style.v +++ b/color/style.v @@ -2,19 +2,6 @@ module color import term -pub const ( - reset = Style.reset - bold = Style.bold - dim = Style.dim - italic = Style.italic - underline = Style.underline - slow_blink = Style.slow_blink - rapid_blink = Style.rapid_blink - inverse = Style.inverse - hidden = Style.hidden - strikethrough = Style.strikethrough -) - enum Style { reset bold diff --git a/color/true_color.v b/color/true_color.v index f0749b9..cec6af8 100644 --- a/color/true_color.v +++ b/color/true_color.v @@ -2,7 +2,14 @@ module color import term -pub fn rgb(r int, g int, b int) !TrueColor { +struct TrueColor { +pub: + r int + g int + b int +} + +pub fn rgb(r int, g int, b int) !Color { if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 { return error('Red, green and blue must each be between 0 and 255') } @@ -13,22 +20,31 @@ pub fn rgb(r int, g int, b int) !TrueColor { } } -pub fn hex(hex int) !TrueColor { +pub fn hex(hex int) !Color { return rgb(hex >> 16, hex >> 8 & 0xFF, hex & 0xFF)! } -[noinit] -struct TrueColor { -pub: - r int - g int - b int -} -pub fn (c TrueColor) color(msg string) string { +pub fn (c TrueColor) render(msg string) string { return term.rgb(c.r, c.g, c.b, msg) } -pub fn (c TrueColor) color_bg(msg string) string { +pub fn (c TrueColor) render_bg(msg string) string { return term.bg_rgb(c.r, c.g, c.b, msg) } + +pub fn (c TrueColor) cprint(msg string) { + print(c.render(msg)) +} + +pub fn (c TrueColor) cprintln(msg string) { + println(c.render(msg)) +} + +pub fn (c TrueColor) cprint_bg(msg string) { + print(c.render_bg(msg)) +} + +pub fn (c TrueColor) cprintln_bg(msg string) { + println(c.render_bg(msg)) +}