Skip to content

Commit

Permalink
fixed #58
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikwilkowski committed Jul 8, 2022
1 parent bd4910f commit 349cf1a
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 43 deletions.
1 change: 1 addition & 0 deletions nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Please look at the coding style and work with it, not against it ;)

## Release History

* 3.1.1 - Fixed #58 gradient color bug, added `gray` to gradient colors
* 3.1.0 - Added support for -V flag fallback
* 3.0.0 - Added rust library port, aligned APIs, added hex background colors, fixed minor alignment bugs, updated license from GPLv2 to GPLv3
* 2.10.1 - bumped dependencies
Expand Down
2 changes: 1 addition & 1 deletion nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cfonts",
"description": "Sexy ANSI fonts for the console",
"version": "3.1.0",
"version": "3.1.1",
"homepage": "https://github.com/dominikwilkowski/cfonts",
"author": {
"name": "Dominik Wilkowski",
Expand Down
4 changes: 2 additions & 2 deletions nodejs/src/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ function ansi_2562ansi_16(code, bg = false) {
(code >= 132 && code <= 135) ||
(code >= 139 && code <= 141) ||
(code >= 145 && code <= 147) ||
(code >= 196 && code <= 171) ||
(code >= 169 && code <= 171) ||
(code >= 175 && code <= 177)
) {
ansi_16_code = 35;
Expand All @@ -393,7 +393,7 @@ function ansi_2562ansi_16(code, bg = false) {
if (
(code >= 164 && code <= 165) ||
(code >= 182 && code <= 183) ||
(code >= 200 && code <= 200) ||
(code >= 200 && code <= 201) ||
(code >= 218 && code <= 219)
) {
ansi_16_code = 95;
Expand Down
2 changes: 2 additions & 0 deletions nodejs/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ const GRADIENTCOLORS = {
magenta: 'magenta',
cyan: 'cyan',
white: 'white',
gray: 'gray',
grey: 'grey',
};

const GRADIENTS = {
Expand Down
5 changes: 5 additions & 0 deletions nodejs/test/unit/Color.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ test(`Color - ansi_2562ansi_16 - Convert RGB to ANSI256 correctly`, () => {
expect(inspect(ansi_2562ansi_16(240))).toEqual(inspect('\x1B[90m'));
expect(inspect(ansi_2562ansi_16(247))).toEqual(inspect('\x1B[37m'));

for (let i = 0; i <= 255; i++) {
expect(`${inspect(ansi_2562ansi_16(i)) !== "'\\x1B[undefinedm'"}${i}`).toEqual(`true${i}`);
expect(`${inspect(ansi_2562ansi_16(i)) !== "'\\x1B[NaNdm'"}${i}`).toEqual(`true${i}`);
}

expect(inspect(ansi_2562ansi_16(52, true))).toEqual(inspect('\x1B[41m'));
});

Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cfonts"
version = "1.0.4"
version = "1.1.0"
edition = "2021"
authors = ["Dominik Wilkowski <Hi@Dominik-Wilkowski.com>"]
license = "GPL-3.0-or-later"
Expand Down
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ We also have an [end-to-end test script](https://github.com/dominikwilkowski/cfo

## Release History

* 1.1.0 - Added `yellow` to the gradient colors
* 1.0.4 - Fixed NO_COLOR not being respected in help, fixed color conversion rgb to ansi_256
* 1.0.3 - Fixed NO_COLOR support when run without FORCE_COLOR
* 1.0.2 - Fixed help and version flags in first position
Expand Down
3 changes: 2 additions & 1 deletion rust/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ pub fn parse(args: Vec<String>) -> Result<Options, String> {
"red" => Ok(String::from("#ff0000")),
"green" => Ok(String::from("#00ff00")),
"blue" => Ok(String::from("#0000ff")),
"yellow" => Ok(String::from("#ffff00")),
"magenta" => Ok(String::from("#ff00ff")),
"cyan" => Ok(String::from("#00ffff")),
"white" => Ok(String::from("#ffffff")),
Expand All @@ -419,7 +420,7 @@ pub fn parse(args: Vec<String>) -> Result<Options, String> {
// parsing hex round trip to make sure it's in a good format
Ok(rgb2hex(&hex2rgb(unknown, &options), &options))
} else {
Err(format!("The gradient color \"{}\" is not supported.\nAllowed options are: black, red, green, blue, magenta, cyan, white, gray, grey", color(unknown, Colors::Green)))
Err(format!("The gradient color \"{}\" is not supported.\nAllowed options are: black, red, green, blue, yellow, magenta, cyan, white, gray, grey", color(unknown, Colors::Green)))
}
}
}).collect::<Result<Vec<String>,String>>()?;
Expand Down
15 changes: 8 additions & 7 deletions rust/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The contents of this module is all about cli specific functionality
use std::env;
use std::fmt::Write as _;

use crate::color::{color, get_term_color_support, TermColorSupport};
use crate::config::{Align, BgColors, Colors, Env, Fonts, OptionType, Options, CLIOPTIONS};
Expand Down Expand Up @@ -61,19 +62,19 @@ pub fn help(options: &Options) -> String {
output += "This is a tool for sexy fonts in the console. Give your cli some love.\n";
output += "\n";
output += "Usage: cfonts \"<value>\" [option1] <input1> [option2] <input1>,<input2> [option3]\n";
output +=
&format!("Example: {}$ cfonts \"sexy font\" -f chrome -a center -c red,green,gray{}\n", bold_start, bold_end);
let _ =
writeln!(output, "Example: {}$ cfonts \"sexy font\" -f chrome -a center -c red,green,gray{}", bold_start, bold_end);
output += "\n";
output += "Options:\n";

for option in CLIOPTIONS {
output += &format!("\n{}{}, {}", bold_start, option.name, option.shortcut);
let _ = write!(output, "\n{}{}, {}", bold_start, option.name, option.shortcut);
if !option.fallback_shortcut.is_empty() {
output += &format!(", {}", option.fallback_shortcut);
let _ = write!(output, ", {}", option.fallback_shortcut);
}
output += &format!("{}\n", bold_end);
output += &format!("{}\n", option.description);
output += &format!("{}${} cfonts {}", bold_start, bold_end, option.example);
let _ = writeln!(output, "{}", bold_end);
let _ = writeln!(output, "{}", option.description);
let _ = write!(output, "{}${} cfonts {}", bold_start, bold_end, option.example);
match option.kind {
OptionType::Font => {
output += &color(&format!(" [ {} ]", Fonts::list()), Colors::Green).to_string();
Expand Down
4 changes: 2 additions & 2 deletions rust/tests/chars_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ mod chars {
];
options.colors = vec![Colors::Red, Colors::Blue];
output = vec![
String::from("\u{1b}[34mgreen\u{1b}[39m \u{1b}[31mred\u{1b}[39m blue \u{1b}[31mred\u{1b}[39m nothing"),
String::from("\x1B[34mgreen\x1B[39m \x1B[31mred\x1B[39m blue \x1B[31mred\x1B[39m nothing"),
String::from("color"),
String::from("nothing"),
String::from("\u{1b}[31mred\u{1b}[39m blue"),
String::from("\x1B[31mred\x1B[39m blue"),
];
assert_eq!(paint_letter(&letter, 4, &options), output);

Expand Down
30 changes: 12 additions & 18 deletions rust/tests/color_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,23 +467,20 @@ mod color {
});

temp_env::with_var("FORCE_COLOR", Some("1"), || {
assert_eq!(
color(" testing ", Colors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[91m testing \u{1b}[39m")
);
assert_eq!(color(" testing ", Colors::Rgb(Rgb::Val(243, 79, 168))), String::from("\x1B[91m testing \x1B[39m"));
});

temp_env::with_var("FORCE_COLOR", Some("2"), || {
assert_eq!(
color(" testing ", Colors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[38;5;211m testing \u{1b}[39m")
String::from("\x1B[38;5;211m testing \x1B[39m")
);
});

temp_env::with_var("FORCE_COLOR", Some("3"), || {
assert_eq!(
color(" testing ", Colors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[38;2;243;79;168m testing \u{1b}[39m")
String::from("\x1B[38;2;243;79;168m testing \x1B[39m")
);
});
}
Expand All @@ -497,21 +494,21 @@ mod color {
temp_env::with_var("FORCE_COLOR", Some("1"), || {
assert_eq!(
bg_color(" testing ", BgColors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[101m testing \u{1b}[49m")
String::from("\x1B[101m testing \x1B[49m")
);
});

temp_env::with_var("FORCE_COLOR", Some("2"), || {
assert_eq!(
bg_color(" testing ", BgColors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[48;5;211m testing \u{1b}[49m")
String::from("\x1B[48;5;211m testing \x1B[49m")
);
});

temp_env::with_var("FORCE_COLOR", Some("3"), || {
assert_eq!(
bg_color(" testing ", BgColors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[48;2;243;79;168m testing \u{1b}[49m")
String::from("\x1B[48;2;243;79;168m testing \x1B[49m")
);
});
}
Expand All @@ -523,23 +520,20 @@ mod color {
});

temp_env::with_vars(vec![("FORCE_COLOR", Some("1")), ("NO_COLOR", Some(""))], || {
assert_eq!(
color(" testing ", Colors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[91m testing \u{1b}[39m")
);
assert_eq!(color(" testing ", Colors::Rgb(Rgb::Val(243, 79, 168))), String::from("\x1B[91m testing \x1B[39m"));
});

temp_env::with_vars(vec![("FORCE_COLOR", Some("2")), ("NO_COLOR", Some(""))], || {
assert_eq!(
color(" testing ", Colors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[38;5;211m testing \u{1b}[39m")
String::from("\x1B[38;5;211m testing \x1B[39m")
);
});

temp_env::with_vars(vec![("FORCE_COLOR", Some("3")), ("NO_COLOR", Some(""))], || {
assert_eq!(
color(" testing ", Colors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[38;2;243;79;168m testing \u{1b}[39m")
String::from("\x1B[38;2;243;79;168m testing \x1B[39m")
);
});
}
Expand All @@ -553,21 +547,21 @@ mod color {
temp_env::with_vars(vec![("FORCE_COLOR", Some("1")), ("NO_COLOR", Some(""))], || {
assert_eq!(
bg_color(" testing ", BgColors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[101m testing \u{1b}[49m")
String::from("\x1B[101m testing \x1B[49m")
);
});

temp_env::with_vars(vec![("FORCE_COLOR", Some("2")), ("NO_COLOR", Some(""))], || {
assert_eq!(
bg_color(" testing ", BgColors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[48;5;211m testing \u{1b}[49m")
String::from("\x1B[48;5;211m testing \x1B[49m")
);
});

temp_env::with_vars(vec![("FORCE_COLOR", Some("3")), ("NO_COLOR", Some(""))], || {
assert_eq!(
bg_color(" testing ", BgColors::Rgb(Rgb::Val(243, 79, 168))),
String::from("\u{1b}[48;2;243;79;168m testing \u{1b}[49m")
String::from("\x1B[48;2;243;79;168m testing \x1B[49m")
);
});
}
Expand Down
18 changes: 9 additions & 9 deletions rust/tests/debug_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ mod tests {

d("test", 1, Dt::Head, &options, &mut output);
let mut stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, "\u{1b}[43m\n \u{1b}[30mtest\u{1b}[39m \u{1b}[49m\n");
assert_eq!(stdout, "\x1B[43m\n \x1B[30mtest\x1B[39m \x1B[49m\n");

d("test", 1, Dt::Log, &options, &mut output);
stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, "\u{1b}[43m\n \u{1b}[30mtest\u{1b}[39m \u{1b}[49m\n \u{1b}[33mtest\u{1b}[39m\n");
assert_eq!(stdout, "\x1B[43m\n \x1B[30mtest\x1B[39m \x1B[49m\n \x1B[33mtest\x1B[39m\n");

d("test", 1, Dt::Error, &options, &mut output);
stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, "\u{1b}[43m\n \u{1b}[30mtest\u{1b}[39m \u{1b}[49m\n \u{1b}[33mtest\u{1b}[39m\n\u{1b}[41m\u{1b}[37m ERROR \u{1b}[39m\u{1b}[49m \u{1b}[31mtest\u{1b}[39m\n");
assert_eq!(stdout, "\x1B[43m\n \x1B[30mtest\x1B[39m \x1B[49m\n \x1B[33mtest\x1B[39m\n\x1B[41m\x1B[37m ERROR \x1B[39m\x1B[49m \x1B[31mtest\x1B[39m\n");
});
}

Expand All @@ -69,7 +69,7 @@ mod tests {

d("test", 1, Dt::Log, &options, &mut output);
stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, " \u{1b}[33mtest\u{1b}[39m\n");
assert_eq!(stdout, " \x1B[33mtest\x1B[39m\n");
output = Vec::new();

options.debug_level = 2;
Expand All @@ -80,28 +80,28 @@ mod tests {

d("test", 2, Dt::Log, &options, &mut output);
stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, " \u{1b}[33mtest\u{1b}[39m\n");
assert_eq!(stdout, " \x1B[33mtest\x1B[39m\n");
output = Vec::new();

d("test", 1, Dt::Log, &options, &mut output);
stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, " \u{1b}[33mtest\u{1b}[39m\n");
assert_eq!(stdout, " \x1B[33mtest\x1B[39m\n");
output = Vec::new();

options.debug_level = 3;
d("test", 3, Dt::Log, &options, &mut output);
stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, " \u{1b}[33mtest\u{1b}[39m\n");
assert_eq!(stdout, " \x1B[33mtest\x1B[39m\n");
output = Vec::new();

d("test", 2, Dt::Log, &options, &mut output);
stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, " \u{1b}[33mtest\u{1b}[39m\n");
assert_eq!(stdout, " \x1B[33mtest\x1B[39m\n");
output = Vec::new();

d("test", 1, Dt::Log, &options, &mut output);
stdout = String::from_utf8(output.clone()).expect("Input not UTF-8");
assert_eq!(stdout, " \u{1b}[33mtest\u{1b}[39m\n");
assert_eq!(stdout, " \x1B[33mtest\x1B[39m\n");
});
}
}
Loading

0 comments on commit 349cf1a

Please sign in to comment.