From f4d7e60a07b628c283bf293aed853feded6cf306 Mon Sep 17 00:00:00 2001 From: djairoh Date: Thu, 9 Feb 2023 14:01:45 +0100 Subject: [PATCH] added support for colouring th background instead of the foreground --- src/cli.rs | 8 ++++++++ src/main.rs | 12 +++++------- src/output.rs | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 49802ee..27604c6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -22,7 +22,14 @@ pub struct Cli { /// Takes priority over '--grayscale' #[arg(short = 'C', long)] 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 + /// + /// incompatible with '--color' #[arg(short= 'g', long)] pub grayscale: bool, /// Use braille characters instead of ASCII @@ -67,6 +74,7 @@ impl Cli { pub fn debug_print(&self) { debug!("complex: {}", self.complex); debug!("colour: {}", self.colour); + debug!("background: {}", self.background); debug!("grayscale: {}", self.grayscale); debug!("braille: {}", self.braille); debug!("debug: {}", self.debug); diff --git a/src/main.rs b/src/main.rs index 9515a0b..aaf27d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,12 +14,6 @@ mod ascii_manipulation; mod output; mod model_rgb_ascii; -//todo: -/* custom background color, print font in white - * --formats flag - * --version flag - */ - /// This is the main function. /// /// 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 { print_file(out, output); } 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); + } } } diff --git a/src/output.rs b/src/output.rs index fd21317..d17922c 100644 --- a/src/output.rs +++ b/src/output.rs @@ -4,7 +4,7 @@ use std::io::{stdout, Write}; use std::path::PathBuf; use std::process::exit; use crossterm::execute; -use crossterm::style::{Color, SetForegroundColor, Print, ResetColor}; +use crossterm::style::{Color, SetForegroundColor, SetBackgroundColor, Print, ResetColor}; use log::error; use crate::model_rgb_ascii::Ascii; @@ -13,6 +13,7 @@ use crate::model_rgb_ascii::Ascii; /// arguments: /// art: Vec> - vector of vectors representing the art /// in_colour: bool - whether to print in colour or as hard white +/// grayscale: bool - whether to print grayscale pub fn print_terminal(art: Vec>, in_colour: bool, grayscale: bool) { for line in art { for ascii in line { @@ -34,6 +35,36 @@ pub fn print_terminal(art: Vec>, in_colour: bool, grayscale: bool) { } } +/// This function prints ASCII art to the background of stdout. +/// +/// arguments: +/// art: Vec> - 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>, 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. /// /// arguments: