1
Fork 0
mirror of https://github.com/RGBCube/color.v synced 2025-08-01 10:27: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,16 +4,16 @@ import term
pub const ( pub const (
// Styles // Styles
reset = Style(StyleImpl.reset) reset = Style(StyleImpl{term.reset})
bold = Style(StyleImpl.bold) bold = Style(StyleImpl{term.bold})
dim = Style(StyleImpl.dim) dim = Style(StyleImpl{term.dim})
italic = Style(StyleImpl.italic) italic = Style(StyleImpl{term.italic})
underline = Style(StyleImpl.underline) underline = Style(StyleImpl{term.underline})
slow_blink = Style(StyleImpl.slow_blink) slow_blink = Style(StyleImpl{term.slow_blink})
rapid_blink = Style(StyleImpl.rapid_blink) rapid_blink = Style(StyleImpl{term.rapid_blink})
inverse = Style(StyleImpl.inverse) inverse = Style(StyleImpl{term.inverse})
hidden = Style(StyleImpl.hidden) hidden = Style(StyleImpl{term.hidden})
strikethrough = Style(StyleImpl.strikethrough) strikethrough = Style(StyleImpl{term.strikethrough})
// Colors // Colors
black = Color(BasicColor{term.black, term.bg_black}) black = Color(BasicColor{term.black, term.bg_black})
bright_black = Color(BasicColor{term.bright_black, term.bright_bg_black}) bright_black = Color(BasicColor{term.bright_black, term.bright_bg_black})

View file

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