From dceda93727a083b0cfbac143e5639b25b79b0809 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sun, 20 Nov 2022 19:58:32 +0300 Subject: [PATCH] Use interfaces --- color/basic_color.v | 8 ++++++++ color/color.v | 27 +++++++++------------------ color/constants.v | 20 ++++++++++---------- color/style.v | 8 -------- color/true_color.v | 8 ++++++++ 5 files changed, 35 insertions(+), 36 deletions(-) diff --git a/color/basic_color.v b/color/basic_color.v index 4706f2b..baacad5 100644 --- a/color/basic_color.v +++ b/color/basic_color.v @@ -22,6 +22,10 @@ enum BasicColor { } fn (c BasicColor) render(msg string) string { + if no_color { + return msg + } + func := match c { .black { term.black } .red { term.red } @@ -45,6 +49,10 @@ fn (c BasicColor) render(msg string) string { } fn (c BasicColor) render_bg(msg string) string { + if no_color { + return msg + } + func := match c { .black { term.bg_black } .red { term.bg_red } diff --git a/color/color.v b/color/color.v index 1d5643e..29e5ad3 100644 --- a/color/color.v +++ b/color/color.v @@ -1,29 +1,20 @@ module color -pub type Color = BasicColor | TrueColor - -pub fn (c Color) render(msg string) string { - if no_color { - return msg - } - - return c.render(msg) +pub interface Renderable { + render(string) string } -pub fn (c Color) render_bg(msg string) string { - if no_color { - return msg - } - - return c.render_bg(msg) +pub fn (r Renderable) cprint(msg string) { + print(r.render(msg)) } -pub fn (c Color) cprint(msg string) { - print(c.render(msg)) +pub fn (r Renderable) cprintln(msg string) { + println(r.render(msg)) } -pub fn (c Color) cprintln(msg string) { - println(c.render(msg)) +pub interface Color { + Renderable + render_bg(string) string } pub fn (c Color) cprint_bg(msg string) { diff --git a/color/constants.v b/color/constants.v index b52ed7a..ca6d31d 100644 --- a/color/constants.v +++ b/color/constants.v @@ -21,16 +21,16 @@ pub const ( bright_cyan = Color(BasicColor.bright_cyan) bright_white = Color(BasicColor.bright_white) // Styles - 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 + reset = Renderable(Style.reset) + bold = Renderable(Style.bold) + dim = Renderable(Style.dim) + italic = Renderable(Style.italic) + underline = Renderable(Style.underline) + slow_blink = Renderable(Style.slow_blink) + rapid_blink = Renderable(Style.rapid_blink) + inverse = Renderable(Style.inverse) + hidden = Renderable(Style.hidden) + strikethrough = Renderable(Style.strikethrough) ) const no_color = !term.can_show_color_on_stdout() diff --git a/color/style.v b/color/style.v index bd31820..b29c9c7 100644 --- a/color/style.v +++ b/color/style.v @@ -35,11 +35,3 @@ pub fn (s Style) render(msg string) string { return func(msg) } - -pub fn (s Style) cprint(msg string) { - print(s.render(msg)) -} - -pub fn (s Style) cprintln(msg string) { - println(s.render(msg)) -} diff --git a/color/true_color.v b/color/true_color.v index d86da33..487b50b 100644 --- a/color/true_color.v +++ b/color/true_color.v @@ -21,9 +21,17 @@ struct TrueColor { } fn (c TrueColor) render(msg string) string { + if no_color { + return msg + } + return term.rgb(c.r, c.g, c.b, msg) } fn (c TrueColor) render_bg(msg string) string { + if no_color { + return msg + } + return term.bg_rgb(c.r, c.g, c.b, msg) }