Skip to content

Commit b6705bb

Browse files
committed
fix get_git_info_field when version or username is empty
1 parent 8fd575a commit b6705bb

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

src/onefetch/info.rs

+29-11
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,13 @@ pub struct Info {
3838
impl std::fmt::Display for Info {
3939
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
4040
if !self.config.disabled_fields.git_info
41-
&& !&self.git_username.is_empty()
42-
&& !&self.git_version.is_empty()
41+
&& (!&self.git_username.is_empty() || !&self.git_version.is_empty())
4342
{
44-
let git_info_length = self.git_username.len() + self.git_version.len() + 3;
43+
let git_info_field = self.get_git_info_field();
4544

46-
writeln!(
47-
f,
48-
"{} {} {}",
49-
&self.bold(&self.git_username).color(self.text_colors.title),
50-
&self.bold("~").color(self.text_colors.tilde),
51-
&self.bold(&self.git_version).color(self.text_colors.title)
52-
)?;
45+
writeln!(f, "{}", git_info_field.0)?;
5346

54-
let separator = "-".repeat(git_info_length);
47+
let separator = "-".repeat(git_info_field.1);
5548

5649
writeln!(f, "{}", separator.color(self.text_colors.underline))?;
5750
}
@@ -289,6 +282,31 @@ impl Info {
289282
}
290283
}
291284

285+
fn get_git_info_field(&self) -> (String, usize) {
286+
let git_info_length = self.git_username.len() + self.git_version.len();
287+
288+
if !&self.git_username.is_empty() && !&self.git_version.is_empty() {
289+
(
290+
format!(
291+
"{} {} {}",
292+
&self.bold(&self.git_username).color(self.text_colors.title),
293+
&self.bold("~").color(self.text_colors.tilde),
294+
&self.bold(&self.git_version).color(self.text_colors.title)
295+
),
296+
git_info_length + 3,
297+
)
298+
} else {
299+
(
300+
format!(
301+
"{}{}",
302+
&self.bold(&self.git_username).color(self.text_colors.title),
303+
&self.bold(&self.git_version).color(self.text_colors.title)
304+
),
305+
git_info_length,
306+
)
307+
}
308+
}
309+
292310
fn get_author_field(&self, title: &str) -> String {
293311
let mut author_field = String::from("");
294312

0 commit comments

Comments
 (0)