mirror of
https://github.com/RGBCube/color.v
synced 2025-08-01 10:27:45 +00:00
Redo Brush
This commit is contained in:
parent
21d038b604
commit
bd8db0703f
2 changed files with 36 additions and 38 deletions
|
@ -1,65 +1,63 @@
|
||||||
module color
|
module color
|
||||||
|
|
||||||
[noinit]
|
// Interface
|
||||||
pub struct Brush {
|
|
||||||
pub:
|
pub interface Brush {
|
||||||
fg ?Color
|
Style
|
||||||
bg ?Color
|
set_disabled(bool)
|
||||||
style []Style
|
|
||||||
__global:
|
|
||||||
disabled bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[params]
|
[params]
|
||||||
pub struct BrushParams {
|
pub interface BrushParams {
|
||||||
fg ?Color
|
fg ?Color
|
||||||
bg ?Color
|
bg ?Color
|
||||||
style []Style
|
|
||||||
disabled bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_brush(p BrushParams) !Brush {
|
pub fn new_brush(styles ...Style, p BrushParams) !Brush {
|
||||||
mut count := map[Style]int{}
|
mut style_counter := map[Style]int{}
|
||||||
|
|
||||||
for style in p.style {
|
for style in styles {
|
||||||
count[style]++
|
if style is Color {
|
||||||
|
return error('A Color was given instead of a Style')
|
||||||
|
}
|
||||||
|
// Style is definitely not a Color
|
||||||
|
style_counter[style]++
|
||||||
|
|
||||||
if count[style] > 1 {
|
if style_counter[style] > 1 {
|
||||||
return error('Multiple of the same style was provided')
|
return error('Multiple of the same style was provided')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Brush{
|
return BrushImpl{styles + p.fg, bg}
|
||||||
fg: p.fg
|
|
||||||
bg: p.bg
|
|
||||||
style: p.style
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (p &Brush) render(msg string) string {
|
// Declaration
|
||||||
|
|
||||||
|
struct BrushImpl {
|
||||||
|
// The foreground is in styles because it fits that interface
|
||||||
|
styles []Style
|
||||||
|
bg ?Color
|
||||||
|
mut:
|
||||||
|
disabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (p &BrushImpl) render(msg string) string {
|
||||||
return if no_color || p.disabled {
|
return if no_color || p.disabled {
|
||||||
msg
|
msg
|
||||||
} else {
|
} else {
|
||||||
mut result := msg
|
mut result := msg
|
||||||
|
|
||||||
if fg := p.fg {
|
for style in p.styles {
|
||||||
result = fg.render(result)
|
result = style.render(result)
|
||||||
}
|
}
|
||||||
if bg := p.bg {
|
if bg := p.bg {
|
||||||
result = bg.render(result)
|
result = bg.render_bg(result)
|
||||||
}
|
|
||||||
for style in p.style {
|
|
||||||
result = style.render(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (p &Brush) cprint(msg string) {
|
pub fn (mut p &BrushImpl) set_disabled(value bool) {
|
||||||
print(p.render(msg))
|
p.disabled = value
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (p &Brush) cprintln(msg string) {
|
|
||||||
println(p.render(msg))
|
|
||||||
}
|
}
|
||||||
|
|
4
test.v
4
test.v
|
@ -8,8 +8,8 @@ fn main() {
|
||||||
color.bold.cprintln('Hello World')
|
color.bold.cprintln('Hello World')
|
||||||
|
|
||||||
brush := color.new_brush(
|
brush := color.new_brush(
|
||||||
fg: color.rgb(0, 0, 0)!
|
fg: color.rgb(0, 0, 0)
|
||||||
bg: color.hex(0xffffff)!
|
bg: color.hex(0xffffff)
|
||||||
style: [color.bold, color.underline, color.italic]
|
style: [color.bold, color.underline, color.italic]
|
||||||
)!
|
)!
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue