Skip to content

Commit

Permalink
Merge pull request #76 from dominikwilkowski/line_height_console
Browse files Browse the repository at this point in the history
Line height console fix
  • Loading branch information
dominikwilkowski authored Feb 15, 2024
2 parents 8d213d9 + 3fdaa23 commit 126b2f0
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 183 deletions.
325 changes: 162 additions & 163 deletions rust/Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cfonts"
version = "1.1.2"
version = "1.1.3"
edition = "2021"
authors = ["Dominik Wilkowski <[email protected]>"]
license = "GPL-3.0-or-later"
Expand All @@ -21,9 +21,9 @@ enable-ansi-support = "0.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rand = "0.8.5"
terminal_size = "0.2.3"
terminal_size = "0.3"
supports-color = "2"

[dev-dependencies]
temp-env = "0.3.0"
assert_cmd = "2.0.4"
temp-env = "0.3.6"
assert_cmd = "2.0.13"
2 changes: 2 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ We also have an [end-to-end test script](https://github.com/dominikwilkowski/cfo

## Release History

* 1.1.3 - Fixed #75, bumped dependencies and fixed some clippy warnings
* 1.1.2 - Added `LICENSE` into the published version
* 1.1.1 - Updated dependencies, bumped `terminal_size` to `0.2.3`
* 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
Expand Down
9 changes: 9 additions & 0 deletions rust/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ pub fn parse(args: Vec<String>) -> Result<Options, String> {
options.debug = true;
}

// we check for the line_height flag to make sure we don't override it with the console font
let line_height_options = options_lookup.get("-z").unwrap();
let line_height_changed = my_args.contains(&line_height_options.name.to_string())
|| my_args.contains(&line_height_options.shortcut.to_string());

d("args::parse()", 1, Dt::Head, &options, &mut std::io::stdout());

if my_args.len() < 2 {
Expand Down Expand Up @@ -241,6 +246,10 @@ pub fn parse(args: Vec<String>) -> Result<Options, String> {
));
}
};

