Skip to content

Commit

Permalink
Merge pull request #48 from dominikwilkowski/tests
Browse files Browse the repository at this point in the history
added some more tests for rust, fixed two bugs
  • Loading branch information
dominikwilkowski authored Jun 5, 2022
2 parents 9f9f6ba + c82f04f commit 444ca7d
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 29 deletions.
115 changes: 114 additions & 1 deletion rust/Cargo.lock

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

3 changes: 2 additions & 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.3"
version = "1.0.4"
edition = "2021"
authors = ["Dominik Wilkowski <Hi@Dominik-Wilkowski.com>"]
license = "GPL-3.0-or-later"
Expand All @@ -26,3 +26,4 @@ supports-color = "1"

[dev-dependencies]
temp-env = "0.2.0"
assert_cmd = "2.0.4"
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ We also have an [end-to-end test script](./tests/end-to-end/index.js) setup that

## Release History

* 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
* 1.0.1 - Fixed font loading
Expand Down
19 changes: 14 additions & 5 deletions rust/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! The contents of this module is all about cli specific functionality
use crate::color::color;
use std::env;

use crate::color::{color, get_term_color_support, TermColorSupport};
use crate::config::{Align, BgColors, Colors, Env, Fonts, OptionType, Options, CLIOPTIONS};
use crate::debug::{d, Dt};
use crate::render::render;
Expand Down Expand Up @@ -47,24 +49,31 @@ pub fn help(options: &Options) -> String {
..Options::default()
});

let (bold_start, bold_end) = if get_term_color_support() == TermColorSupport::NoColor {
(String::from(""), String::from(""))
} else {
(String::from("\x1b[1m"), String::from("\x1b[22m"))
};

output += "\n\n";
output += &render_options.text;
output += "\n\n";
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 += "Example: \x1b[1m$ cfonts \"sexy font\" -f chrome -a center -c red,green,gray\x1b[22m\n";
output +=
&format!("Example: {}$ cfonts \"sexy font\" -f chrome -a center -c red,green,gray{}\n", bold_start, bold_end);
output += "\n";
output += "Options:\n";

