added support for colouring th background instead of the foreground

This commit is contained in:
Djairo Hougee 2023-02-09 14:01:45 +01:00
parent c138c047ed
commit f4d7e60a07
3 changed files with 45 additions and 8 deletions

View File

@ -22,7 +22,14 @@ pub struct Cli {
/// Takes priority over '--grayscale' /// Takes priority over '--grayscale'
#[arg(short = 'C', long)] #[arg(short = 'C', long)]
pub colour: bool, pub colour: bool,
/// Colour the background instead of the foreground
///
/// Requires '--colour' or '--grayscale' to be passed
#[arg(long)]
pub background: bool,
/// Display ASCII art in grayscale /// Display ASCII art in grayscale
///
/// incompatible with '--color'
#[arg(short= 'g', long)] #[arg(short= 'g', long)]
pub grayscale: bool, pub grayscale: bool,
/// Use braille characters instead of ASCII /// Use braille characters instead of ASCII
@ -67,6 +74,7 @@ impl Cli {
pub fn debug_print(&self) { pub fn debug_print(&self) {
debug!("complex: {}", self.complex); debug!("complex: {}", self.complex);
debug!("colour: {}", self.colour); debug!("colour: {}", self.colour);
debug!("background: {}", self.background);
debug!("grayscale: {}", self.grayscale); debug!("grayscale: {}", self.grayscale);
debug!("braille: {}", self.braille); debug!("braille: {}", self.braille);
debug!("debug: {}", self.debug); debug!("debug: {}", self.debug);

View File

@ -14,12 +14,6 @@ mod ascii_manipulation;
mod output; mod output;
mod model_rgb_ascii; mod model_rgb_ascii;
//todo:
/* custom background color, print font in white
* --formats flag
* --version flag
*/
/// This is the main function. /// This is the main function.
/// ///
/// It drives the CLI module, calls the appropriate convert functions and directs output. /// It drives the CLI module, calls the appropriate convert functions and directs output.
@ -51,6 +45,10 @@ fn main() {
if let Some(output) = cli.output { if let Some(output) = cli.output {
print_file(out, output); print_file(out, output);
} else { } else {
print_terminal(out, cli.colour, cli.grayscale); if cli.background && (cli.colour || cli.grayscale) {
print_terminal_background(out, cli.colour, cli.grayscale);
} else {
print_terminal(out, cli.colour, cli.grayscale);
}
} }
} }

View File

@ -4,7 +4,7 @@ use std::io::{stdout, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use crossterm::execute; use crossterm::execute;
use crossterm::style::{Color, SetForegroundColor, Print, ResetColor}; use crossterm::style::{Color, SetForegroundColor, SetBackgroundColor, Print, ResetColor};
use log::error; use log::error;
use crate::model_rgb_ascii::Ascii; use crate::model_rgb_ascii::Ascii;
@ -13,6 +13,7 @@ use crate::model_rgb_ascii::Ascii;
/// arguments: /// arguments:
/// art: Vec<Vec<Ascii>> - vector of vectors representing the art /// art: Vec<Vec<Ascii>> - vector of vectors representing the art
/// in_colour: bool - whether to print in colour or as hard white /// in_colour: bool - whether to print in colour or as hard white
/// grayscale: bool - whether to print grayscale
pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) { pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) {
for line in art { for line in art {
for ascii in line { for ascii in line {
@ -34,6 +35,36 @@ pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) {
} }
} }
/// This function prints ASCII art to the background of stdout.
///
/// arguments:
/// art: Vec<Vec<Ascii>> - vector of vectors representing the art
/// in_colour: bool - whether to print in colour
/// grayscale: bool - whether to print grayscale
pub fn print_terminal_background(art: Vec<Vec<Ascii>>, in_colour: bool, grayscale: bool) {
for line in art {
for ascii in line {
if in_colour {
let _ = execute!(stdout(),
SetBackgroundColor(Color::Rgb {r: ascii.rgb[0], g: ascii.rgb[1], b: ascii.rgb[2]}),
SetForegroundColor(Color::Rgb {r: 255-ascii.rgb[0], g: 255-ascii.rgb[1], b: 255-ascii.rgb[2]}),
Print(ascii.char),
ResetColor);
} else if grayscale {
let _ = execute!(stdout(),
SetBackgroundColor(Color::Rgb {r: ascii.col_depth, g: ascii.col_depth, b: ascii.col_depth}),
SetForegroundColor(Color::Rgb {r: 255-ascii.col_depth, g: 255-ascii.col_depth, b: 255-ascii.col_depth}),
Print(ascii.char),
ResetColor);
} else {
error!("This should be unreachable!");
exit(1);
}
}
println!();
}
}
/// This function prints ASCII art to a file. /// This function prints ASCII art to a file.
/// ///
/// arguments: /// arguments: