Skip to content

Commit a87dc8f

Browse files
committed
move Printer into its own file
1 parent 96e3a55 commit a87dc8f

File tree

4 files changed

+85
-86
lines changed

4 files changed

+85
-86
lines changed

Diff for: src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![recursion_limit = "1024"]
33
#![cfg_attr(feature = "fail-on-deprecated", deny(deprecated))]
44

5-
use onefetch::{cli::Cli, cli_utils, error::*, info, repo};
5+
use onefetch::{cli::Cli, cli_utils, error::*, info, printer, repo};
66

77
use std::{io, process};
88

@@ -32,7 +32,7 @@ fn run() -> Result<()> {
3232

3333
let info = info::Info::new(config)?;
3434

35-
let mut printer = cli_utils::Printer::new(io::BufWriter::new(io::stdout()), info);
35+
let mut printer = printer::Printer::new(io::BufWriter::new(io::stdout()), info);
3636

3737
printer.print()?;
3838

Diff for: src/onefetch/cli_utils.rs

+1-84
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,8 @@
1-
use crate::onefetch::{
2-
ascii_art::AsciiArt, deps::package_manager::PackageManager, error::*, info::Info,
3-
language::Language,
4-
};
5-
use colored::Color;
1+
use crate::onefetch::{deps::package_manager::PackageManager, error::*, language::Language};
62
use std::env;
7-
use std::io::Write;
83
use std::process::{Command, Stdio};
94
use strum::IntoEnumIterator;
105

11-
pub struct Printer<W> {
12-
writer: W,
13-
info: Info,
14-
}
15-
16-
impl<W: Write> Printer<W> {
17-
pub fn new(writer: W, info: Info) -> Self {
18-
Self { writer, info }
19-
}
20-
21-
pub fn print(&mut self) -> Result<()> {
22-
let center_pad = " ";
23-
let info_str = format!("{}", &self.info);
24-
let mut info_lines = info_str.lines();
25-
let colors: Vec<Color> = Vec::new();
26-
let mut buf = String::new();
27-
28-
if self.info.config.art_off {
29-
buf.push_str(&info_str);
30-
} else if let Some(custom_image) = &self.info.config.image {
31-
buf.push_str(
32-
&self
33-
.info
34-
.config
35-
.image_backend
36-
.as_ref()
37-
.unwrap()
38-
.add_image(
39-
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
40-
custom_image,
41-
self.info.config.image_color_resolution,
42-
)
43-
.chain_err(|| "Error while drawing image")?,
44-
);
45-
} else {
46-
let mut logo_lines = if let Some(custom_ascii) = &self.info.config.ascii_input {
47-
AsciiArt::new(custom_ascii, &colors, !self.info.config.no_bold)
48-
} else {
49-
AsciiArt::new(self.get_ascii(), &self.info.ascii_colors, !self.info.config.no_bold)
50-
};
51-
52-
loop {
53-
match (logo_lines.next(), info_lines.next()) {
54-
(Some(logo_line), Some(info_line)) => {
55-
buf.push_str(&format!("{}{}{:^}\n", logo_line, center_pad, info_line))
56-
}
57-
(Some(logo_line), None) => buf.push_str(&format!("{}\n", logo_line)),
58-
(None, Some(info_line)) => buf.push_str(&format!(
59-
"{:<width$}{}{:^}\n",
60-
"",
61-
center_pad,
62-
info_line,
63-
width = logo_lines.width()
64-
)),
65-
(None, None) => {
66-
buf.push('\n');
67-
break;
68-
}
69-
}
70-
}
71-
}
72-
73-
write!(self.writer, "{}", buf)?;
74-
75-
Ok(())
76-
}
77-
78-
fn get_ascii(&self) -> &str {
79-
let language = if let Some(ascii_language) = &self.info.config.ascii_language {
80-
ascii_language
81-
} else {
82-
&self.info.dominant_language
83-
};
84-
85-
language.get_ascii_art()
86-
}
87-
}
88-
896
pub fn print_supported_languages() -> Result<()> {
907
for l in Language::iter() {
918
println!("{}", l);

Diff for: src/onefetch/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod info;
99
pub mod info_fields;
1010
pub mod language;
1111
pub mod license;
12+
pub mod printer;
1213
pub mod repo;
1314
pub mod text_color;
1415
mod utils;

Diff for: src/onefetch/printer.rs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use crate::onefetch::{ascii_art::AsciiArt, error::*, info::Info};
2+
use colored::Color;
3+
use std::io::Write;
4+
5+
pub struct Printer<W> {
6+
writer: W,
7+
info: Info,
8+
}
9+
10+
impl<W: Write> Printer<W> {
11+
pub fn new(writer: W, info: Info) -> Self {
12+
Self { writer, info }
13+
}
14+
15+
pub fn print(&mut self) -> Result<()> {
16+
let center_pad = " ";
17+
let info_str = format!("{}", &self.info);
18+
let mut info_lines = info_str.lines();
19+
let colors: Vec<Color> = Vec::new();
20+
let mut buf = String::new();
21+
22+
if self.info.config.art_off {
23+
buf.push_str(&info_str);
24+
} else if let Some(custom_image) = &self.info.config.image {
25+
buf.push_str(
26+
&self
27+
.info
28+
.config
29+
.image_backend
30+
.as_ref()
31+
.unwrap()
32+
.add_image(
33+
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
34+
custom_image,
35+
self.info.config.image_color_resolution,
36+
)
37+
.chain_err(|| "Error while drawing image")?,
38+
);
39+
} else {
40+
let mut logo_lines = if let Some(custom_ascii) = &self.info.config.ascii_input {
41+
AsciiArt::new(custom_ascii, &colors, !self.info.config.no_bold)
42+
} else {
43+
AsciiArt::new(self.get_ascii(), &self.info.ascii_colors, !self.info.config.no_bold)
44+
};
45+
46+
loop {
47+
match (logo_lines.next(), info_lines.next()) {
48+
(Some(logo_line), Some(info_line)) => {
49+
buf.push_str(&format!("{}{}{:^}\n", logo_line, center_pad, info_line))
50+
}
51+
(Some(logo_line), None) => buf.push_str(&format!("{}\n", logo_line)),
52+
(None, Some(info_line)) => buf.push_str(&format!(
53+
"{:<width$}{}{:^}\n",
54+
"",
55+
center_pad,
56+
info_line,
57+
width = logo_lines.width()
58+
)),
59+
(None, None) => {
60+
buf.push('\n');
61+
break;
62+
}
63+
}
64+
}
65+
}
66+
67+
write!(self.writer, "{}", buf)?;
68+
69+
Ok(())
70+
}
71+
72+
fn get_ascii(&self) -> &str {
73+
let language = if let Some(ascii_language) = &self.info.config.ascii_language {
74+
ascii_language
75+
} else {
76+
&self.info.dominant_language
77+
};
78+
79+
language.get_ascii_art()
80+
}
81+
}

0 commit comments

Comments
 (0)