diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..517d63e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.v] +indent_style = tab +indent_size = 4 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f4011a7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +* text=auto eol=lf +*.bat eol=crlf + +**/*.v linguist-language=V +**/*.vv linguist-language=V +**/*.vsh linguist-language=V +**/v.mod linguist-language=V diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f15bf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# Binaries for programs and plugins +main +VColor +*.exe +*.exe~ +*.so +*.dylib +*.dll + +# Ignore common editor/system specific metadata +.DS_Store +.idea/ +.vscode/ +*.iml diff --git a/README.md b/README.md new file mode 100644 index 0000000..b39fa6f --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# v-color + +An easier way to print color to the terminal + +# Example + +## Basic + +```v +import color + +println(color.red.apply('Hello World')) +println(color.bold.apply('Hello World')) +``` + +## Advanced + +```v +import color + +p := color.PaintBrush{ + fg: color.rgb(0, 0, 0)!, + bg: color.hex(0xffffff)!, + styles: [color.bold, color.underline, color.italic] +} + +print(p.apply('Hello World')) +``` diff --git a/color/basic_color.v b/color/basic_color.v new file mode 100644 index 0000000..43ccef8 --- /dev/null +++ b/color/basic_color.v @@ -0,0 +1,85 @@ +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 + green + yellow + blue + magenta + cyan + white + bright_black + bright_red + bright_green + bright_yellow + bright_blue + bright_magenta + bright_cyan + bright_white +} + +pub fn (c BasicColor) apply(msg string) string { + 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 } + } + return func(msg) +} + +pub fn (c BasicColor) apply_bg(msg string) string { + 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 } + } + return func(msg) +} diff --git a/color/color.v b/color/color.v new file mode 100644 index 0000000..258802e --- /dev/null +++ b/color/color.v @@ -0,0 +1,21 @@ +module color + +type Color = BasicColor | NoColor | TrueColor + +const no_color = NoColor{} + +struct NoColor {} + +fn (c Color) apply(msg string) string { + return match c { + NoColor { msg } + else { c.apply(msg) } + } +} + +fn (c Color) apply_bg(msg string) string { + return match c { + NoColor { msg } + else { c.apply_bg(msg) } + } +} diff --git a/color/paintbrush.v b/color/paintbrush.v new file mode 100644 index 0000000..d14b24f --- /dev/null +++ b/color/paintbrush.v @@ -0,0 +1,27 @@ +module color + +import term +import datatypes + +const can_show_color = term.can_show_color_on_stdout() + +pub struct PaintBrush { +pub: + fg Color = no_color + bg Color = no_color + styles []Style +} + +pub fn (p &PaintBrush) apply(msg string) string { + if !color.can_show_color { + return msg + } + + mut result := p.bg.apply(p.fg.apply(msg)) + + for style in p.styles { + result = style.apply(result) + } + + return result +} diff --git a/color/style.v b/color/style.v new file mode 100644 index 0000000..dd3d1de --- /dev/null +++ b/color/style.v @@ -0,0 +1,45 @@ +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 + dim + italic + underline + // slow_blink + // rapid_blink + inverse + hidden + strikethrough +} + +pub fn (s Style) apply(msg string) string { + 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 } + } + return func(msg) +} diff --git a/color/true_color.v b/color/true_color.v new file mode 100644 index 0000000..8864fea --- /dev/null +++ b/color/true_color.v @@ -0,0 +1,34 @@ +module color + +import term + +[noinit] +struct TrueColor { +pub: + r int [required] + g int [required] + b int [required] +} + +pub fn rgb(r int, g int, b int) !TrueColor { + 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') + } + return TrueColor{ + r: r + g: g + b: b + } +} + +pub fn hex(hex int) !TrueColor { + return rgb(hex >> 16, hex >> 8 & 0xFF, hex & 0xFF)! +} + +pub fn (c TrueColor) apply(msg string) string { + return term.rgb(c.r, c.g, c.b, msg) +} + +pub fn (c TrueColor) apply_bg(msg string) string { + return term.bg_rgb(c.r, c.g, c.b, msg) +} diff --git a/v.mod b/v.mod new file mode 100644 index 0000000..a5f9d17 --- /dev/null +++ b/v.mod @@ -0,0 +1,7 @@ +Module { + name: 'color' + description: 'An easier way to print color to the terminal' + version: '1.0.0' + license: 'MIT' + dependencies: [] +}