mirror of
https://github.com/RGBCube/color.v
synced 2025-07-31 09:57:47 +00:00
Make a factory method for PaintBrush (validate styles)
This commit is contained in:
parent
cae034cfb3
commit
712324f893
3 changed files with 36 additions and 10 deletions
|
@ -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')
|
||||
```
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue