mirror of
https://github.com/RGBCube/color.v
synced 2025-08-01 10:27: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
|
import term
|
||||||
|
|
||||||
// Interface
|
|
||||||
|
|
||||||
// Color is a Style extension that also supports rendering in the background.
|
// 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 {
|
pub interface Color {
|
||||||
Style
|
Style // Inherits Style methods.
|
||||||
render_bg(string) string
|
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.
|
// print_bg prints the given string with the color in the background.
|
||||||
pub fn (c Color) print_bg(msg string) {
|
pub fn (c Color) print_bg(str string) {
|
||||||
print(c.render_bg(msg))
|
print(c.render_bg(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
// println_bg prints the given string with the color in the background with an added newline.
|
// println_bg prints the given string with the color in the background with an added newline.
|
||||||
pub fn (c Color) println_bg(msg string) {
|
pub fn (c Color) println_bg(str string) {
|
||||||
println(c.render_bg(msg))
|
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 {
|
pub fn rgb(r u8, g u8, b u8) Color {
|
||||||
return TrueColor{
|
return TrueColor{
|
||||||
r: r
|
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 {
|
pub fn hex(hex int) Color {
|
||||||
return rgb(u8(hex >> 16), u8(hex >> 8 & 0xFF), u8(hex & 0xFF))
|
return rgb(u8(hex >> 16), u8(hex >> 8 & 0xFF), u8(hex & 0xFF))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implementations
|
|
||||||
|
|
||||||
struct TrueColor {
|
struct TrueColor {
|
||||||
r u8
|
r u8
|
||||||
g u8
|
g u8
|
||||||
b u8
|
b u8
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c TrueColor) render(msg string) string {
|
fn (c TrueColor) render(str string) string {
|
||||||
return if no_color {
|
return if no_color {
|
||||||
msg
|
str
|
||||||
} else {
|
} 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 {
|
return if no_color {
|
||||||
msg
|
str
|
||||||
} else {
|
} 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
|
red
|
||||||
green
|
green
|
||||||
yellow
|
yellow
|
||||||
// blue
|
blue
|
||||||
// magenta
|
magenta
|
||||||
// cyan
|
cyan
|
||||||
// white
|
white
|
||||||
// bright_black
|
bright_black
|
||||||
// bright_red
|
bright_red
|
||||||
// bright_green
|
bright_green
|
||||||
// bright_yellow
|
bright_yellow
|
||||||
// bright_blue
|
bright_blue
|
||||||
// bright_magenta
|
bright_magenta
|
||||||
// bright_cyan
|
bright_cyan
|
||||||
// bright_white
|
bright_white
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c BasicColor) render(msg string) string {
|
fn (c BasicColor) render(str string) string {
|
||||||
return if no_color {
|
return if no_color {
|
||||||
msg
|
str
|
||||||
} else {
|
} else {
|
||||||
func := match c {
|
func := match c {
|
||||||
.black { term.black }
|
.black { term.black }
|
||||||
.red { term.red }
|
.red { term.red }
|
||||||
.green { term.green }
|
.green { term.green }
|
||||||
.yellow { term.yellow }
|
.yellow { term.yellow }
|
||||||
//.blue { term.blue }
|
.blue { term.blue }
|
||||||
//.magenta { term.magenta }
|
.magenta { term.magenta }
|
||||||
//.cyan { term.cyan }
|
.cyan { term.cyan }
|
||||||
//.white { term.white }
|
.white { term.white }
|
||||||
//.bright_black { term.bright_black }
|
.bright_black { term.bright_black }
|
||||||
//.bright_red { term.bright_red }
|
.bright_red { term.bright_red }
|
||||||
//.bright_green { term.bright_green }
|
.bright_green { term.bright_green }
|
||||||
//.bright_yellow { term.bright_yellow }
|
.bright_yellow { term.bright_yellow }
|
||||||
//.bright_blue { term.bright_blue }
|
.bright_blue { term.bright_blue }
|
||||||
//.bright_magenta { term.bright_magenta }
|
.bright_magenta { term.bright_magenta }
|
||||||
//.bright_cyan { term.bright_cyan }
|
.bright_cyan { term.bright_cyan }
|
||||||
//.bright_white { term.bright_white }
|
.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 {
|
return if no_color {
|
||||||
msg
|
str
|
||||||
} else {
|
} else {
|
||||||
func := match c {
|
func := match c {
|
||||||
.black { term.bg_black }
|
.black { term.bg_black }
|
||||||
.red { term.bg_red }
|
.red { term.bg_red }
|
||||||
.green { term.bg_green }
|
.green { term.bg_green }
|
||||||
.yellow { term.bg_yellow }
|
.yellow { term.bg_yellow }
|
||||||
//.blue { term.bg_blue }
|
.blue { term.bg_blue }
|
||||||
//.magenta { term.bg_magenta }
|
.magenta { term.bg_magenta }
|
||||||
//.cyan { term.bg_cyan }
|
.cyan { term.bg_cyan }
|
||||||
//.white { term.bg_white }
|
.white { term.bg_white }
|
||||||
//.bright_black { term.bright_bg_black }
|
.bright_black { term.bright_bg_black }
|
||||||
//.bright_red { term.bright_bg_red }
|
.bright_red { term.bright_bg_red }
|
||||||
//.bright_green { term.bright_bg_green }
|
.bright_green { term.bright_bg_green }
|
||||||
//.bright_yellow { term.bright_bg_yellow }
|
.bright_yellow { term.bright_bg_yellow }
|
||||||
//.bright_blue { term.bright_bg_blue }
|
.bright_blue { term.bright_bg_blue }
|
||||||
//.bright_magenta { term.bright_bg_magenta }
|
.bright_magenta { term.bright_bg_magenta }
|
||||||
//.bright_cyan { term.bright_bg_cyan }
|
.bright_cyan { term.bright_bg_cyan }
|
||||||
//.bright_white { term.bright_bg_white }
|
.bright_white { term.bright_bg_white }
|
||||||
}
|
}
|
||||||
|
|
||||||
func(msg)
|
func(str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,29 +8,29 @@ pub const (
|
||||||
red = Color(BasicColor.red)
|
red = Color(BasicColor.red)
|
||||||
green = Color(BasicColor.green)
|
green = Color(BasicColor.green)
|
||||||
yellow = Color(BasicColor.yellow)
|
yellow = Color(BasicColor.yellow)
|
||||||
// blue = Color(BasicColor.blue)
|
blue = Color(BasicColor.blue)
|
||||||
// magenta = Color(BasicColor.magenta)
|
magenta = Color(BasicColor.magenta)
|
||||||
// cyan = Color(BasicColor.cyan)
|
cyan = Color(BasicColor.cyan)
|
||||||
// white = Color(BasicColor.white)
|
white = Color(BasicColor.white)
|
||||||
// bright_black = Color(BasicColor.bright_black)
|
bright_black = Color(BasicColor.bright_black)
|
||||||
// bright_red = Color(BasicColor.bright_red)
|
bright_red = Color(BasicColor.bright_red)
|
||||||
// bright_green = Color(BasicColor.bright_green)
|
bright_green = Color(BasicColor.bright_green)
|
||||||
// bright_yellow = Color(BasicColor.bright_yellow)
|
bright_yellow = Color(BasicColor.bright_yellow)
|
||||||
// bright_blue = Color(BasicColor.bright_blue)
|
bright_blue = Color(BasicColor.bright_blue)
|
||||||
// bright_magenta = Color(BasicColor.bright_magenta)
|
bright_magenta = Color(BasicColor.bright_magenta)
|
||||||
// bright_cyan = Color(BasicColor.bright_cyan)
|
bright_cyan = Color(BasicColor.bright_cyan)
|
||||||
// bright_white = Color(BasicColor.bright_white)
|
bright_white = Color(BasicColor.bright_white)
|
||||||
// Styles
|
// Styles
|
||||||
reset = Style(StyleImpl.reset)
|
reset = Style(StyleImpl.reset)
|
||||||
bold = Style(StyleImpl.bold)
|
bold = Style(StyleImpl.bold)
|
||||||
dim = Style(StyleImpl.dim)
|
dim = Style(StyleImpl.dim)
|
||||||
italic = Style(StyleImpl.italic)
|
italic = Style(StyleImpl.italic)
|
||||||
// underline = Style(StyleImpl.underline)
|
underline = Style(StyleImpl.underline)
|
||||||
// slow_blink = Style(StyleImpl.slow_blink)
|
slow_blink = Style(StyleImpl.slow_blink)
|
||||||
// rapid_blink = Style(StyleImpl.rapid_blink)
|
rapid_blink = Style(StyleImpl.rapid_blink)
|
||||||
// inverse = Style(StyleImpl.inverse)
|
inverse = Style(StyleImpl.inverse)
|
||||||
// hidden = Style(StyleImpl.hidden)
|
hidden = Style(StyleImpl.hidden)
|
||||||
// strikethrough = Style(StyleImpl.strikethrough)
|
strikethrough = Style(StyleImpl.strikethrough)
|
||||||
)
|
)
|
||||||
|
|
||||||
const no_color = !term.can_show_color_on_stdout()
|
const no_color = !term.can_show_color_on_stdout()
|
||||||
|
|
|
@ -1,16 +1,12 @@
|
||||||
module color
|
module color
|
||||||
|
|
||||||
// Interface
|
|
||||||
|
|
||||||
// Brush is the complex Style type that can hold multiple colors and styles.
|
// Brush is the complex Style type that can hold multiple colors and styles.
|
||||||
pub interface Brush {
|
pub interface Brush {
|
||||||
Style
|
Style // Inherits Style methods.
|
||||||
mut:
|
mut:
|
||||||
set_disabled(bool)
|
set_disabled(bool) // Enable/disable the brush. When disabled, the brush doesn't render anything and returns the given string.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialization
|
|
||||||
|
|
||||||
[params]
|
[params]
|
||||||
pub struct BrushParams {
|
pub struct BrushParams {
|
||||||
fg ?Color
|
fg ?Color
|
||||||
|
@ -20,7 +16,8 @@ pub struct BrushParams {
|
||||||
}
|
}
|
||||||
|
|
||||||
// new_brush creates a new Brush with the given parameters.
|
// 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{
|
return BrushImpl{
|
||||||
fg: p.fg
|
fg: p.fg
|
||||||
bg: p.bg
|
bg: p.bg
|
||||||
|
@ -29,6 +26,13 @@ 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
|
// Declaration
|
||||||
|
|
||||||
struct BrushImpl {
|
struct BrushImpl {
|
||||||
|
@ -39,11 +43,11 @@ mut:
|
||||||
disabled bool
|
disabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (p &BrushImpl) render(msg string) string {
|
fn (p BrushImpl) render(str string) string {
|
||||||
return if no_color || p.disabled {
|
return if no_color || p.disabled {
|
||||||
msg
|
str
|
||||||
} else {
|
} else {
|
||||||
mut result := msg
|
mut result := str
|
||||||
|
|
||||||
if fg := p.fg {
|
if fg := p.fg {
|
||||||
result = fg.render(result)
|
result = fg.render(result)
|
||||||
|
|
|
@ -2,55 +2,51 @@ module color
|
||||||
|
|
||||||
import term
|
import term
|
||||||
|
|
||||||
// Interface
|
|
||||||
|
|
||||||
// Style is an interface for a style.
|
// Style is an interface for a style.
|
||||||
pub interface 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.
|
// print prints the given string with the given style.
|
||||||
pub fn (s Style) print(msg string) {
|
pub fn (s Style) print(str string) {
|
||||||
print(s.render(msg))
|
print(s.render(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
// println prints the given string with the given style with an added newline.
|
// println prints the given string with the given style with an added newline.
|
||||||
pub fn (s Style) println(msg string) {
|
pub fn (s Style) println(str string) {
|
||||||
println(s.render(msg))
|
println(s.render(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implementation
|
|
||||||
|
|
||||||
enum StyleImpl {
|
enum StyleImpl {
|
||||||
reset
|
reset
|
||||||
bold
|
bold
|
||||||
dim
|
dim
|
||||||
italic
|
italic
|
||||||
// underline
|
underline
|
||||||
// slow_blink
|
slow_blink
|
||||||
// rapid_blink
|
rapid_blink
|
||||||
// inverse
|
inverse
|
||||||
// hidden
|
hidden
|
||||||
// strikethrough
|
strikethrough
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (s StyleImpl) render(msg string) string {
|
fn (s StyleImpl) render(str string) string {
|
||||||
return if no_color {
|
return if no_color {
|
||||||
msg
|
str
|
||||||
} else {
|
} else {
|
||||||
func := match s {
|
func := match s {
|
||||||
.reset { term.reset }
|
.reset { term.reset }
|
||||||
.bold { term.bold }
|
.bold { term.bold }
|
||||||
.dim { term.dim }
|
.dim { term.dim }
|
||||||
.italic { term.italic }
|
.italic { term.italic }
|
||||||
//.underline { term.underline }
|
.underline { term.underline }
|
||||||
//.slow_blink { term.slow_blink }
|
.slow_blink { term.slow_blink }
|
||||||
//.rapid_blink { term.rapid_blink }
|
.rapid_blink { term.rapid_blink }
|
||||||
//.inverse { term.inverse }
|
.inverse { term.inverse }
|
||||||
//.hidden { term.hidden }
|
.hidden { term.hidden }
|
||||||
//.strikethrough { term.strikethrough }
|
.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)
|
fg: color.rgb(0, 0, 0)
|
||||||
bg: color.hex(0xffffff)
|
bg: color.hex(0xffffff)
|
||||||
styles: [color.bold, color.dim, color.italic]
|
styles: [color.bold, color.dim, color.italic]
|
||||||
)!
|
)
|
||||||
|
|
||||||
brush.println('Hello World')
|
brush.println('Hello World')
|
||||||
}
|
}
|
||||||
|
|
2
v.mod
2
v.mod
|
@ -1,7 +1,7 @@
|
||||||
Module {
|
Module {
|
||||||
name: 'color'
|
name: 'color'
|
||||||
description: 'An easier way to print colored text to the terminal.'
|
description: 'An easier way to print colored text to the terminal.'
|
||||||
version: '1.1.0'
|
version: '1.2.0'
|
||||||
license: 'MIT'
|
license: 'MIT'
|
||||||
dependencies: []
|
dependencies: []
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue