diff --git a/README.md b/README.md index 2a3afe1..8765099 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ color.bold.cprintln('Hello World') ```v import color -brush := color.PaintBrush{ +brush := color.new_brush( fg: color.rgb(0, 0, 0)! bg: color.hex(0xffffff)! - styles: [color.bold, color.underline, color.italic] -} + style: [color.bold, color.underline, color.italic] +)! brush.cprintln('Hello World') ``` diff --git a/builder_error2.v b/builder_error2.v index 37ea936..2ac366c 100644 --- a/builder_error2.v +++ b/builder_error2.v @@ -3,11 +3,11 @@ module main import color fn main() { - p := color.PaintBrush{ + p := color.new_brush( fg: color.rgb(0, 0, 0)! bg: color.hex(0xffffff)! - styles: [color.bold, color.underline, color.italic] - } + style: [color.bold, color.underline, color.italic] + )! p.cprintln('Hello World') } diff --git a/color/paintbrush.v b/color/paintbrush.v index 101e758..d4a6969 100644 --- a/color/paintbrush.v +++ b/color/paintbrush.v @@ -1,10 +1,36 @@ module color +[noinit] pub struct PaintBrush { pub: - fg ?Color - bg ?Color - styles []Style + fg ?Color + bg ?Color + 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 { @@ -20,7 +46,7 @@ pub fn (p &PaintBrush) render(msg string) string { if bg := p.bg { result = bg.render(result) } - for style in p.styles { + for style in p.style { result = style.render(result) }