1
Fork 0
mirror of https://github.com/RGBCube/color.v synced 2025-07-30 17:37:45 +00:00

Make StyleImpl more performant and better

This commit is contained in:
RGBCube 2022-12-16 21:46:29 +03:00
parent 87ab517bea
commit 7ab21e05d3
2 changed files with 22 additions and 46 deletions

View file

@ -4,26 +4,26 @@ import term
pub const (
// Styles
reset = Style(StyleImpl.reset)
bold = Style(StyleImpl.bold)
dim = Style(StyleImpl.dim)
italic = Style(StyleImpl.italic)
underline = Style(StyleImpl.underline)
slow_blink = Style(StyleImpl.slow_blink)
rapid_blink = Style(StyleImpl.rapid_blink)
inverse = Style(StyleImpl.inverse)
hidden = Style(StyleImpl.hidden)
strikethrough = Style(StyleImpl.strikethrough)
reset = Style(StyleImpl{term.reset})
bold = Style(StyleImpl{term.bold})
dim = Style(StyleImpl{term.dim})
italic = Style(StyleImpl{term.italic})
underline = Style(StyleImpl{term.underline})
slow_blink = Style(StyleImpl{term.slow_blink})
rapid_blink = Style(StyleImpl{term.rapid_blink})
inverse = Style(StyleImpl{term.inverse})
hidden = Style(StyleImpl{term.hidden})
strikethrough = Style(StyleImpl{term.strikethrough})
// Colors
black = Color(BasicColor{term.black, term.bg_black})
bright_black = Color(BasicColor{term.bright_black, term.bright_bg_black})
red = Color(BasicColor{term.red, term.bg_red})
bright_red = Color(BasicColor{term.bright_red, term.bright_bg_red})
green = Color(BasicColor{term.green, term.bg_green})
bright_green = Color(BasicColor{term.bright_green, term.bright_bg_green})
yellow = Color(BasicColor{term.yellow, term.bg_yellow})
bright_yellow = Color(BasicColor{term.bright_yellow, term.bright_bg_yellow})
blue = Color(BasicColor{term.blue, term.bg_blue})
black = Color(BasicColor{term.black, term.bg_black})
bright_black = Color(BasicColor{term.bright_black, term.bright_bg_black})
red = Color(BasicColor{term.red, term.bg_red})
bright_red = Color(BasicColor{term.bright_red, term.bright_bg_red})
green = Color(BasicColor{term.green, term.bg_green})
bright_green = Color(BasicColor{term.bright_green, term.bright_bg_green})
yellow = Color(BasicColor{term.yellow, term.bg_yellow})
bright_yellow = Color(BasicColor{term.bright_yellow, term.bright_bg_yellow})
blue = Color(BasicColor{term.blue, term.bg_blue})
bright_blue = Color(BasicColor{term.bright_blue, term.bright_bg_blue})
magenta = Color(BasicColor{term.magenta, term.bg_magenta})
bright_magenta = Color(BasicColor{term.bright_magenta, term.bright_bg_magenta})

View file

@ -1,7 +1,5 @@
module color
import term
// Style is an interface for a style.
pub interface Style {
render(string) string // Renders the string with the given style.
@ -17,36 +15,14 @@ pub fn (s Style) println(str string) {
println(s.render(str))
}
enum StyleImpl {
reset
bold
dim
italic
underline
slow_blink
rapid_blink
inverse
hidden
strikethrough
struct StyleImpl {
render_fn fn (string) string
}
fn (s StyleImpl) render(str string) string {
return if no_color {
str
} else {
func := match s {
.reset { term.reset }
.bold { term.bold }
.dim { term.dim }
.italic { term.italic }
.underline { term.underline }
.slow_blink { term.slow_blink }
.rapid_blink { term.rapid_blink }
.inverse { term.inverse }
.hidden { term.hidden }
.strikethrough { term.strikethrough }
}
func(str)
s.render_fn(str)
}
}