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 {
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 }

View file

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

View file

@ -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()

View file

@ -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))
}

View file

@ -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)
}