for option in CLIOPTIONS {
output += &format!("\n\x1b[1m{}, {}", option.name, option.shortcut);
output += &format!("\n{}{}, {}", bold_start, option.name, option.shortcut);
if !option.fallback_shortcut.is_empty() {
output += &format!(", {}", option.fallback_shortcut);
}
output += "\x1b[22m\n";
output += &format!("{}\n", bold_end);
output += &format!("{}\n", option.description);
output += &format!("\x1b[1m$\x1b[22m cfonts {}", option.example);
output += &format!("{}${} cfonts {}", bold_start, bold_end, option.example);
match option.kind {
OptionType::Font => {
output += &color(&format!(" [ {} ]", Fonts::list()), Colors::Green).to_string();
Expand Down
14 changes: 7 additions & 7 deletions rust/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ pub fn rgb2ansi_16m(rgb: &Rgb, layer: ColorLayer) -> String {
/// use cfonts::color::{rgb_u8_2ansi_256,};
///
/// assert_eq!(rgb_u8_2ansi_256(100, 200, 100), 114);
/// assert_eq!(rgb_u8_2ansi_256(255, 255, 255), 16);
/// assert_eq!(rgb_u8_2ansi_256(0, 0, 0), 231);
/// assert_eq!(rgb_u8_2ansi_256(255, 255, 255), 231);
/// assert_eq!(rgb_u8_2ansi_256(0, 0, 0), 16);
/// assert_eq!(rgb_u8_2ansi_256(167, 5, 98), 126);
/// ```
pub fn rgb_u8_2ansi_256(r: u8, g: u8, b: u8) -> u8 {
Expand All @@ -521,7 +521,7 @@ pub fn rgb_u8_2ansi_256(r: u8, g: u8, b: u8) -> u8 {
let blue = b as f64;

if r == g && g == b {
if red > 8.0 {
if red < 8.0 {
return 16;
}
if red > 248.0 {
Expand All @@ -548,12 +548,12 @@ pub fn rgb_u8_2ansi_256(r: u8, g: u8, b: u8) -> u8 {
///
/// assert_eq!(rgb2ansi_256(&Rgb::Val(255, 0, 0), ColorLayer::Foreground), "\x1b[38;5;196m".to_string());
/// assert_eq!(rgb2ansi_256(&Rgb::Val(255, 255, 0), ColorLayer::Foreground), "\x1b[38;5;226m".to_string());
/// assert_eq!(rgb2ansi_256(&Rgb::Val(255, 255, 255), ColorLayer::Foreground), "\x1b[38;5;16m".to_string());
/// assert_eq!(rgb2ansi_256(&Rgb::Val(255, 255, 255), ColorLayer::Foreground), "\x1b[38;5;231m".to_string());
/// assert_eq!(rgb2ansi_256(&Rgb::Val(157, 5, 98), ColorLayer::Foreground), "\x1b[38;5;126m".to_string());
///
/// assert_eq!(rgb2ansi_256(&Rgb::Val(255, 0, 0), ColorLayer::Background), "\x1b[48;5;196m".to_string());
/// assert_eq!(rgb2ansi_256(&Rgb::Val(255, 255, 0), ColorLayer::Background), "\x1b[48;5;226m".to_string());
/// assert_eq!(rgb2ansi_256(&Rgb::Val(255, 255, 255), ColorLayer::Background), "\x1b[48;5;16m".to_string());
/// assert_eq!(rgb2ansi_256(&Rgb::Val(255, 255, 255), ColorLayer::Background), "\x1b[48;5;231m".to_string());
/// assert_eq!(rgb2ansi_256(&Rgb::Val(157, 5, 98), ColorLayer::Background), "\x1b[48;5;126m".to_string());
/// ```
pub fn rgb2ansi_256(rgb: &Rgb, layer: ColorLayer) -> String {
Expand All @@ -578,12 +578,12 @@ pub fn rgb2ansi_256(rgb: &Rgb, layer: ColorLayer) -> String {
///
/// assert_eq!(rgb2ansi_16(&Rgb::Val(255, 0, 0), ColorLayer::Foreground), "\x1b[91m".to_string());
/// assert_eq!(rgb2ansi_16(&Rgb::Val(255, 255, 0), ColorLayer::Foreground), "\x1b[93m".to_string());
/// assert_eq!(rgb2ansi_16(&Rgb::Val(255, 255, 255), ColorLayer::Foreground), "\x1b[0m".to_string());
/// assert_eq!(rgb2ansi_16(&Rgb::Val(255, 255, 255), ColorLayer::Foreground), "\x1b[97m".to_string());
/// assert_eq!(rgb2ansi_16(&Rgb::Val(157, 5, 98), ColorLayer::Foreground), "\x1b[31m".to_string());
///
/// assert_eq!(rgb2ansi_16(&Rgb::Val(255, 0, 0), ColorLayer::Background), "\x1b[101m".to_string());
/// assert_eq!(rgb2ansi_16(&Rgb::Val(255, 255, 0), ColorLayer::Background), "\x1b[103m".to_string());
/// assert_eq!(rgb2ansi_16(&Rgb::Val(255, 255, 255), ColorLayer::Background), "\x1b[10m".to_string());
/// assert_eq!(rgb2ansi_16(&Rgb::Val(255, 255, 255), ColorLayer::Background), "\x1b[107m".to_string());
/// assert_eq!(rgb2ansi_16(&Rgb::Val(157, 5, 98), ColorLayer::Background), "\x1b[41m".to_string());
/// ```
pub fn rgb2ansi_16(rgb: &Rgb, layer: ColorLayer) -> String {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::config::{Colors, Fonts, Options};
use crate::debug::{d, Dt};

/// The shape of our font data
#[derive(Debug, Deserialize)]
#[derive(Deserialize)]
pub struct Font {
/// The name of our font
pub name: String,
Expand Down
Loading

0 comments on commit 444ca7d

Please sign in to comment.