Skip to content

Commit 59a2033

Browse files
committed
refacto info.rs by adding printer
1 parent fab30cc commit 59a2033

File tree

7 files changed

+155
-118
lines changed

7 files changed

+155
-118
lines changed

Diff for: src/main.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// `error_chain!` can recurse deeply
22
#![recursion_limit = "1024"]
33

4-
use onefetch::{cli::Cli, error::*, info};
4+
use onefetch::{cli::Cli, cli_utils, error::*, info};
55

66
use {
77
process::{Command, Stdio},
8-
std::process,
8+
std::{io, process},
99
};
1010

1111
mod onefetch;
@@ -22,12 +22,15 @@ fn run() -> Result<()> {
2222
let options = Cli::new()?;
2323

2424
if options.print_languages {
25-
return Cli::print_supported_languages();
25+
return cli_utils::print_supported_languages();
2626
}
2727

2828
let info = info::Info::new(options)?;
2929

30-
print!("{}", info);
30+
let mut printer = cli_utils::Printer::new(io::BufWriter::new(io::stdout()), info);
31+
32+
printer.print()?;
33+
3134
Ok(())
3235
}
3336

@@ -38,7 +41,7 @@ fn main() {
3841
process::exit(0);
3942
}
4043
Err(error) => {
41-
let stderr = std::io::stderr();
44+
let stderr = io::stderr();
4245
default_error_handler(&error, &mut stderr.lock());
4346
process::exit(1);
4447
}

Diff for: src/onefetch/ascii_art.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use colored::{Color, Colorize};
22

33
pub struct AsciiArt<'a> {
44
content: Box<dyn 'a + Iterator<Item = &'a str>>,
5-
colors: Vec<Color>,
5+
colors: &'a [Color],
66
bold: bool,
77
start: usize,
88
end: usize,
99
}
1010
impl<'a> AsciiArt<'a> {
11-
pub fn new(input: &'a str, colors: Vec<Color>, bold: bool) -> AsciiArt<'a> {
11+
pub fn new(input: &'a str, colors: &'a [Color], bold: bool) -> AsciiArt<'a> {
1212
let mut lines: Vec<_> = input.lines().skip_while(|line| line.is_empty()).collect();
1313
while let Some(line) = lines.last() {
1414
if Tokens(line).is_empty() {

Diff for: src/onefetch/cli.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use {
22
crate::onefetch::{
3-
error::*, image_backends, info_fields, info_fields::InfoFields, language::Language,
3+
cli_utils,
4+
error::*,
5+
image_backends,
6+
info_fields::{self, InfoFields},
7+
language::Language,
48
},
59
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
610
image::DynamicImage,
@@ -178,7 +182,7 @@ impl Cli {
178182
let no_merges = matches.is_present("no-merge-commits");
179183
let no_color_blocks = matches.is_present("no-color-blocks");
180184
let print_languages = matches.is_present("languages");
181-
let true_color = is_truecolor_terminal();
185+
let true_color = cli_utils::is_truecolor_terminal();
182186

183187
let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields")
184188
{
@@ -250,20 +254,4 @@ impl Cli {
250254
true_color,
251255
})
252256
}
253-
254-
pub fn print_supported_languages() -> Result<()> {
255-
let iterator = Language::iter().filter(|x| *x != Language::Unknown);
256-
257-
for l in iterator {
258-
println!("{}", l);
259-
}
260-
261-
Ok(())
262-
}
263-
}
264-
265-
fn is_truecolor_terminal() -> bool {
266-
env::var("COLORTERM")
267-
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
268-
.unwrap_or(false)
269257
}

Diff for: src/onefetch/cli_utils.rs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use crate::onefetch::{ascii_art::AsciiArt, error::*, info::Info, language::Language};
2+
use colored::Color;
3+
use std::env;
4+
use std::io::Write;
5+
use strum::IntoEnumIterator;
6+
7+
pub struct Printer<W> {
8+
writer: W,
9+
info: Info,
10+
}
11+
12+
impl<W: Write> Printer<W> {
13+
pub fn new(writer: W, info: Info) -> Self {
14+
Self { writer, info }
15+
}
16+
17+
pub fn print(&mut self) -> Result<()> {
18+
let center_pad = " ";
19+
let info_str = format!("{}", &self.info);
20+
let mut info_lines = info_str.lines();
21+
let colors: Vec<Color> = Vec::new();
22+
let mut buf = String::new();
23+
if let Some(custom_image) = &self.info.config.image {
24+
if let Some(image_backend) = &self.info.config.image_backend {
25+
buf.push_str(&image_backend.add_image(
26+
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
27+
custom_image,
28+
));
29+
} else {
30+
panic!("No image backend found")
31+
}
32+
} else {
33+
let mut logo_lines = if let Some(custom_ascii) = &self.info.config.ascii_input {
34+
AsciiArt::new(custom_ascii, &colors, !self.info.config.no_bold)
35+
} else {
36+
AsciiArt::new(
37+
self.get_ascii(),
38+
&self.info.colors,
39+
!self.info.config.no_bold,
40+
)
41+
};
42+
43+
loop {
44+
match (logo_lines.next(), info_lines.next()) {
45+
(Some(logo_line), Some(info_line)) => {
46+
buf.push_str(&format!("{}{}{:^}\n", logo_line, center_pad, info_line))
47+
}
48+
(Some(logo_line), None) => buf.push_str(&format!("{}\n", logo_line)),
49+
(None, Some(info_line)) => buf.push_str(&format!(
50+
"{:<width$}{}{:^}\n",
51+
"",
52+
center_pad,
53+
info_line,
54+
width = logo_lines.width()
55+
)),
56+
(None, None) => {
57+
buf.push_str("\n");
58+
break;
59+
}
60+
}
61+
}
62+
}
63+
64+
writeln!(self.writer, "{}", buf)?;
65+
66+
Ok(())
67+
}
68+
69+
fn get_ascii(&self) -> &str {
70+
let language = if let Language::Unknown = self.info.config.ascii_language {
71+
&self.info.dominant_language
72+
} else {
73+
&self.info.config.ascii_language
74+
};
75+
76+
language.get_ascii_art()
77+
}
78+
}
79+
80+
pub fn print_supported_languages() -> Result<()> {
81+
let iterator = Language::iter().filter(|x| *x != Language::Unknown);
82+
83+
for l in iterator {
84+
println!("{}", l);
85+
}
86+
87+
Ok(())
88+
}
89+
90+
pub fn is_truecolor_terminal() -> bool {
91+
env::var("COLORTERM")
92+
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
93+
.unwrap_or(false)
94+
}

0 commit comments

Comments
 (0)