Skip to content

Commit 3e726c8

Browse files
committed
code review
1 parent 78617c7 commit 3e726c8

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

Diff for: src/main.rs

+48-29
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::process::{Command, Stdio};
1212
use std::str::FromStr;
1313
use license::License;
1414
use std::ffi::OsStr;
15+
use std::fmt::Write;
1516

1617
struct Info {
1718
project_name: String,
@@ -24,59 +25,77 @@ struct Info {
2425

2526
impl fmt::Display for Info {
2627
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
let mut s = String::from("\n");
28+
let mut buffer = String::new();
2829
let color = get_color(&self.language);
2930

30-
s.push_str(
31-
&("Project: ".color(color).bold().to_string() + &format!("{}\n", self.project_name)),
32-
);
33-
34-
s.push_str(
35-
&("Language: ".color(color).bold().to_string() + &format!("{}\n", self.language)),
36-
);
37-
38-
if self.authors.len() > 0 {
31+
writeln!(
32+
buffer,
33+
"{}{}",
34+
"Project: ".color(color).bold(),
35+
self.project_name
36+
)?;
37+
writeln!(
38+
buffer,
39+
"{}{}",
40+
"Language: ".color(color).bold(),
41+
self.language
42+
)?;
43+
44+
if !self.authors.is_empty() {
3945
let title = if self.authors.len() > 1 {
4046
"Authors: "
4147
} else {
4248
"Author: "
4349
};
4450

45-
let first = self.authors.first().unwrap();
46-
s.push_str(&(title.color(color).bold().to_string() + &format!("{}\n", first)));
51+
writeln!(
52+
buffer,
53+
"{}{}",
54+
title.color(color).bold(),
55+
self.authors.first().unwrap()
56+
)?;
4757

48-
let title = (0..title.len()).map(|_| " ").collect::<String>();
58+
let title = " ".repeat(title.len());
4959

5060
for author in self.authors.iter().skip(1) {
51-
s.push_str(&(title.color(color).bold().to_string() + &format!("{}\n", author)));
61+
writeln!(buffer, "{}{}", title.color(color).bold(), author)?;
5262
}
5363
}
5464

55-
s.push_str(&("Repo: ".color(color).bold().to_string() + &format!("{}\n", self.repo)));
56-
s.push_str(
57-
&("Number of lines: ".color(color).bold().to_string()
58-
+ &format!("{}\n", self.number_of_lines)),
59-
);
60-
s.push_str(&("License: ".color(color).bold().to_string() + &format!("{}\n", self.license)));
65+
writeln!(buffer, "{}{}", "Repo: ".color(color).bold(), self.repo)?;
66+
writeln!(
67+
buffer,
68+
"{}{}",
69+
"Number of lines: ".color(color).bold(),
70+
self.number_of_lines
71+
)?;
72+
writeln!(
73+
buffer,
74+
"{}{}",
75+
"License: ".color(color).bold(),
76+
self.license
77+
)?;
6178

6279
let logo = self.get_ascii();
63-
let mut lines = s.lines();
80+
let mut lines = buffer.lines();
6481
let left_pad = logo.lines().map(|l| l.len()).max().unwrap_or(0);
65-
let mut o = String::new();
82+
6683
for a in logo.lines() {
6784
let b = match lines.next() {
6885
Some(line) => line,
6986
None => "",
7087
};
71-
o.push_str(&format!(
72-
"{:width$} {}\n",
88+
89+
writeln!(
90+
f,
91+
"{:width$} {}",
7392
a.color(color).bold(),
7493
b,
7594
width = left_pad
76-
));
95+
)?;
7796
}
7897

79-
write!(f, "{}", o)
98+
Ok(())
8099
}
81100
}
82101

@@ -168,8 +187,8 @@ fn main() {
168187

169188
let info = Info {
170189
project_name: config.repository_name,
171-
language: language,
172-
authors: authors,
190+
language,
191+
authors,
173192
repo: config.repository_url,
174193
number_of_lines: get_total_loc(&tokei_langs),
175194
license: project_license(),
@@ -243,7 +262,7 @@ fn get_configuration() -> Result<Configuration, Error> {
243262
};
244263

245264
let url = remote_url.clone();
246-
let name_parts: Vec<&str> = url.split("/").collect();
265+
let name_parts: Vec<&str> = url.split('/').collect();
247266

248267
if name_parts.len() > 0 {
249268
repository_name = name_parts[name_parts.len() - 1].to_string();

0 commit comments

Comments
 (0)