Skip to content

Commit

Permalink
move Printer into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Nov 11, 2020
1 parent 96e3a55 commit a87dc8f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()?;

Expand Down
85 changes: 1 addition & 84 deletions src/onefetch/cli_utils.rs
Original file line number Diff line number Diff line change
@@ -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<W> {
writer: W,
info: Info,
}

impl<W: Write> Printer<W> {
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<Color> = 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!(
"{:<width$}{}{:^}\n",
"",
center_pad,
info_line,
width = logo_lines.width()
)),
(None, None) => {
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);
Expand Down
1 change: 1 addition & 0 deletions src/onefetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
81 changes: 81 additions & 0 deletions src/onefetch/printer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::onefetch::{ascii_art::AsciiArt, error::*, info::Info};
use colored::Color;
use std::io::Write;

pub struct Printer<W> {
writer: W,
info: Info,
}

impl<W: Write> Printer<W> {
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<Color> = 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!(
"{:<width$}{}{:^}\n",
"",
center_pad,
info_line,
width = logo_lines.width()
)),
(None, None) => {
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()
}
}

0 comments on commit a87dc8f

Please sign in to comment.