if options.font == Fonts::FontConsole && !line_height_changed {
options.line_height = 0;
}
}
OptionType::Align => {
i += 1;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub fn rgb2hex(rgb: &Rgb, options: &Options) -> String {
d(&format!("color::rgb2hex()\nrgb:{:?}", rgb), 5, Dt::Log, options, &mut std::io::stdout());

let (r, g, b) = rgb.get_value();
let result = format!("#{:0>2x}{:0>2x}{:0>2x}", r as u8, g as u8, b as u8);
let result = format!("#{:0>2x}{:0>2x}{:0>2x}", r, g, b);

d(&format!("color::rgb2hex() {:?} -> {:?}", rgb, result), 5, Dt::Log, options, &mut std::io::stdout());
result
Expand Down
8 changes: 4 additions & 4 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ pub struct Options {
pub debug_level: u16,
}

impl Options {
impl Default for Options {
/// The default values for each of the options so you don't have to pick each option every time
pub fn default() -> Self {
fn default() -> Self {
Options {
text: String::from(""),
font: Fonts::FontBlock,
Expand All @@ -306,7 +306,7 @@ impl Options {
}

/// The type of options our [`CLIOPTIONS`] can have
#[derive(Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OptionType {
/// There is only one text option which is for the text
Text,
Expand All @@ -329,7 +329,7 @@ pub enum OptionType {
}

/// The struct of a single option inside our [`CLIOPTIONS`]
#[derive(Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CliOption<'a> {
/// The key of this option so we can address it later
pub key: &'a str,
Expand Down
2 changes: 1 addition & 1 deletion rust/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub fn get_transition_steps(colors: &[String], steps: usize, options: &Options)
// steps left over to be distributed
let rest = (steps as i8 - (colors.len() as i8 + transition_steps * (colors.len() as i8 - 1))) as usize;
// the gaps array has one less items than our points (cause it's gaps between each of the points)
let mut gaps = vec![transition_steps as i8; colors.len() - 1];
let mut gaps = vec![transition_steps; colors.len() - 1];
let len = gaps.len();

for i in 0..rest {
Expand Down
12 changes: 2 additions & 10 deletions rust/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::chars::{
add_letter, add_line, add_line_height, align_last_line, get_letter_length, get_letter_space, paint_letter,
};
use crate::color::{bgcolor2hex, get_background_color};
use crate::config::{Align, BgColors, Env, Fonts, Options};
use crate::config::{Align, BgColors, Env, Options};
use crate::debug::{d, Dt};
use crate::font;
use crate::gradient::add_gradient_colors;
Expand Down Expand Up @@ -92,14 +92,6 @@ pub fn render(options: Options) -> RenderedString {
};
d(&format!("render()\nletter_spacing:{:?}", letter_spacing), 1, Dt::Log, &options, &mut std::io::stdout());

// the console font is special in that it has less line height space
let line_height = if options.font == Fonts::FontConsole && options.line_height > 0 {
options.line_height - 1
} else {
options.line_height
};
d(&format!("render()\nline_height:{:?}", line_height), 1, Dt::Log, &options, &mut std::io::stdout());

let letter_space = get_letter_space(&font.letterspace, letter_spacing, &options);
let letter_space_len = get_letter_length(&letter_space, font.colors, &options);
let painted_letter_space = paint_letter(&letter_space, font.colors, &options);
Expand Down Expand Up @@ -155,7 +147,7 @@ pub fn render(options: Options) -> RenderedString {
d("render() aligned last line", 1, Dt::Log, &options, &mut std::io::stdout());
add_line(&mut output, font.lines, &options);
d("render() added new line", 1, Dt::Log, &options, &mut std::io::stdout());
add_line_height(&mut output, line_height, &options);
add_line_height(&mut output, options.line_height, &options);
d("render() added line_height", 1, Dt::Log, &options, &mut std::io::stdout());
add_letter(&mut output, &font.buffer, &options);
d("render() added buffer", 1, Dt::Log, &options, &mut std::io::stdout());
Expand Down
28 changes: 28 additions & 0 deletions rust/tests/args_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ mod args {
);

options.font = Fonts::FontConsole;
options.line_height = 0;
assert_eq!(
parse(vec![
"path/to/bin".to_string(),
Expand All @@ -298,6 +299,33 @@ mod args {
.unwrap(),
options
);
options.line_height = 2;
assert_eq!(
parse(vec![
"path/to/bin".to_string(),
"my text".to_string(),
"-f".to_string(),
"Console".to_string(),
"-z".to_string(),
"2".to_string(),
])
.unwrap(),
options
);
options.line_height = 1;
assert_eq!(
parse(vec![
"path/to/bin".to_string(),
"my text".to_string(),
"-f".to_string(),
"Console".to_string(),
"-z".to_string(),
"1".to_string(),
])
.unwrap(),
options
);

options.font = Fonts::FontBlock;
assert_eq!(
parse(vec![
Expand Down
31 changes: 31 additions & 0 deletions rust/tests/end-to-end_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,37 @@ fn get_all_tests() -> Vec<Test> {
force_color: String::from("3"),
no_color: false,
},
Test {
name: String::from("Gradient with new line"),
args: vec!["test|x".to_string(), "-g".to_string(), "red,green".to_string()],
fixture: concat!("\n\n",
" \x1B[38;2;255;0;0m█\x1B[39m\x1B[38;2;255;0;28m█\x1B[39m\x1B[38;2;255;0;56m█\x1B[39m\x1B[38;2;255;0;84m█\x1B[39m\x1B[38;2;255;0;113m█\x1B[39m\x1B[38;2;255;0;141m█\x1B[39m\x1B[38;2;255;0;170m█\x1B[39m\x1B[38;2;255;0;198m█\x1B[39m\x1B[38;2;255;0;226m╗\x1B[39m\x1B[38;2;255;0;255m \x1B[39m\x1B[38;2;226;0;255m█\x1B[39m\x1B[38;2;198;0;255m█\x1B[39m\x1B[38;2;170;0;255m█\x1B[39m\x1B[38;2;141;0;255m█\x1B[39m\x1B[38;2;113;0;255m█\x1B[39m\x1B[38;2;84;0;255m█\x1B[39m\x1B[38;2;56;0;255m█\x1B[39m\x1B[38;2;28;0;255m╗\x1B[39m\x1B[38;2;0;0;255m \x1B[39m\x1B[38;2;0;28;255m█\x1B[39m\x1B[38;2;0;56;255m█\x1B[39m\x1B[38;2;0;85;255m█\x1B[39m\x1B[38;2;0;113;255m█\x1B[39m\x1B[38;2;0;141;255m█\x1B[39m\x1B[38;2;0;169;255m█\x1B[39m\x1B[38;2;0;198;255m█\x1B[39m\x1B[38;2;0;226;255m╗\x1B[39m\x1B[38;2;0;255;255m \x1B[39m\x1B[38;2;0;255;226m█\x1B[39m\x1B[38;2;0;255;198m█\x1B[39m\x1B[38;2;0;255;169m█\x1B[39m\x1B[38;2;0;255;141m█\x1B[39m\x1B[38;2;0;255;113m█\x1B[39m\x1B[38;2;0;255;85m█\x1B[39m\x1B[38;2;0;255;56m█\x1B[39m\x1B[38;2;0;255;28m█\x1B[39m\x1B[38;2;0;255;0m╗\x1B[39m\n",
" \x1B[38;2;255;0;0m╚\x1B[39m\x1B[38;2;255;0;28m═\x1B[39m\x1B[38;2;255;0;56m═\x1B[39m\x1B[38;2;255;0;84m█\x1B[39m\x1B[38;2;255;0;113m█\x1B[39m\x1B[38;2;255;0;141m╔\x1B[39m\x1B[38;2;255;0;170m═\x1B[39m\x1B[38;2;255;0;198m═\x1B[39m\x1B[38;2;255;0;226m╝\x1B[39m\x1B[38;2;255;0;255m \x1B[39m\x1B[38;2;226;0;255m█\x1B[39m\x1B[38;2;198;0;255m█\x1B[39m\x1B[38;2;170;0;255m╔\x1B[39m\x1B[38;2;141;0;255m═\x1B[39m\x1B[38;2;113;0;255m═\x1B[39m\x1B[38;2;84;0;255m═\x1B[39m\x1B[38;2;56;0;255m═\x1B[39m\x1B[38;2;28;0;255m╝\x1B[39m\x1B[38;2;0;0;255m \x1B[39m\x1B[38;2;0;28;255m█\x1B[39m\x1B[38;2;0;56;255m█\x1B[39m\x1B[38;2;0;85;255m╔\x1B[39m\x1B[38;2;0;113;255m═\x1B[39m\x1B[38;2;0;141;255m═\x1B[39m\x1B[38;2;0;169;255m═\x1B[39m\x1B[38;2;0;198;255m═\x1B[39m\x1B[38;2;0;226;255m╝\x1B[39m\x1B[38;2;0;255;255m \x1B[39m\x1B[38;2;0;255;226m╚\x1B[39m\x1B[38;2;0;255;198m═\x1B[39m\x1B[38;2;0;255;169m═\x1B[39m\x1B[38;2;0;255;141m█\x1B[39m\x1B[38;2;0;255;113m█\x1B[39m\x1B[38;2;0;255;85m╔\x1B[39m\x1B[38;2;0;255;56m═\x1B[39m\x1B[38;2;0;255;28m═\x1B[39m\x1B[38;2;0;255;0m╝\x1B[39m\n",
" \x1B[38;2;255;0;0m \x1B[39m\x1B[38;2;255;0;28m \x1B[39m\x1B[38;2;255;0;56m \x1B[39m\x1B[38;2;255;0;84m█\x1B[39m\x1B[38;2;255;0;113m█\x1B[39m\x1B[38;2;255;0;141m║\x1B[39m\x1B[38;2;255;0;170m \x1B[39m\x1B[38;2;255;0;198m \x1B[39m\x1B[38;2;255;0;226m \x1B[39m\x1B[38;2;255;0;255m \x1B[39m\x1B[38;2;226;0;255m█\x1B[39m\x1B[38;2;198;0;255m█\x1B[39m\x1B[38;2;170;0;255m█\x1B[39m\x1B[38;2;141;0;255m█\x1B[39m\x1B[38;2;113;0;255m█\x1B[39m\x1B[38;2;84;0;255m╗\x1B[39m\x1B[38;2;56;0;255m \x1B[39m\x1B[38;2;28;0;255m \x1B[39m\x1B[38;2;0;0;255m \x1B[39m\x1B[38;2;0;28;255m█\x1B[39m\x1B[38;2;0;56;255m█\x1B[39m\x1B[38;2;0;85;255m█\x1B[39m\x1B[38;2;0;113;255m█\x1B[39m\x1B[38;2;0;141;255m█\x1B[39m\x1B[38;2;0;169;255m█\x1B[39m\x1B[38;2;0;198;255m█\x1B[39m\x1B[38;2;0;226;255m╗\x1B[39m\x1B[38;2;0;255;255m \x1B[39m\x1B[38;2;0;255;226m \x1B[39m\x1B[38;2;0;255;198m \x1B[39m\x1B[38;2;0;255;169m \x1B[39m\x1B[38;2;0;255;141m█\x1B[39m\x1B[38;2;0;255;113m█\x1B[39m\x1B[38;2;0;255;85m║\x1B[39m\x1B[38;2;0;255;56m \x1B[39m\x1B[38;2;0;255;28m \x1B[39m\x1B[38;2;0;255;0m \x1B[39m\n",
" \x1B[38;2;255;0;0m \x1B[39m\x1B[38;2;255;0;28m \x1B[39m\x1B[38;2;255;0;56m \x1B[39m\x1B[38;2;255;0;84m█\x1B[39m\x1B[38;2;255;0;113m█\x1B[39m\x1B[38;2;255;0;141m║\x1B[39m\x1B[38;2;255;0;170m \x1B[39m\x1B[38;2;255;0;198m \x1B[39m\x1B[38;2;255;0;226m \x1B[39m\x1B[38;2;255;0;255m \x1B[39m\x1B[38;2;226;0;255m█\x1B[39m\x1B[38;2;198;0;255m█\x1B[39m\x1B[38;2;170;0;255m╔\x1B[39m\x1B[38;2;141;0;255m═\x1B[39m\x1B[38;2;113;0;255m═\x1B[39m\x1B[38;2;84;0;255m╝\x1B[39m\x1B[38;2;56;0;255m \x1B[39m\x1B[38;2;28;0;255m \x1B[39m\x1B[38;2;0;0;255m \x1B[39m\x1B[38;2;0;28;255m╚\x1B[39m\x1B[38;2;0;56;255m═\x1B[39m\x1B[38;2;0;85;255m═\x1B[39m\x1B[38;2;0;113;255m═\x1B[39m\x1B[38;2;0;141;255m═\x1B[39m\x1B[38;2;0;169;255m█\x1B[39m\x1B[38;2;0;198;255m█\x1B[39m\x1B[38;2;0;226;255m║\x1B[39m\x1B[38;2;0;255;255m \x1B[39m\x1B[38;2;0;255;226m \x1B[39m\x1B[38;2;0;255;198m \x1B[39m\x1B[38;2;0;255;169m \x1B[39m\x1B[38;2;0;255;141m█\x1B[39m\x1B[38;2;0;255;113m█\x1B[39m\x1B[38;2;0;255;85m║\x1B[39m\x1B[38;2;0;255;56m \x1B[39m\x1B[38;2;0;255;28m \x1B[39m\x1B[38;2;0;255;0m \x1B[39m\n",
" \x1B[38;2;255;0;0m \x1B[39m\x1B[38;2;255;0;28m \x1B[39m\x1B[38;2;255;0;56m \x1B[39m\x1B[38;2;255;0;84m█\x1B[39m\x1B[38;2;255;0;113m█\x1B[39m\x1B[38;2;255;0;141m║\x1B[39m\x1B[38;2;255;0;170m \x1B[39m\x1B[38;2;255;0;198m \x1B[39m\x1B[38;2;255;0;226m \x1B[39m\x1B[38;2;255;0;255m \x1B[39m\x1B[38;2;226;0;255m█\x1B[39m\x1B[38;2;198;0;255m█\x1B[39m\x1B[38;2;170;0;255m█\x1B[39m\x1B[38;2;141;0;255m█\x1B[39m\x1B[38;2;113;0;255m█\x1B[39m\x1B[38;2;84;0;255m█\x1B[39m\x1B[38;2;56;0;255m█\x1B[39m\x1B[38;2;28;0;255m╗\x1B[39m\x1B[38;2;0;0;255m \x1B[39m\x1B[38;2;0;28;255m█\x1B[39m\x1B[38;2;0;56;255m█\x1B[39m\x1B[38;2;0;85;255m█\x1B[39m\x1B[38;2;0;113;255m█\x1B[39m\x1B[38;2;0;141;255m█\x1B[39m\x1B[38;2;0;169;255m█\x1B[39m\x1B[38;2;0;198;255m█\x1B[39m\x1B[38;2;0;226;255m║\x1B[39m\x1B[38;2;0;255;255m \x1B[39m\x1B[38;2;0;255;226m \x1B[39m\x1B[38;2;0;255;198m \x1B[39m\x1B[38;2;0;255;169m \x1B[39m\x1B[38;2;0;255;141m█\x1B[39m\x1B[38;2;0;255;113m█\x1B[39m\x1B[38;2;0;255;85m║\x1B[39m\x1B[38;2;0;255;56m \x1B[39m\x1B[38;2;0;255;28m \x1B[39m\x1B[38;2;0;255;0m \x1B[39m\n",
" \x1B[38;2;255;0;0m \x1B[39m\x1B[38;2;255;0;28m \x1B[39m\x1B[38;2;255;0;56m \x1B[39m\x1B[38;2;255;0;84m╚\x1B[39m\x1B[38;2;255;0;113m═\x1B[39m\x1B[38;2;255;0;141m╝\x1B[39m\x1B[38;2;255;0;170m \x1B[39m\x1B[38;2;255;0;198m \x1B[39m\x1B[38;2;255;0;226m \x1B[39m\x1B[38;2;255;0;255m \x1B[39m\x1B[38;2;226;0;255m╚\x1B[39m\x1B[38;2;198;0;255m═\x1B[39m\x1B[38;2;170;0;255m═\x1B[39m\x1B[38;2;141;0;255m═\x1B[39m\x1B[38;2;113;0;255m═\x1B[39m\x1B[38;2;84;0;255m═\x1B[39m\x1B[38;2;56;0;255m═\x1B[39m\x1B[38;2;28;0;255m╝\x1B[39m\x1B[38;2;0;0;255m \x1B[39m\x1B[38;2;0;28;255m╚\x1B[39m\x1B[38;2;0;56;255m═\x1B[39m\x1B[38;2;0;85;255m═\x1B[39m\x1B[38;2;0;113;255m═\x1B[39m\x1B[38;2;0;141;255m═\x1B[39m\x1B[38;2;0;169;255m═\x1B[39m\x1B[38;2;0;198;255m═\x1B[39m\x1B[38;2;0;226;255m╝\x1B[39m\x1B[38;2;0;255;255m \x1B[39m\x1B[38;2;0;255;226m \x1B[39m\x1B[38;2;0;255;198m \x1B[39m\x1B[38;2;0;255;169m \x1B[39m\x1B[38;2;0;255;141m╚\x1B[39m\x1B[38;2;0;255;113m═\x1B[39m\x1B[38;2;0;255;85m╝\x1B[39m\x1B[38;2;0;255;56m \x1B[39m\x1B[38;2;0;255;28m \x1B[39m\x1B[38;2;0;255;0m \x1B[39m\n",
"\n",
" \x1B[38;2;255;0;0m█\x1B[39m\x1B[38;2;255;0;28m█\x1B[39m\x1B[38;2;255;0;56m╗\x1B[39m\x1B[38;2;255;0;84m \x1B[39m\x1B[38;2;255;0;113m \x1B[39m\x1B[38;2;255;0;141m█\x1B[39m\x1B[38;2;255;0;170m█\x1B[39m\x1B[38;2;255;0;198m╗\x1B[39m\n",
" \x1B[38;2;255;0;0m╚\x1B[39m\x1B[38;2;255;0;28m█\x1B[39m\x1B[38;2;255;0;56m█\x1B[39m\x1B[38;2;255;0;84m╗\x1B[39m\x1B[38;2;255;0;113m█\x1B[39m\x1B[38;2;255;0;141m█\x1B[39m\x1B[38;2;255;0;170m╔\x1B[39m\x1B[38;2;255;0;198m╝\x1B[39m\n",
" \x1B[38;2;255;0;0m \x1B[39m\x1B[38;2;255;0;28m╚\x1B[39m\x1B[38;2;255;0;56m█\x1B[39m\x1B[38;2;255;0;84m█\x1B[39m\x1B[38;2;255;0;113m█\x1B[39m\x1B[38;2;255;0;141m╔\x1B[39m\x1B[38;2;255;0;170m╝\x1B[39m\x1B[38;2;255;0;198m \x1B[39m\n",
" \x1B[38;2;255;0;0m \x1B[39m\x1B[38;2;255;0;28m█\x1B[39m\x1B[38;2;255;0;56m█\x1B[39m\x1B[38;2;255;0;84m╔\x1B[39m\x1B[38;2;255;0;113m█\x1B[39m\x1B[38;2;255;0;141m█\x1B[39m\x1B[38;2;255;0;170m╗\x1B[39m\x1B[38;2;255;0;198m \x1B[39m\n",
" \x1B[38;2;255;0;0m█\x1B[39m\x1B[38;2;255;0;28m█\x1B[39m\x1B[38;2;255;0;56m╔\x1B[39m\x1B[38;2;255;0;84m╝\x1B[39m\x1B[38;2;255;0;113m \x1B[39m\x1B[38;2;255;0;141m█\x1B[39m\x1B[38;2;255;0;170m█\x1B[39m\x1B[38;2;255;0;198m╗\x1B[39m\n",
" \x1B[38;2;255;0;0m╚\x1B[39m\x1B[38;2;255;0;28m═\x1B[39m\x1B[38;2;255;0;56m╝\x1B[39m\x1B[38;2;255;0;84m \x1B[39m\x1B[38;2;255;0;113m \x1B[39m\x1B[38;2;255;0;141m╚\x1B[39m\x1B[38;2;255;0;170m═\x1B[39m\x1B[38;2;255;0;198m╝\x1B[39m\n",
"\n\n").to_string(),
force_color: String::from("3"),
no_color: false,
},
Test {
name: String::from("Gradient with new line and console font"),
args: vec!["test|x".to_string(), "-g".to_string(), "red,green".to_string(), "-f".to_string(), "console".to_string()],
fixture: concat!("\n\n",
"\x1B[38;2;255;0;0mt\x1B[39m\x1B[38;2;170;0;255me\x1B[39m\x1B[38;2;0;169;255ms\x1B[39m\x1B[38;2;0;255;0mt\x1B[39m\n",
"\x1B[38;2;255;0;0mx\x1B[39m\n",
"\n\n").to_string(),
force_color: String::from("3"),
no_color: false,
},
Test {
name: String::from("Gradient independent"),
args: vec!["test|x".to_string(), "-g".to_string(), "red,green".to_string(), "-i".to_string()],
Expand Down

0 comments on commit 126b2f0

Please sign in to comment.