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

Make a factory method for PaintBrush (validate styles)

This commit is contained in:
RGBCube 2022-11-18 21:30:28 +03:00
parent cae034cfb3
commit 712324f893
3 changed files with 36 additions and 10 deletions

View file

@ -19,11 +19,11 @@ color.bold.cprintln('Hello World')
```v ```v
import color import color
brush := color.PaintBrush{ brush := color.new_brush(
fg: color.rgb(0, 0, 0)! fg: color.rgb(0, 0, 0)!
bg: color.hex(0xffffff)! bg: color.hex(0xffffff)!
styles: [color.bold, color.underline, color.italic] style: [color.bold, color.underline, color.italic]
} )!
brush.cprintln('Hello World') brush.cprintln('Hello World')
``` ```

View file

@ -3,11 +3,11 @@ module main
import color import color
fn main() { fn main() {
p := color.PaintBrush{ p := color.new_brush(
fg: color.rgb(0, 0, 0)! fg: color.rgb(0, 0, 0)!
bg: color.hex(0xffffff)! bg: color.hex(0xffffff)!
styles: [color.bold, color.underline, color.italic] style: [color.bold, color.underline, color.italic]
} )!
p.cprintln('Hello World') p.cprintln('Hello World')
} }

View file

@ -1,10 +1,36 @@
module color module color
[noinit]
pub struct PaintBrush { pub struct PaintBrush {
pub: pub:
fg ?Color fg ?Color
bg ?Color bg ?Color
styles []Style style []Style
}
[params]
pub struct PaintBrushParams {
fg ?Color
bg ?Color
style []Style
}
pub fn new_brush(p PaintBrushParams) !PaintBrush {
mut count := map[Style]int{}
for style in p.style {
count[style]++
if count[style] > 1 {
return error('Multiple of the same style was provided')
}
}
return PaintBrush{
fg: p.fg
bg: p.bg
style: p.style
}
} }
pub fn (p &PaintBrush) render(msg string) string { pub fn (p &PaintBrush) render(msg string) string {
@ -20,7 +46,7 @@ pub fn (p &PaintBrush) render(msg string) string {
if bg := p.bg { if bg := p.bg {
result = bg.render(result) result = bg.render(result)
} }
for style in p.styles { for style in p.style {
result = style.render(result) result = style.render(result)
} }