1
Fork 0
mirror of https://github.com/RGBCube/color.v synced 2025-08-01 10:27:45 +00:00

Use interfaces

This commit is contained in:
RGBCube 2022-11-20 19:58:32 +03:00
parent cfe0f2a2c3
commit dceda93727
5 changed files with 35 additions and 36 deletions

View file

@ -22,6 +22,10 @@ enum BasicColor {
} }
fn (c BasicColor) render(msg string) string { fn (c BasicColor) render(msg string) string {
if no_color {
return msg
}
func := match c { func := match c {
.black { term.black } .black { term.black }
.red { term.red } .red { term.red }
@ -45,6 +49,10 @@ fn (c BasicColor) render(msg string) string {
} }
fn (c BasicColor) render_bg(msg string) string { fn (c BasicColor) render_bg(msg string) string {
if no_color {
return msg
}
func := match c { func := match c {
.black { term.bg_black } .black { term.bg_black }
.red { term.bg_red } .red { term.bg_red }

View file

@ -1,29 +1,20 @@
module color module color
pub type Color = BasicColor | TrueColor pub interface Renderable {
render(string) string
pub fn (c Color) render(msg string) string {
if no_color {
return msg
}
return c.render(msg)
} }
pub fn (c Color) render_bg(msg string) string { pub fn (r Renderable) cprint(msg string) {
if no_color { print(r.render(msg))
return msg
}
return c.render_bg(msg)
} }
pub fn (c Color) cprint(msg string) { pub fn (r Renderable) cprintln(msg string) {
print(c.render(msg)) println(r.render(msg))
} }
pub fn (c Color) cprintln(msg string) { pub interface Color {
println(c.render(msg)) Renderable
render_bg(string) string
} }
pub fn (c Color) cprint_bg(msg string) { pub fn (c Color) cprint_bg(msg string) {

View file

@ -21,16 +21,16 @@ pub const (
bright_cyan = Color(BasicColor.bright_cyan) bright_cyan = Color(BasicColor.bright_cyan)
bright_white = Color(BasicColor.bright_white) bright_white = Color(BasicColor.bright_white)
// Styles // Styles
reset = Style.reset reset = Renderable(Style.reset)
bold = Style.bold bold = Renderable(Style.bold)
dim = Style.dim dim = Renderable(Style.dim)
italic = Style.italic italic = Renderable(Style.italic)
underline = Style.underline underline = Renderable(Style.underline)
slow_blink = Style.slow_blink slow_blink = Renderable(Style.slow_blink)
rapid_blink = Style.rapid_blink rapid_blink = Renderable(Style.rapid_blink)
inverse = Style.inverse inverse = Renderable(Style.inverse)
hidden = Style.hidden hidden = Renderable(Style.hidden)
strikethrough = Style.strikethrough strikethrough = Renderable(Style.strikethrough)
) )
const no_color = !term.can_show_color_on_stdout() const no_color = !term.can_show_color_on_stdout()

View file

@ -35,11 +35,3 @@ pub fn (s Style) render(msg string) string {
return func(msg) 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))
}

View file

@ -21,9 +21,17 @@ struct TrueColor {
} }
fn (c TrueColor) render(msg string) string { fn (c TrueColor) render(msg string) string {
if no_color {
return msg
}
return term.rgb(c.r, c.g, c.b, msg) return term.rgb(c.r, c.g, c.b, msg)
} }
fn (c TrueColor) render_bg(msg string) string { fn (c TrueColor) render_bg(msg string) string {
if no_color {
return msg
}
return term.bg_rgb(c.r, c.g, c.b, msg) return term.bg_rgb(c.r, c.g, c.b, msg)
} }