Skip to content

Commit 5e8867a

Browse files
committed
refacto info::display
1 parent 6197db5 commit 5e8867a

File tree

2 files changed

+138
-126
lines changed

2 files changed

+138
-126
lines changed

Diff for: src/onefetch/info.rs

+137-125
Original file line numberDiff line numberDiff line change
@@ -36,51 +36,23 @@ pub struct Info {
3636
impl std::fmt::Display for Info {
3737
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3838
if !self.config.disabled_fields.git_info {
39-
let git_info_length;
40-
if self.git_username != "" {
41-
git_info_length = self.git_username.len() + self.git_version.len() + 3;
42-
write!(
43-
f,
44-
"{} {} ",
45-
&self.get_formatted_info_label(&self.git_username, self.color_set.title),
46-
&self.get_formatted_info_label("~", self.color_set.tilde),
47-
)?;
48-
} else {
49-
git_info_length = self.git_version.len();
50-
}
51-
write_buf(
39+
let git_info_length = self.git_username.len() + self.git_version.len() + 3;
40+
41+
writeln!(
5242
f,
53-
&self.get_formatted_info_label(&self.git_version, self.color_set.title),
54-
"",
43+
"{} {} {}",
44+
&self.bold(&self.git_username).color(self.color_set.title),
45+
&self.bold("~").color(self.color_set.tilde),
46+
&self.bold(&self.git_version).color(self.color_set.title)
5547
)?;
48+
5649
let separator = "-".repeat(git_info_length);
57-
write_buf(
58-
f,
59-
&self.get_formatted_info_label("", self.color_set.underline),
60-
&self.get_formatted_info_label(&separator, self.color_set.underline),
61-
)?;
50+
51+
writeln!(f, "{}", separator.color(self.color_set.underline),)?;
6252
}
6353

6454
if !self.config.disabled_fields.project {
65-
let branches_str = match self.number_of_branches {
66-
0 => String::new(),
67-
1 => String::from("1 branch"),
68-
_ => format!("{} branches", self.number_of_branches),
69-
};
70-
71-
let tags_str = match self.number_of_tags {
72-
0 => String::new(),
73-
1 => String::from("1 tag"),
74-
_ => format!("{} tags", self.number_of_tags),
75-
};
76-
77-
let branches_tags_str = if tags_str.is_empty() && branches_str.is_empty() {
78-
String::new()
79-
} else if branches_str.is_empty() || tags_str.is_empty() {
80-
format!("({})", format!("{}{}", tags_str, branches_str))
81-
} else {
82-
format!("({}, {})", branches_str, tags_str)
83-
};
55+
let branches_tags_str = self.get_branches_and_tags_field();
8456

8557
let project_str = &self.get_formatted_subtitle_label(
8658
"Project",
@@ -98,8 +70,9 @@ impl std::fmt::Display for Info {
9870
}
9971

10072
if !self.config.disabled_fields.head {
101-
write_buf(
73+
writeln!(
10274
f,
75+
"{}{}",
10376
&self.get_formatted_subtitle_label(
10477
"HEAD",
10578
self.color_set.subtitle,
@@ -109,9 +82,10 @@ impl std::fmt::Display for Info {
10982
)?;
11083
}
11184

112-
if !self.config.disabled_fields.pending && self.pending != "" {
113-
write_buf(
85+
if !self.config.disabled_fields.pending && !self.pending.is_empty() {
86+
writeln!(
11487
f,
88+
"{}{}",
11589
&self.get_formatted_subtitle_label(
11690
"Pending",
11791
self.color_set.subtitle,
@@ -122,8 +96,9 @@ impl std::fmt::Display for Info {
12296
}
12397

12498
if !self.config.disabled_fields.version {
125-
write_buf(
99+
writeln!(
126100
f,
101+
"{}{}",
127102
&self.get_formatted_subtitle_label(
128103
"Version",
129104
self.color_set.subtitle,
@@ -134,8 +109,9 @@ impl std::fmt::Display for Info {
134109
}
135110

136111
if !self.config.disabled_fields.created {
137-
write_buf(
112+
writeln!(
138113
f,
114+
"{}{}",
139115
&self.get_formatted_subtitle_label(
140116
"Created",
141117
self.color_set.subtitle,
@@ -147,52 +123,22 @@ impl std::fmt::Display for Info {
147123

148124
if !self.config.disabled_fields.languages && !self.languages.is_empty() {
149125
if self.languages.len() > 1 {
150-
let title = "Languages";
151-
let pad = " ".repeat(title.len() + 2);
152-
let mut s = String::from("");
153-
let languages: Vec<(String, f64)> = {
154-
let mut iter = self.languages.iter().map(|x| (format!("{}", x.0), x.1));
155-
if self.languages.len() > 6 {
156-
let mut languages = iter.by_ref().take(6).collect::<Vec<_>>();
157-
let other_sum = iter.fold(0.0, |acc, x| acc + x.1);
158-
languages.push(("Other".to_owned(), other_sum));
159-
languages
160-
} else {
161-
iter.collect()
162-
}
163-
};
164-
165-
for (cnt, language) in languages.iter().enumerate() {
166-
let formatted_number =
167-
format!("{:.*}", 1, language.1).color(self.color_set.info);
168-
if cnt != 0 && cnt % 2 == 0 {
169-
s = s + &format!(
170-
"\n{}{} ({} %) ",
171-
pad,
172-
language.0.color(self.color_set.info),
173-
formatted_number
174-
);
175-
} else {
176-
s = s + &format!(
177-
"{} ({} %) ",
178-
language.0.color(self.color_set.info),
179-
formatted_number
180-
);
181-
}
182-
}
126+
let languages_str = self.get_languages_field();
127+
183128
writeln!(
184129
f,
185130
"{}{}",
186131
&self.get_formatted_subtitle_label(
187-
title,
132+
"Languages",
188133
self.color_set.subtitle,
189134
self.color_set.colon,
190135
),
191-
s.color(self.color_set.info)
136+
languages_str.color(self.color_set.info)
192137
)?;
193138
} else {
194-
write_buf(
139+
writeln!(
195140
f,
141+
"{}{}",
196142
&self.get_formatted_subtitle_label(
197143
"Language",
198144
self.color_set.subtitle,
@@ -213,38 +159,24 @@ impl std::fmt::Display for Info {
213159
"Author"
214160
};
215161

216-
writeln!(
162+
let author_str = self.get_author_field(title);
163+
164+
write!(
217165
f,
218-
"{}{}{} {} {}",
166+
"{}{}",
219167
&self.get_formatted_subtitle_label(
220168
title,
221169
self.color_set.subtitle,
222-
self.color_set.colon
170+
self.color_set.colon,
223171
),
224-
self.authors[0].2.to_string().color(self.color_set.info),
225-
"%".color(self.color_set.info),
226-
self.authors[0].0.to_string().color(self.color_set.info),
227-
self.authors[0].1.to_string().color(self.color_set.info)
172+
author_str.color(self.color_set.info),
228173
)?;
229-
230-
let title = " ".repeat(title.len() + 2);
231-
232-
for author in self.authors.iter().skip(1) {
233-
writeln!(
234-
f,
235-
"{}{}{} {} {}",
236-
self.get_formatted_info_label(&title, self.color_set.subtitle),
237-
author.2.to_string().color(self.color_set.info),
238-
"%".color(self.color_set.info),
239-
author.0.to_string().color(self.color_set.info),
240-
author.1.to_string().color(self.color_set.info)
241-
)?;
242-
}
243174
}
244175

245176
if !self.config.disabled_fields.last_change {
246-
write_buf(
177+
writeln!(
247178
f,
179+
"{}{}",
248180
&self.get_formatted_subtitle_label(
249181
"Last change",
250182
self.color_set.subtitle,
@@ -255,8 +187,9 @@ impl std::fmt::Display for Info {
255187
}
256188

257189
if !self.config.disabled_fields.repo {
258-
write_buf(
190+
writeln!(
259191
f,
192+
"{}{}",
260193
&self.get_formatted_subtitle_label(
261194
"Repo",
262195
self.color_set.subtitle,
@@ -267,8 +200,9 @@ impl std::fmt::Display for Info {
267200
}
268201

269202
if !self.config.disabled_fields.commits {
270-
write_buf(
203+
writeln!(
271204
f,
205+
"{}{}",
272206
&self.get_formatted_subtitle_label(
273207
"Commits",
274208
self.color_set.subtitle,
@@ -279,8 +213,9 @@ impl std::fmt::Display for Info {
279213
}
280214

281215
if !self.config.disabled_fields.lines_of_code {
282-
write_buf(
216+
writeln!(
283217
f,
218+
"{} {}",
284219
&self.get_formatted_subtitle_label(
285220
"Lines of code",
286221
self.color_set.subtitle,
@@ -291,8 +226,9 @@ impl std::fmt::Display for Info {
291226
}
292227

293228
if !self.config.disabled_fields.size {
294-
write_buf(
229+
writeln!(
295230
f,
231+
"{}{}",
296232
&self.get_formatted_subtitle_label(
297233
"Size",
298234
self.color_set.subtitle,
@@ -303,8 +239,9 @@ impl std::fmt::Display for Info {
303239
}
304240

305241
if !self.config.disabled_fields.license {
306-
write_buf(
242+
writeln!(
307243
f,
244+
"{}{}",
308245
&self.get_formatted_subtitle_label(
309246
"License",
310247
self.color_set.subtitle,
@@ -719,15 +656,6 @@ impl Info {
719656
Ok(output)
720657
}
721658

722-
fn get_formatted_info_label(&self, label: &str, color: Color) -> ColoredString {
723-
let formatted_label = label.color(color);
724-
if self.config.no_bold {
725-
formatted_label
726-
} else {
727-
formatted_label.bold()
728-
}
729-
}
730-
731659
fn get_colors(
732660
ascii_language: &Language,
733661
dominant_language: &Language,
@@ -787,18 +715,102 @@ impl Info {
787715
colon_clr: Color,
788716
) -> ColoredString {
789717
let formatted_label = format!("{}{} ", label.color(color), ":".color(colon_clr));
718+
self.bold(&formatted_label)
719+
}
720+
721+
fn bold(&self, label: &str) -> ColoredString {
790722
if self.config.no_bold {
791-
formatted_label.normal()
723+
label.normal()
792724
} else {
793-
formatted_label.bold()
725+
label.bold()
794726
}
795727
}
796-
}
797728

798-
fn write_buf<T: std::fmt::Display>(
799-
buffer: &mut std::fmt::Formatter,
800-
title: &ColoredString,
801-
content: T,
802-
) -> std::fmt::Result {
803-
writeln!(buffer, "{}{}", title, content)
729+
fn get_author_field(&self, title: &str) -> String {
730+
let mut authors_str = String::from("");
731+
732+
let pad = title.len() + 2;
733+
734+
for (i, author) in self.authors.iter().enumerate() {
735+
if i == 0 {
736+
authors_str.push_str(&format!(
737+
"{}{} {} {}\n",
738+
author.2.to_string().color(self.color_set.info),
739+
"%".color(self.color_set.info),
740+
author.0.to_string().color(self.color_set.info),
741+
author.1.to_string().color(self.color_set.info),
742+
));
743+
} else {
744+
authors_str.push_str(&format!(
745+
"{:<width$}{}{} {} {}\n",
746+
"",
747+
author.2.to_string().color(self.color_set.info),
748+
"%".color(self.color_set.info),
749+
author.0.to_string().color(self.color_set.info),
750+
author.1.to_string().color(self.color_set.info),
751+
width = pad
752+
));
753+
}
754+
}
755+
756+
authors_str
757+
}
758+
759+
fn get_languages_field(&self) -> String {
760+
let title = "Languages";
761+
let pad = " ".repeat(title.len() + 2);
762+
let mut languages_str = String::from("");
763+
let languages: Vec<(String, f64)> = {
764+
let mut iter = self.languages.iter().map(|x| (format!("{}", x.0), x.1));
765+
if self.languages.len() > 6 {
766+
let mut languages = iter.by_ref().take(6).collect::<Vec<_>>();
767+
let other_sum = iter.fold(0.0, |acc, x| acc + x.1);
768+
languages.push(("Other".to_owned(), other_sum));
769+
languages
770+
} else {
771+
iter.collect()
772+
}
773+
};
774+
775+
for (cnt, language) in languages.iter().enumerate() {
776+
let formatted_number = format!("{:.*}", 1, language.1).color(self.color_set.info);
777+
if cnt != 0 && cnt % 2 == 0 {
778+
languages_str.push_str(&format!(
779+
"\n{}{} ({} %) ",
780+
pad,
781+
language.0.color(self.color_set.info),
782+
formatted_number
783+
));
784+
} else {
785+
languages_str.push_str(&format!(
786+
"{} ({} %) ",
787+
language.0.color(self.color_set.info),
788+
formatted_number
789+
));
790+
}
791+
}
792+
languages_str
793+
}
794+
795+
fn get_branches_and_tags_field(&self) -> String {
796+
let branches_str = match self.number_of_branches {
797+
0 => String::new(),
798+
1 => String::from("1 branch"),
799+
_ => format!("{} branches", self.number_of_branches),
800+
};
801+
802+
let tags_str = match self.number_of_tags {
803+
0 => String::new(),
804+
1 => String::from("1 tag"),
805+
_ => format!("{} tags", self.number_of_tags),
806+
};
807+
808+
if tags_str.is_empty() && branches_str.is_empty() {
809+
String::new()
810+
} else if branches_str.is_empty() || tags_str.is_empty() {
811+
format!("({})", format!("{}{}", tags_str, branches_str))
812+
} else {
813+
format!("({}, {})", branches_str, tags_str)
814+
}
815+
}
804816
}

0 commit comments

Comments
 (0)