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

Fix Brush

This commit is contained in:
RGBCube 2022-11-20 21:11:52 +03:00
parent d411886407
commit 3f3b453fd4

View file

@ -4,6 +4,7 @@ module color
pub interface Brush { pub interface Brush {
Style Style
mut:
set_disabled(bool) set_disabled(bool)
} }
@ -11,12 +12,13 @@ pub interface Brush {
pub interface BrushParams { pub interface BrushParams {
fg ?Color fg ?Color
bg ?Color bg ?Color
styles []Style
} }
pub fn new_brush(styles ...Style, p BrushParams) !Brush { pub fn new_brush(p BrushParams) !Brush {
mut style_counter := map[Style]int{} mut style_counter := map[Style]int{}
for style in styles { for style in p.styles {
if style is Color { if style is Color {
return error('A Color was given instead of a Style') return error('A Color was given instead of a Style')
} }
@ -28,15 +30,19 @@ pub fn new_brush(styles ...Style, p BrushParams) !Brush {
} }
} }
return BrushImpl{styles + p.fg, bg} return BrushImpl{
fg: p.fg
bg: p.bg
styles: p.styles
}
} }
// Declaration // Declaration
struct BrushImpl { struct BrushImpl {
// The foreground is in styles because it fits that interface fg ?Color
styles []Style
bg ?Color bg ?Color
styles []Style
mut: mut:
disabled bool disabled bool
} }
@ -47,17 +53,20 @@ fn (p &BrushImpl) render(msg string) string {
} else { } else {
mut result := msg mut result := msg
for style in p.styles { if fg := p.fg {
result = style.render(result) result = fg.render(result)
} }
if bg := p.bg { if bg := p.bg {
result = bg.render_bg(result) result = bg.render_bg(result)
} }
for style in p.styles {
result = style.render(result)
}
result result
} }
} }
fn (mut p &BrushImpl) set_disabled(value bool) { fn (mut p BrushImpl) set_disabled(value bool) {
p.disabled = value p.disabled = value
} }