mirror of
https://github.com/RGBCube/color.v
synced 2025-08-01 10:27:45 +00:00
Add utility methods and refactor stuff
This commit is contained in:
parent
a4617fa1e2
commit
91a01bdcbd
8 changed files with 129 additions and 59 deletions
|
@ -3,6 +3,6 @@ module main
|
|||
import color
|
||||
|
||||
fn main() {
|
||||
println(color.red.color('Hello World'))
|
||||
println(color.bold.color('Hello World'))
|
||||
color.red.cprintln('Hello World')
|
||||
color.bold.cprintln('Hello World')
|
||||
}
|
||||
|
|
|
@ -9,5 +9,5 @@ fn main() {
|
|||
styles: [color.bold, color.underline, color.italic]
|
||||
}
|
||||
|
||||
print(p.color('Hello World'))
|
||||
p.cprintln('Hello World')
|
||||
}
|
||||
|
|
|
@ -2,25 +2,6 @@ module color
|
|||
|
||||
import term
|
||||
|
||||
pub const (
|
||||
black = BasicColor.black
|
||||
red = BasicColor.red
|
||||
green = BasicColor.green
|
||||
yellow = BasicColor.yellow
|
||||
blue = BasicColor.blue
|
||||
magenta = BasicColor.magenta
|
||||
cyan = BasicColor.cyan
|
||||
white = BasicColor.white
|
||||
bright_black = BasicColor.bright_black
|
||||
bright_red = BasicColor.bright_red
|
||||
bright_green = BasicColor.bright_green
|
||||
bright_yellow = BasicColor.bright_yellow
|
||||
bright_blue = BasicColor.bright_blue
|
||||
bright_magenta = BasicColor.bright_magenta
|
||||
bright_cyan = BasicColor.bright_cyan
|
||||
bright_white = BasicColor.bright_white
|
||||
)
|
||||
|
||||
enum BasicColor {
|
||||
black
|
||||
red
|
||||
|
@ -40,7 +21,11 @@ enum BasicColor {
|
|||
bright_white
|
||||
}
|
||||
|
||||
pub fn (c BasicColor) color(msg string) string {
|
||||
pub fn (c BasicColor) render(msg string) string {
|
||||
if no_color {
|
||||
return msg
|
||||
}
|
||||
|
||||
func := match c {
|
||||
.black { term.black }
|
||||
.red { term.red }
|
||||
|
@ -59,10 +44,15 @@ pub fn (c BasicColor) color(msg string) string {
|
|||
.bright_cyan { term.bright_cyan }
|
||||
.bright_white { term.bright_white }
|
||||
}
|
||||
|
||||
return func(msg)
|
||||
}
|
||||
|
||||
pub fn (c BasicColor) color_bg(msg string) string {
|
||||
pub fn (c BasicColor) render_bg(msg string) string {
|
||||
if no_color {
|
||||
return msg
|
||||
}
|
||||
|
||||
func := match c {
|
||||
.black { term.bg_black }
|
||||
.red { term.bg_red }
|
||||
|
@ -81,5 +71,22 @@ pub fn (c BasicColor) color_bg(msg string) string {
|
|||
.bright_cyan { term.bright_bg_cyan }
|
||||
.bright_white { term.bright_bg_white }
|
||||
}
|
||||
|
||||
return func(msg)
|
||||
}
|
||||
|
||||
pub fn (c BasicColor) cprint(msg string) {
|
||||
print(c.render(msg))
|
||||
}
|
||||
|
||||
pub fn (c BasicColor) cprintln(msg string) {
|
||||
println(c.render(msg))
|
||||
}
|
||||
|
||||
pub fn (c BasicColor) cprint_bg(msg string) {
|
||||
print(c.render_bg(msg))
|
||||
}
|
||||
|
||||
pub fn (c BasicColor) cprintln_bg(msg string) {
|
||||
println(c.render_bg(msg))
|
||||
}
|
||||
|
|
|
@ -2,11 +2,28 @@ module color
|
|||
|
||||
type Color = BasicColor | TrueColor
|
||||
|
||||
// I have no idea why this is needed
|
||||
fn (c Color) color(msg string) string {
|
||||
return c.color(msg)
|
||||
// I have no idea why these are needed
|
||||
|
||||
fn (c Color) render(msg string) string {
|
||||
return c.render(msg)
|
||||
}
|
||||
|
||||
fn (c Color) color_bg(msg string) string {
|
||||
return c.color_bg(msg)
|
||||
fn (c Color) render_bg(msg string) string {
|
||||
return c.render_bg(msg)
|
||||
}
|
||||
|
||||
fn (c Color) cprint(msg string) string {
|
||||
return c.cprint(msg)
|
||||
}
|
||||
|
||||
fn (c Color) cprintln(msg string) string {
|
||||
return c.cprintln(msg)
|
||||
}
|
||||
|
||||
fn (c Color) cprintln_bg(msg string) string {
|
||||
return c.cprintln_bg(msg)
|
||||
}
|
||||
|
||||
fn (c Color) cprint_bg(msg string) string {
|
||||
return c.cprint_bg(msg)
|
||||
}
|
||||
|
|
37
color/constants.v
Normal file
37
color/constants.v
Normal file
|
@ -0,0 +1,37 @@
|
|||
module color
|
||||
|
||||
import term
|
||||
|
||||
pub const (
|
||||
black = BasicColor.black
|
||||
red = BasicColor.red
|
||||
green = BasicColor.green
|
||||
yellow = BasicColor.yellow
|
||||
blue = BasicColor.blue
|
||||
magenta = BasicColor.magenta
|
||||
cyan = BasicColor.cyan
|
||||
white = BasicColor.white
|
||||
bright_black = BasicColor.bright_black
|
||||
bright_red = BasicColor.bright_red
|
||||
bright_green = BasicColor.bright_green
|
||||
bright_yellow = BasicColor.bright_yellow
|
||||
bright_blue = BasicColor.bright_blue
|
||||
bright_magenta = BasicColor.bright_magenta
|
||||
bright_cyan = BasicColor.bright_cyan
|
||||
bright_white = BasicColor.bright_white
|
||||
)
|
||||
|
||||
pub const (
|
||||
reset = Style.reset
|
||||
bold = Style.bold
|
||||
dim = Style.dim
|
||||
italic = Style.italic
|
||||
underline = Style.underline
|
||||
slow_blink = Style.slow_blink
|
||||
rapid_blink = Style.rapid_blink
|
||||
inverse = Style.inverse
|
||||
hidden = Style.hidden
|
||||
strikethrough = Style.strikethrough
|
||||
)
|
||||
|
||||
const no_color = !term.can_show_color_on_stdout()
|
|
@ -2,8 +2,6 @@ module color
|
|||
|
||||
import term
|
||||
|
||||
const can_show_color = term.can_show_color_on_stdout()
|
||||
|
||||
pub struct PaintBrush {
|
||||
pub:
|
||||
fg ?Color
|
||||
|
@ -11,18 +9,18 @@ pub:
|
|||
styles []Style
|
||||
}
|
||||
|
||||
pub fn (p &PaintBrush) color(msg string) string {
|
||||
if !color.can_show_color {
|
||||
pub fn (p &PaintBrush) render(msg string) string {
|
||||
if no_color {
|
||||
return msg
|
||||
}
|
||||
|
||||
mut result := msg
|
||||
|
||||
if fg := p.fg {
|
||||
result = fg.color(result)
|
||||
result = fg.render(result)
|
||||
}
|
||||
if bg := p.bg {
|
||||
result = bg.color(result)
|
||||
result = bg.render(result)
|
||||
}
|
||||
|
||||
for style in p.styles {
|
||||
|
@ -31,3 +29,11 @@ pub fn (p &PaintBrush) color(msg string) string {
|
|||
|
||||
return result
|
||||
}
|
||||
|
||||
pub fn (p &PaintBrush) cprint(msg string) {
|
||||
print(p.render(msg))
|
||||
}
|
||||
|
||||
pub fn (p &PaintBrush) cprintln(msg string) {
|
||||
println(p.render(msg))
|
||||
}
|
||||
|
|
|
@ -2,19 +2,6 @@ module color
|
|||
|
||||
import term
|
||||
|
||||
pub const (
|
||||
reset = Style.reset
|
||||
bold = Style.bold
|
||||
dim = Style.dim
|
||||
italic = Style.italic
|
||||
underline = Style.underline
|
||||
slow_blink = Style.slow_blink
|
||||
rapid_blink = Style.rapid_blink
|
||||
inverse = Style.inverse
|
||||
hidden = Style.hidden
|
||||
strikethrough = Style.strikethrough
|
||||
)
|
||||
|
||||
enum Style {
|
||||
reset
|
||||
bold
|
||||
|
|
|
@ -2,7 +2,14 @@ module color
|
|||
|
||||
import term
|
||||
|
||||
pub fn rgb(r int, g int, b int) !TrueColor {
|
||||
struct TrueColor {
|
||||
pub:
|
||||
r int
|
||||
g int
|
||||
b int
|
||||
}
|
||||
|
||||
pub fn rgb(r int, g int, b int) !Color {
|
||||
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
|
||||
return error('Red, green and blue must each be between 0 and 255')
|
||||
}
|
||||
|
@ -13,22 +20,31 @@ pub fn rgb(r int, g int, b int) !TrueColor {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn hex(hex int) !TrueColor {
|
||||
pub fn hex(hex int) !Color {
|
||||
return rgb(hex >> 16, hex >> 8 & 0xFF, hex & 0xFF)!
|
||||
}
|
||||
|
||||
[noinit]
|
||||
struct TrueColor {
|
||||
pub:
|
||||
r int
|
||||
g int
|
||||
b int
|
||||
}
|
||||
|
||||
pub fn (c TrueColor) color(msg string) string {
|
||||
pub fn (c TrueColor) render(msg string) string {
|
||||
return term.rgb(c.r, c.g, c.b, msg)
|
||||
}
|
||||
|
||||
pub fn (c TrueColor) color_bg(msg string) string {
|
||||
pub fn (c TrueColor) render_bg(msg string) string {
|
||||
return term.bg_rgb(c.r, c.g, c.b, msg)
|
||||
}
|
||||
|
||||
pub fn (c TrueColor) cprint(msg string) {
|
||||
print(c.render(msg))
|
||||
}
|
||||
|
||||
pub fn (c TrueColor) cprintln(msg string) {
|
||||
println(c.render(msg))
|
||||
}
|
||||
|
||||
pub fn (c TrueColor) cprint_bg(msg string) {
|
||||
print(c.render_bg(msg))
|
||||
}
|
||||
|
||||
pub fn (c TrueColor) cprintln_bg(msg string) {
|
||||
println(c.render_bg(msg))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue