Skip to content

Commit 3bf51c5

Browse files
committedOct 23, 2020
Merge branch 'master' into disable-ascii-art
2 parents f705b01 + 0b37765 commit 3bf51c5

File tree

7 files changed

+167
-130
lines changed

7 files changed

+167
-130
lines changed
 

‎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
}

‎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() {

‎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,
@@ -186,7 +190,7 @@ impl Cli {
186190
let no_color_blocks = matches.is_present("no-color-blocks");
187191
let print_languages = matches.is_present("languages");
188192
let art_off = matches.is_present("off");
189-
let true_color = is_truecolor_terminal();
193+
let true_color = cli_utils::is_truecolor_terminal();
190194

191195
let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields")
192196
{
@@ -259,20 +263,4 @@ impl Cli {
259263
art_off,
260264
})
261265
}
262-
263-
pub fn print_supported_languages() -> Result<()> {
264-
let iterator = Language::iter().filter(|x| *x != Language::Unknown);
265-
266-
for l in iterator {
267-
println!("{}", l);
268-
}
269-
270-
Ok(())
271-
}
272-
}
273-
274-
fn is_truecolor_terminal() -> bool {
275-
env::var("COLORTERM")
276-
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
277-
.unwrap_or(false)
278266
}

‎src/onefetch/cli_utils.rs

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
24+
if !self.info.config.art_off {
25+
if let Some(custom_image) = &self.info.config.image {
26+
if let Some(image_backend) = &self.info.config.image_backend {
27+
buf.push_str(&image_backend.add_image(
28+
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
29+
custom_image,
30+
));
31+
} else {
32+
panic!("No image backend found")
33+
}
34+
} else {
35+
let mut logo_lines = if let Some(custom_ascii) = &self.info.config.ascii_input {
36+
AsciiArt::new(custom_ascii, &colors, !self.info.config.no_bold)
37+
} else {
38+
AsciiArt::new(
39+
self.get_ascii(),
40+
&self.info.colors,
41+
!self.info.config.no_bold,
42+
)
43+
};
44+
45+
loop {
46+
match (logo_lines.next(), info_lines.next()) {
47+
(Some(logo_line), Some(info_line)) => {
48+
buf.push_str(&format!("{}{}{:^}\n", logo_line, center_pad, info_line))
49+
}
50+
(Some(logo_line), None) => buf.push_str(&format!("{}\n", logo_line)),
51+
(None, Some(info_line)) => buf.push_str(&format!(
52+
"{:<width$}{}{:^}\n",
53+
"",
54+
center_pad,
55+
info_line,
56+
width = logo_lines.width()
57+
)),
58+
(None, None) => {
59+
buf.push('\n');
60+
break;
61+
}
62+
}
63+
}
64+
}
65+
}
66+
writeln!(self.writer, "{}", buf)?;
67+
68+
Ok(())
69+
}
70+
71+
fn get_ascii(&self) -> &str {
72+
let language = if let Language::Unknown = self.info.config.ascii_language {
73+
&self.info.dominant_language
74+
} else {
75+
&self.info.config.ascii_language
76+
};
77+
78+
language.get_ascii_art()
79+
}
80+
}
81+
82+
pub fn print_supported_languages() -> Result<()> {
83+
let iterator = Language::iter().filter(|x| *x != Language::Unknown);
84+
85+
for l in iterator {
86+
println!("{}", l);
87+
}
88+
89+
Ok(())
90+
}
91+
92+
pub fn is_truecolor_terminal() -> bool {
93+
env::var("COLORTERM")
94+
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
95+
.unwrap_or(false)
96+
}

‎src/onefetch/info.rs

+43-94
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use {
22
crate::onefetch::{
3-
ascii_art::AsciiArt, cli::Cli, commit_info::CommitInfo, error::*, language::Language,
4-
license::Detector,
3+
cli::Cli, commit_info::CommitInfo, error::*, language::Language, license::Detector,
54
},
65
colored::{Color, ColoredString, Colorize},
76
git2::Repository,
87
regex::Regex,
9-
std::fmt::Write,
108
tokio::process::Command,
119
};
1210

@@ -17,7 +15,7 @@ pub struct Info {
1715
current_commit: CommitInfo,
1816
version: String,
1917
creation_date: String,
20-
dominant_language: Language,
18+
pub dominant_language: Language,
2119
languages: Vec<(Language, f64)>,
2220
authors: Vec<(String, usize, usize)>,
2321
last_change: String,
@@ -29,13 +27,13 @@ pub struct Info {
2927
number_of_tags: usize,
3028
number_of_branches: usize,
3129
license: String,
32-
config: Cli,
30+
pub colors: Vec<Color>,
31+
pub config: Cli,
3332
}
3433

3534
impl std::fmt::Display for Info {
3635
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
37-
let mut buf = String::new();
38-
let color = match self.colors().get(0) {
36+
let color = match self.colors.get(0) {
3937
Some(&c) => c,
4038
None => Color::White,
4139
};
@@ -44,24 +42,20 @@ impl std::fmt::Display for Info {
4442
if self.git_username != "" {
4543
git_info_length = self.git_username.len() + self.git_version.len() + 3;
4644
write!(
47-
&mut buf,
45+
f,
4846
"{} ~ ",
4947
&self.get_formatted_info_label(&self.git_username, color)
5048
)?;
5149
} else {
5250
git_info_length = self.git_version.len();
5351
}
5452
write_buf(
55-
&mut buf,
53+
f,
5654
&self.get_formatted_info_label(&self.git_version, color),
5755
"",
5856
)?;
5957
let separator = "-".repeat(git_info_length);
60-
write_buf(
61-
&mut buf,
62-
&self.get_formatted_info_label("", color),
63-
&separator,
64-
)?;
58+
write_buf(f, &self.get_formatted_info_label("", color), &separator)?;
6559
}
6660
if !self.config.disabled_fields.project {
6761
let branches_str = match self.number_of_branches {
@@ -87,39 +81,39 @@ impl std::fmt::Display for Info {
8781
let project_str = &self.get_formatted_info_label("Project: ", color);
8882

8983
writeln!(
90-
buf,
84+
f,
9185
"{}{} {}",
9286
project_str, self.project_name, branches_tags_str
9387
)?;
9488
}
9589

9690
if !self.config.disabled_fields.head {
9791
write_buf(
98-
&mut buf,
92+
f,
9993
&self.get_formatted_info_label("HEAD: ", color),
10094
&self.current_commit,
10195
)?;
10296
}
10397

10498
if !self.config.disabled_fields.pending && self.pending != "" {
10599
write_buf(
106-
&mut buf,
100+
f,
107101
&self.get_formatted_info_label("Pending: ", color),
108102
&self.pending,
109103
)?;
110104
}
111105

112106
if !self.config.disabled_fields.version {
113107
write_buf(
114-
&mut buf,
108+
f,
115109
&self.get_formatted_info_label("Version: ", color),
116110
&self.version,
117111
)?;
118112
}
119113

120114
if !self.config.disabled_fields.created {
121115
write_buf(
122-
&mut buf,
116+
f,
123117
&self.get_formatted_info_label("Created: ", color),
124118
&self.creation_date,
125119
)?;
@@ -150,10 +144,10 @@ impl std::fmt::Display for Info {
150144
s = s + &format!("{} ({} %) ", language.0, formatted_number);
151145
}
152146
}
153-
writeln!(buf, "{}{}", &self.get_formatted_info_label(title, color), s)?;
147+
writeln!(f, "{}{}", &self.get_formatted_info_label(title, color), s)?;
154148
} else {
155149
write_buf(
156-
&mut buf,
150+
f,
157151
&self.get_formatted_info_label("Language: ", color),
158152
&self.dominant_language,
159153
)?;
@@ -168,7 +162,7 @@ impl std::fmt::Display for Info {
168162
};
169163

170164
writeln!(
171-
buf,
165+
f,
172166
"{}{}% {} {}",
173167
&self.get_formatted_info_label(title, color),
174168
self.authors[0].2,
@@ -180,7 +174,7 @@ impl std::fmt::Display for Info {
180174

181175
for author in self.authors.iter().skip(1) {
182176
writeln!(
183-
buf,
177+
f,
184178
"{}{}% {} {}",
185179
&self.get_formatted_info_label(&title, color),
186180
author.2,
@@ -192,55 +186,55 @@ impl std::fmt::Display for Info {
192186

193187
if !self.config.disabled_fields.last_change {
194188
write_buf(
195-
&mut buf,
189+
f,
196190
&self.get_formatted_info_label("Last change: ", color),
197191
&self.last_change,
198192
)?;
199193
}
200194

201195
if !self.config.disabled_fields.repo {
202196
write_buf(
203-
&mut buf,
197+
f,
204198
&self.get_formatted_info_label("Repo: ", color),
205199
&self.repo_url,
206200
)?;
207201
}
208202

209203
if !self.config.disabled_fields.commits {
210204
write_buf(
211-
&mut buf,
205+
f,
212206
&self.get_formatted_info_label("Commits: ", color),
213207
&self.commits,
214208
)?;
215209
}
216210

217211
if !self.config.disabled_fields.lines_of_code {
218212
write_buf(
219-
&mut buf,
213+
f,
220214
&self.get_formatted_info_label("Lines of code: ", color),
221215
&self.number_of_lines,
222216
)?;
223217
}
224218

225219
if !self.config.disabled_fields.size {
226220
write_buf(
227-
&mut buf,
221+
f,
228222
&self.get_formatted_info_label("Size: ", color),
229223
&self.repo_size,
230224
)?;
231225
}
232226

233227
if !self.config.disabled_fields.license {
234228
write_buf(
235-
&mut buf,
229+
f,
236230
&self.get_formatted_info_label("License: ", color),
237231
&self.license,
238232
)?;
239233
}
240234

241235
if !self.config.no_color_blocks {
242236
writeln!(
243-
buf,
237+
f,
244238
"\n{0}{1}{2}{3}{4}{5}{6}{7}",
245239
" ".on_black(),
246240
" ".on_red(),
@@ -253,53 +247,6 @@ impl std::fmt::Display for Info {
253247
)?;
254248
}
255249

256-
let center_pad = " ";
257-
let mut info_lines = buf.lines();
258-
259-
if self.config.art_off {
260-
writeln!(f, "{}", buf)?;
261-
} else if let Some(custom_image) = &self.config.image {
262-
if let Some(image_backend) = &self.config.image_backend {
263-
writeln!(
264-
f,
265-
"{}",
266-
image_backend.add_image(
267-
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
268-
custom_image
269-
)
270-
)?;
271-
} else {
272-
panic!("No image backend found")
273-
}
274-
} else {
275-
let mut logo_lines = if let Some(custom_ascii) = &self.config.ascii_input {
276-
AsciiArt::new(custom_ascii, Vec::new(), !self.config.no_bold)
277-
} else {
278-
AsciiArt::new(self.get_ascii(), self.colors(), !self.config.no_bold)
279-
};
280-
281-
loop {
282-
match (logo_lines.next(), info_lines.next()) {
283-
(Some(logo_line), Some(info_line)) => {
284-
writeln!(f, "{}{}{:^}", logo_line, center_pad, info_line)?
285-
}
286-
(Some(logo_line), None) => writeln!(f, "{}", logo_line)?,
287-
(None, Some(info_line)) => writeln!(
288-
f,
289-
"{:<width$}{}{:^}",
290-
"",
291-
center_pad,
292-
info_line,
293-
width = logo_lines.width()
294-
)?,
295-
(None, None) => {
296-
writeln!(f, "\n")?;
297-
break;
298-
}
299-
}
300-
}
301-
}
302-
303250
Ok(())
304251
}
305252
}
@@ -340,6 +287,12 @@ impl Info {
340287
let last_change = Info::get_date_of_last_commit(&git_history);
341288
let project_license = Detector::new()?.get_project_license(workdir_str);
342289
let dominant_language = Language::get_dominant_language(&languages_stats);
290+
let colors = Info::get_colors(
291+
&config.ascii_language,
292+
&dominant_language,
293+
&config.ascii_colors,
294+
config.true_color,
295+
);
343296

344297
Ok(Info {
345298
git_version: git_v,
@@ -360,6 +313,7 @@ impl Info {
360313
number_of_tags,
361314
number_of_branches,
362315
license: project_license?,
316+
colors,
363317
config,
364318
})
365319
}
@@ -690,30 +644,25 @@ impl Info {
690644
}
691645
}
692646

693-
fn get_ascii(&self) -> &str {
694-
let language = if let Language::Unknown = self.config.ascii_language {
695-
&self.dominant_language
696-
} else {
697-
&self.config.ascii_language
698-
};
699-
700-
language.get_ascii_art()
701-
}
702-
703-
fn colors(&self) -> Vec<Color> {
704-
let language = if let Language::Unknown = self.config.ascii_language {
705-
&self.dominant_language
647+
fn get_colors(
648+
ascii_language: &Language,
649+
dominant_language: &Language,
650+
ascii_colors: &[String],
651+
true_color: bool,
652+
) -> Vec<Color> {
653+
let language = if let Language::Unknown = ascii_language {
654+
&dominant_language
706655
} else {
707-
&self.config.ascii_language
656+
&ascii_language
708657
};
709658

710-
let colors = language.get_colors(self.config.true_color);
659+
let colors = language.get_colors(true_color);
711660

712661
let colors: Vec<Color> = colors
713662
.iter()
714663
.enumerate()
715664
.map(|(index, default_color)| {
716-
if let Some(color_num) = self.config.ascii_colors.get(index) {
665+
if let Some(color_num) = ascii_colors.get(index) {
717666
if let Some(color) = Info::num_to_color(color_num) {
718667
return color;
719668
}
@@ -749,7 +698,7 @@ impl Info {
749698
}
750699

751700
fn write_buf<T: std::fmt::Display>(
752-
buffer: &mut String,
701+
buffer: &mut std::fmt::Formatter,
753702
title: &ColoredString,
754703
content: T,
755704
) -> std::fmt::Result {

‎src/onefetch/language.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,16 @@ define_languages! {
231231
Color::Red,
232232
Color::Red
233233
] : [
234-
Color::TrueColor{ r:248, g:129, b:052 },
235-
Color::TrueColor{ r:249, g:119, b:050 },
236-
Color::TrueColor{ r:249, g:109, b:048 },
237-
Color::TrueColor{ r:250, g:099, b:046 },
238-
Color::TrueColor{ r:250, g:089, b:044 },
239-
Color::TrueColor{ r:251, g:080, b:042 },
240-
Color::TrueColor{ r:251, g:070, b:040 },
241-
Color::TrueColor{ r:252, g:060, b:038 },
242-
Color::TrueColor{ r:252, g:050, b:036 },
243-
Color::TrueColor{ r:253, g:040, b:034 }
234+
Color::TrueColor{ r:248, g:129, b:52 },
235+
Color::TrueColor{ r:249, g:119, b:50 },
236+
Color::TrueColor{ r:249, g:109, b:48 },
237+
Color::TrueColor{ r:250, g:99, b:46 },
238+
Color::TrueColor{ r:250, g:89, b:44 },
239+
Color::TrueColor{ r:251, g:80, b:42 },
240+
Color::TrueColor{ r:251, g:70, b:40 },
241+
Color::TrueColor{ r:252, g:60, b:38 },
242+
Color::TrueColor{ r:252, g:50, b:36 },
243+
Color::TrueColor{ r:253, g:40, b:34 }
244244
] )
245245
},
246246
{ Tcl, "tcl.ascii", "Tcl", define_colors!( [Color::Blue, Color::White, Color::Cyan] ) },
@@ -249,7 +249,7 @@ define_languages! {
249249
{ Vue, "vue.ascii", "Vue", define_colors!( [Color::Green, Color::Blue] ) },
250250
{ Xml, "xml.ascii", "XML", define_colors!( [Color::Yellow, Color::White, Color::Green] ) },
251251
{ Zig, "zig.ascii", "Zig", define_colors!( [Color::Yellow] ) },
252-
{ Zsh, "zsh.ascii", "Z-shell", define_colors!( [Color::White] ) },
252+
{ Zsh, "zsh.ascii", "Zsh", define_colors!( [Color::White] ) },
253253
}
254254

255255
impl Language {

‎src/onefetch/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod ascii_art;
22
pub mod cli;
3+
pub mod cli_utils;
34
pub mod commit_info;
45
pub mod error;
56
pub mod image_backends;

0 commit comments

Comments
 (0)
Please sign in to comment.