mirror of
https://github.com/RGBCube/color.v
synced 2025-07-30 17:37:45 +00:00
Docs and fixes
This commit is contained in:
parent
3927f096e7
commit
b5db7e911f
6 changed files with 119 additions and 121 deletions
116
color/color.v
116
color/color.v
|
@ -2,26 +2,25 @@ module color
|
|||
|
||||
import term
|
||||
|
||||
// Interface
|
||||
|
||||
// Color is a Style extension that also supports rendering in the background.
|
||||
// Every Color is a Style, but not every Style is a Color.
|
||||
pub interface Color {
|
||||
Style
|
||||
render_bg(string) string
|
||||
Style // Inherits Style methods.
|
||||
render_bg(string) string // Renders the color as the background for the given string.
|
||||
}
|
||||
|
||||
// print_bg prints the given string with the color in the background.
|
||||
pub fn (c Color) print_bg(msg string) {
|
||||
print(c.render_bg(msg))
|
||||
pub fn (c Color) print_bg(str string) {
|
||||
print(c.render_bg(str))
|
||||
}
|
||||
|
||||
// println_bg prints the given string with the color in the background with an added newline.
|
||||
pub fn (c Color) println_bg(msg string) {
|
||||
println(c.render_bg(msg))
|
||||
pub fn (c Color) println_bg(str string) {
|
||||
println(c.render_bg(str))
|
||||
}
|
||||
|
||||
// TrueColor Initialization
|
||||
|
||||
// rgb returns a Color with the given RGB values.
|
||||
pub fn rgb(r u8, g u8, b u8) Color {
|
||||
return TrueColor{
|
||||
r: r
|
||||
|
@ -30,31 +29,30 @@ pub fn rgb(r u8, g u8, b u8) Color {
|
|||
}
|
||||
}
|
||||
|
||||
// hex returns a Color with the given hex value.
|
||||
pub fn hex(hex int) Color {
|
||||
return rgb(u8(hex >> 16), u8(hex >> 8 & 0xFF), u8(hex & 0xFF))
|
||||
}
|
||||
|
||||
// Implementations
|
||||
|
||||
struct TrueColor {
|
||||
r u8
|
||||
g u8
|
||||
b u8
|
||||
}
|
||||
|
||||
fn (c TrueColor) render(msg string) string {
|
||||
fn (c TrueColor) render(str string) string {
|
||||
return if no_color {
|
||||
msg
|
||||
str
|
||||
} else {
|
||||
term.rgb(c.r, c.g, c.b, msg)
|
||||
term.rgb(c.r, c.g, c.b, str)
|
||||
}
|
||||
}
|
||||
|
||||
fn (c TrueColor) render_bg(msg string) string {
|
||||
fn (c TrueColor) render_bg(str string) string {
|
||||
return if no_color {
|
||||
msg
|
||||
str
|
||||
} else {
|
||||
term.bg_rgb(c.r, c.g, c.b, msg)
|
||||
term.bg_rgb(c.r, c.g, c.b, str)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,70 +61,70 @@ enum BasicColor {
|
|||
red
|
||||
green
|
||||
yellow
|
||||
// blue
|
||||
// magenta
|
||||
// cyan
|
||||
// white
|
||||
// bright_black
|
||||
// bright_red
|
||||
// bright_green
|
||||
// bright_yellow
|
||||
// bright_blue
|
||||
// bright_magenta
|
||||
// bright_cyan
|
||||
// bright_white
|
||||
blue
|
||||
magenta
|
||||
cyan
|
||||
white
|
||||
bright_black
|
||||
bright_red
|
||||
bright_green
|
||||
bright_yellow
|
||||
bright_blue
|
||||
bright_magenta
|
||||
bright_cyan
|
||||
bright_white
|
||||
}
|
||||
|
||||
fn (c BasicColor) render(msg string) string {
|
||||
fn (c BasicColor) render(str string) string {
|
||||
return if no_color {
|
||||
msg
|
||||
str
|
||||
} else {
|
||||
func := match c {
|
||||
.black { term.black }
|
||||
.red { term.red }
|
||||
.green { term.green }
|
||||
.yellow { term.yellow }
|
||||
//.blue { term.blue }
|
||||
//.magenta { term.magenta }
|
||||
//.cyan { term.cyan }
|
||||
//.white { term.white }
|
||||
//.bright_black { term.bright_black }
|
||||
//.bright_red { term.bright_red }
|
||||
//.bright_green { term.bright_green }
|
||||
//.bright_yellow { term.bright_yellow }
|
||||
//.bright_blue { term.bright_blue }
|
||||
//.bright_magenta { term.bright_magenta }
|
||||
//.bright_cyan { term.bright_cyan }
|
||||
//.bright_white { term.bright_white }
|
||||
.blue { term.blue }
|
||||
.magenta { term.magenta }
|
||||
.cyan { term.cyan }
|
||||
.white { term.white }
|
||||
.bright_black { term.bright_black }
|
||||
.bright_red { term.bright_red }
|
||||
.bright_green { term.bright_green }
|
||||
.bright_yellow { term.bright_yellow }
|
||||
.bright_blue { term.bright_blue }
|
||||
.bright_magenta { term.bright_magenta }
|
||||
.bright_cyan { term.bright_cyan }
|
||||
.bright_white { term.bright_white }
|
||||
}
|
||||
|
||||
func(msg)
|
||||
func(str)
|
||||
}
|
||||
}
|
||||
|
||||
fn (c BasicColor) render_bg(msg string) string {
|
||||
fn (c BasicColor) render_bg(str string) string {
|
||||
return if no_color {
|
||||
msg
|
||||
str
|
||||
} else {
|
||||
func := match c {
|
||||
.black { term.bg_black }
|
||||
.red { term.bg_red }
|
||||
.green { term.bg_green }
|
||||
.yellow { term.bg_yellow }
|
||||
//.blue { term.bg_blue }
|
||||
//.magenta { term.bg_magenta }
|
||||
//.cyan { term.bg_cyan }
|
||||
//.white { term.bg_white }
|
||||
//.bright_black { term.bright_bg_black }
|
||||
//.bright_red { term.bright_bg_red }
|
||||
//.bright_green { term.bright_bg_green }
|
||||
//.bright_yellow { term.bright_bg_yellow }
|
||||
//.bright_blue { term.bright_bg_blue }
|
||||
//.bright_magenta { term.bright_bg_magenta }
|
||||
//.bright_cyan { term.bright_bg_cyan }
|
||||
//.bright_white { term.bright_bg_white }
|
||||
.blue { term.bg_blue }
|
||||
.magenta { term.bg_magenta }
|
||||
.cyan { term.bg_cyan }
|
||||
.white { term.bg_white }
|
||||
.bright_black { term.bright_bg_black }
|
||||
.bright_red { term.bright_bg_red }
|
||||
.bright_green { term.bright_bg_green }
|
||||
.bright_yellow { term.bright_bg_yellow }
|
||||
.bright_blue { term.bright_bg_blue }
|
||||
.bright_magenta { term.bright_bg_magenta }
|
||||
.bright_cyan { term.bright_bg_cyan }
|
||||
.bright_white { term.bright_bg_white }
|
||||
}
|
||||
|
||||
func(msg)
|
||||
func(str)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,33 +4,33 @@ import term
|
|||
|
||||
pub const (
|
||||
// Colors
|
||||
black = Color(BasicColor.black)
|
||||
red = Color(BasicColor.red)
|
||||
green = Color(BasicColor.green)
|
||||
black = Color(BasicColor.black)
|
||||
red = Color(BasicColor.red)
|
||||
green = Color(BasicColor.green)
|
||||
yellow = Color(BasicColor.yellow)
|
||||
// blue = Color(BasicColor.blue)
|
||||
// magenta = Color(BasicColor.magenta)
|
||||
// cyan = Color(BasicColor.cyan)
|
||||
// white = Color(BasicColor.white)
|
||||
// bright_black = Color(BasicColor.bright_black)
|
||||
// bright_red = Color(BasicColor.bright_red)
|
||||
// bright_green = Color(BasicColor.bright_green)
|
||||
// bright_yellow = Color(BasicColor.bright_yellow)
|
||||
// bright_blue = Color(BasicColor.bright_blue)
|
||||
// bright_magenta = Color(BasicColor.bright_magenta)
|
||||
// bright_cyan = Color(BasicColor.bright_cyan)
|
||||
// bright_white = Color(BasicColor.bright_white)
|
||||
blue = Color(BasicColor.blue)
|
||||
magenta = Color(BasicColor.magenta)
|
||||
cyan = Color(BasicColor.cyan)
|
||||
white = Color(BasicColor.white)
|
||||
bright_black = Color(BasicColor.bright_black)
|
||||
bright_red = Color(BasicColor.bright_red)
|
||||
bright_green = Color(BasicColor.bright_green)
|
||||
bright_yellow = Color(BasicColor.bright_yellow)
|
||||
bright_blue = Color(BasicColor.bright_blue)
|
||||
bright_magenta = Color(BasicColor.bright_magenta)
|
||||
bright_cyan = Color(BasicColor.bright_cyan)
|
||||
bright_white = Color(BasicColor.bright_white)
|
||||
// Styles
|
||||
reset = Style(StyleImpl.reset)
|
||||
bold = Style(StyleImpl.bold)
|
||||
dim = Style(StyleImpl.dim)
|
||||
reset = Style(StyleImpl.reset)
|
||||
bold = Style(StyleImpl.bold)
|
||||
dim = Style(StyleImpl.dim)
|
||||
italic = Style(StyleImpl.italic)
|
||||
// underline = Style(StyleImpl.underline)
|
||||
// slow_blink = Style(StyleImpl.slow_blink)
|
||||
// rapid_blink = Style(StyleImpl.rapid_blink)
|
||||
// inverse = Style(StyleImpl.inverse)
|
||||
// hidden = Style(StyleImpl.hidden)
|
||||
// strikethrough = Style(StyleImpl.strikethrough)
|
||||
underline = Style(StyleImpl.underline)
|
||||
slow_blink = Style(StyleImpl.slow_blink)
|
||||
rapid_blink = Style(StyleImpl.rapid_blink)
|
||||
inverse = Style(StyleImpl.inverse)
|
||||
hidden = Style(StyleImpl.hidden)
|
||||
strikethrough = Style(StyleImpl.strikethrough)
|
||||
)
|
||||
|
||||
const no_color = !term.can_show_color_on_stdout()
|
||||
|
|
|
@ -1,16 +1,12 @@
|
|||
module color
|
||||
|
||||
// Interface
|
||||
|
||||
// Brush is the complex Style type that can hold multiple colors and styles.
|
||||
pub interface Brush {
|
||||
Style
|
||||
mut:
|
||||
set_disabled(bool)
|
||||
Style // Inherits Style methods.
|
||||
mut:
|
||||
set_disabled(bool) // Enable/disable the brush. When disabled, the brush doesn't render anything and returns the given string.
|
||||
}
|
||||
|
||||
// Initialization
|
||||
|
||||
[params]
|
||||
pub struct BrushParams {
|
||||
fg ?Color
|
||||
|
@ -20,7 +16,8 @@ pub struct BrushParams {
|
|||
}
|
||||
|
||||
// new_brush creates a new Brush with the given parameters.
|
||||
pub fn new_brush(p BrushParams) !Brush {
|
||||
|
||||
pub fn new_brush(p BrushParams) Brush {
|
||||
return BrushImpl{
|
||||
fg: p.fg
|
||||
bg: p.bg
|
||||
|
@ -29,21 +26,28 @@ pub fn new_brush(p BrushParams) !Brush {
|
|||
}
|
||||
}
|
||||
|
||||
// new_brush_pointers creates a new Brush pointer with the given parameters.
|
||||
// This is useful for long-lived brush instances.
|
||||
|
||||
pub fn new_brush_pointer(p BrushParams) &Brush {
|
||||
return &new_brush(p)
|
||||
}
|
||||
|
||||
// Declaration
|
||||
|
||||
struct BrushImpl {
|
||||
fg ?Color
|
||||
bg ?Color
|
||||
styles []Style
|
||||
mut:
|
||||
mut:
|
||||
disabled bool
|
||||
}
|
||||
|
||||
fn (p &BrushImpl) render(msg string) string {
|
||||
fn (p BrushImpl) render(str string) string {
|
||||
return if no_color || p.disabled {
|
||||
msg
|
||||
str
|
||||
} else {
|
||||
mut result := msg
|
||||
mut result := str
|
||||
|
||||
if fg := p.fg {
|
||||
result = fg.render(result)
|
||||
|
|
|
@ -2,55 +2,51 @@ module color
|
|||
|
||||
import term
|
||||
|
||||
// Interface
|
||||
|
||||
// Style is an interface for a style.
|
||||
pub interface Style {
|
||||
render(string) string
|
||||
render(string) string // Renders the string with the given style.
|
||||
}
|
||||
|
||||
// print prints the given string with the given style.
|
||||
pub fn (s Style) print(msg string) {
|
||||
print(s.render(msg))
|
||||
pub fn (s Style) print(str string) {
|
||||
print(s.render(str))
|
||||
}
|
||||
|
||||
// println prints the given string with the given style with an added newline.
|
||||
pub fn (s Style) println(msg string) {
|
||||
println(s.render(msg))
|
||||
pub fn (s Style) println(str string) {
|
||||
println(s.render(str))
|
||||
}
|
||||
|
||||
// Implementation
|
||||
|
||||
enum StyleImpl {
|
||||
reset
|
||||
bold
|
||||
dim
|
||||
italic
|
||||
// underline
|
||||
// slow_blink
|
||||
// rapid_blink
|
||||
// inverse
|
||||
// hidden
|
||||
// strikethrough
|
||||
underline
|
||||
slow_blink
|
||||
rapid_blink
|
||||
inverse
|
||||
hidden
|
||||
strikethrough
|
||||
}
|
||||
|
||||
fn (s StyleImpl) render(msg string) string {
|
||||
fn (s StyleImpl) render(str string) string {
|
||||
return if no_color {
|
||||
msg
|
||||
str
|
||||
} else {
|
||||
func := match s {
|
||||
.reset { term.reset }
|
||||
.bold { term.bold }
|
||||
.dim { term.dim }
|
||||
.italic { term.italic }
|
||||
//.underline { term.underline }
|
||||
//.slow_blink { term.slow_blink }
|
||||
//.rapid_blink { term.rapid_blink }
|
||||
//.inverse { term.inverse }
|
||||
//.hidden { term.hidden }
|
||||
//.strikethrough { term.strikethrough }
|
||||
.underline { term.underline }
|
||||
.slow_blink { term.slow_blink }
|
||||
.rapid_blink { term.rapid_blink }
|
||||
.inverse { term.inverse }
|
||||
.hidden { term.hidden }
|
||||
.strikethrough { term.strikethrough }
|
||||
}
|
||||
|
||||
func(msg)
|
||||
func(str)
|
||||
}
|
||||
}
|
||||
|
|
2
test.v
2
test.v
|
@ -11,7 +11,7 @@ fn main() {
|
|||
fg: color.rgb(0, 0, 0)
|
||||
bg: color.hex(0xffffff)
|
||||
styles: [color.bold, color.dim, color.italic]
|
||||
)!
|
||||
)
|
||||
|
||||
brush.println('Hello World')
|
||||
}
|
||||
|
|
2
v.mod
2
v.mod
|
@ -1,7 +1,7 @@
|
|||
Module {
|
||||
name: 'color'
|
||||
description: 'An easier way to print colored text to the terminal.'
|
||||
version: '1.1.0'
|
||||
version: '1.2.0'
|
||||
license: 'MIT'
|
||||
dependencies: []
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue