Skip to content

Commit

Permalink
fix get_git_info_field when version or username is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Dec 16, 2020
1 parent 8fd575a commit b6705bb
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/onefetch/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,13 @@ pub struct Info {
impl std::fmt::Display for Info {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if !self.config.disabled_fields.git_info
&& !&self.git_username.is_empty()
&& !&self.git_version.is_empty()
&& (!&self.git_username.is_empty() || !&self.git_version.is_empty())
{
let git_info_length = self.git_username.len() + self.git_version.len() + 3;
let git_info_field = self.get_git_info_field();

writeln!(
f,
"{} {} {}",
&self.bold(&self.git_username).color(self.text_colors.title),
&self.bold("~").color(self.text_colors.tilde),
&self.bold(&self.git_version).color(self.text_colors.title)
)?;
writeln!(f, "{}", git_info_field.0)?;

let separator = "-".repeat(git_info_length);
let separator = "-".repeat(git_info_field.1);

writeln!(f, "{}", separator.color(self.text_colors.underline))?;
}
Expand Down Expand Up @@ -289,6 +282,31 @@ impl Info {
}
}

fn get_git_info_field(&self) -> (String, usize) {
let git_info_length = self.git_username.len() + self.git_version.len();

if !&self.git_username.is_empty() && !&self.git_version.is_empty() {
(
format!(
"{} {} {}",
&self.bold(&self.git_username).color(self.text_colors.title),
&self.bold("~").color(self.text_colors.tilde),
&self.bold(&self.git_version).color(self.text_colors.title)
),
git_info_length + 3,
)
} else {
(
format!(
"{}{}",
&self.bold(&self.git_username).color(self.text_colors.title),
&self.bold(&self.git_version).color(self.text_colors.title)
),
git_info_length,
)
}
}

fn get_author_field(&self, title: &str) -> String {
let mut author_field = String::from("");

Expand Down

0 comments on commit b6705bb

Please sign in to comment.