diff --git a/src/main.rs b/src/main.rs index fdc505816..9dc85d13e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ #![recursion_limit = "1024"] #![cfg_attr(feature = "fail-on-deprecated", deny(deprecated))] -use onefetch::{cli::Cli, cli_utils, error::*, info, repo}; +use onefetch::{cli::Cli, cli_utils, error::*, info, printer, repo}; use std::{io, process}; @@ -32,7 +32,7 @@ fn run() -> Result<()> { let info = info::Info::new(config)?; - let mut printer = cli_utils::Printer::new(io::BufWriter::new(io::stdout()), info); + let mut printer = printer::Printer::new(io::BufWriter::new(io::stdout()), info); printer.print()?; diff --git a/src/onefetch/cli_utils.rs b/src/onefetch/cli_utils.rs index 3659aac1c..e7a0873ac 100644 --- a/src/onefetch/cli_utils.rs +++ b/src/onefetch/cli_utils.rs @@ -1,91 +1,8 @@ -use crate::onefetch::{ - ascii_art::AsciiArt, deps::package_manager::PackageManager, error::*, info::Info, - language::Language, -}; -use colored::Color; +use crate::onefetch::{deps::package_manager::PackageManager, error::*, language::Language}; use std::env; -use std::io::Write; use std::process::{Command, Stdio}; use strum::IntoEnumIterator; -pub struct Printer { - writer: W, - info: Info, -} - -impl Printer { - pub fn new(writer: W, info: Info) -> Self { - Self { writer, info } - } - - pub fn print(&mut self) -> Result<()> { - let center_pad = " "; - let info_str = format!("{}", &self.info); - let mut info_lines = info_str.lines(); - let colors: Vec = Vec::new(); - let mut buf = String::new(); - - if self.info.config.art_off { - buf.push_str(&info_str); - } else if let Some(custom_image) = &self.info.config.image { - buf.push_str( - &self - .info - .config - .image_backend - .as_ref() - .unwrap() - .add_image( - info_lines.map(|s| format!("{}{}", center_pad, s)).collect(), - custom_image, - self.info.config.image_color_resolution, - ) - .chain_err(|| "Error while drawing image")?, - ); - } else { - let mut logo_lines = if let Some(custom_ascii) = &self.info.config.ascii_input { - AsciiArt::new(custom_ascii, &colors, !self.info.config.no_bold) - } else { - AsciiArt::new(self.get_ascii(), &self.info.ascii_colors, !self.info.config.no_bold) - }; - - loop { - match (logo_lines.next(), info_lines.next()) { - (Some(logo_line), Some(info_line)) => { - buf.push_str(&format!("{}{}{:^}\n", logo_line, center_pad, info_line)) - } - (Some(logo_line), None) => buf.push_str(&format!("{}\n", logo_line)), - (None, Some(info_line)) => buf.push_str(&format!( - "{: { - buf.push('\n'); - break; - } - } - } - } - - write!(self.writer, "{}", buf)?; - - Ok(()) - } - - fn get_ascii(&self) -> &str { - let language = if let Some(ascii_language) = &self.info.config.ascii_language { - ascii_language - } else { - &self.info.dominant_language - }; - - language.get_ascii_art() - } -} - pub fn print_supported_languages() -> Result<()> { for l in Language::iter() { println!("{}", l); diff --git a/src/onefetch/mod.rs b/src/onefetch/mod.rs index ebe7dc4a5..df22b3bf1 100644 --- a/src/onefetch/mod.rs +++ b/src/onefetch/mod.rs @@ -9,6 +9,7 @@ pub mod info; pub mod info_fields; pub mod language; pub mod license; +pub mod printer; pub mod repo; pub mod text_color; mod utils; diff --git a/src/onefetch/printer.rs b/src/onefetch/printer.rs new file mode 100644 index 000000000..ab2ea4e21 --- /dev/null +++ b/src/onefetch/printer.rs @@ -0,0 +1,81 @@ +use crate::onefetch::{ascii_art::AsciiArt, error::*, info::Info}; +use colored::Color; +use std::io::Write; + +pub struct Printer { + writer: W, + info: Info, +} + +impl Printer { + pub fn new(writer: W, info: Info) -> Self { + Self { writer, info } + } + + pub fn print(&mut self) -> Result<()> { + let center_pad = " "; + let info_str = format!("{}", &self.info); + let mut info_lines = info_str.lines(); + let colors: Vec = Vec::new(); + let mut buf = String::new(); + + if self.info.config.art_off { + buf.push_str(&info_str); + } else if let Some(custom_image) = &self.info.config.image { + buf.push_str( + &self + .info + .config + .image_backend + .as_ref() + .unwrap() + .add_image( + info_lines.map(|s| format!("{}{}", center_pad, s)).collect(), + custom_image, + self.info.config.image_color_resolution, + ) + .chain_err(|| "Error while drawing image")?, + ); + } else { + let mut logo_lines = if let Some(custom_ascii) = &self.info.config.ascii_input { + AsciiArt::new(custom_ascii, &colors, !self.info.config.no_bold) + } else { + AsciiArt::new(self.get_ascii(), &self.info.ascii_colors, !self.info.config.no_bold) + }; + + loop { + match (logo_lines.next(), info_lines.next()) { + (Some(logo_line), Some(info_line)) => { + buf.push_str(&format!("{}{}{:^}\n", logo_line, center_pad, info_line)) + } + (Some(logo_line), None) => buf.push_str(&format!("{}\n", logo_line)), + (None, Some(info_line)) => buf.push_str(&format!( + "{: { + buf.push('\n'); + break; + } + } + } + } + + write!(self.writer, "{}", buf)?; + + Ok(()) + } + + fn get_ascii(&self) -> &str { + let language = if let Some(ascii_language) = &self.info.config.ascii_language { + ascii_language + } else { + &self.info.dominant_language + }; + + language.get_ascii_art() + } +}