Skip to content

Commit

Permalink
fix: optimize table width on list subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
replydev committed Jul 24, 2024
1 parent c90e910 commit c182195
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/arguments/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl<'a> TryFrom<&'a OTPElement> for JsonOtpList<'a> {
}
}

const NO_ISSUER_TEXT: &str = "<No issuer>";

impl SubcommandExecutor for ListArgs {
fn run_command(self, otp_database: OTPDatabase) -> color_eyre::Result<OTPDatabase> {
if self.format.unwrap_or_default().json {
Expand All @@ -69,24 +71,44 @@ impl SubcommandExecutor for ListArgs {
.map_err(|e| eyre!("Error during JSON serialization: {:?}", e))?;
print!("{stringified}");
} else {
let issuer_width = calculate_width(&otp_database, |element| {
let issuer_length = element.issuer.chars().count();
if issuer_length > 0 {
issuer_length
} else {
NO_ISSUER_TEXT.chars().count()
}
});

let label_width =
calculate_width(&otp_database, |element| element.label.chars().count());

println!(
"{0: <6} {1: <30} {2: <70} {3: <10}",
"Index", "Issuer", "Label", "OTP"
"{0: <6} {1} {2} {3: <10}",
"Index",
"Issuer".to_owned() + " ".repeat(issuer_width - 6).as_ref(),
"Label".to_owned() + " ".repeat(label_width - 5).as_ref(),
"OTP",
);
otp_database
.elements
.iter()
.enumerate()
.for_each(|(index, e)| {
println!(
"{0: <6} {1: <30} {2: <70} {3: <10}",
"{0: <6} {1} {2} {3: <10}",
index + 1,
if e.issuer.is_empty() {
"<No issuer>"
NO_ISSUER_TEXT.to_owned()
+ " "
.repeat(issuer_width - NO_ISSUER_TEXT.chars().count())
.as_str()
} else {
e.issuer.as_str()
e.issuer.to_owned()
+ " ".repeat(issuer_width - e.issuer.chars().count()).as_str()
},
&e.label,
e.label.to_owned()
+ " ".repeat(label_width - e.label.chars().count()).as_str(),
e.get_otp_code().unwrap_or("ERROR".to_string())
)
});
Expand All @@ -95,3 +117,16 @@ impl SubcommandExecutor for ListArgs {
Ok(otp_database)
}
}

fn calculate_width<F>(otp_database: &OTPDatabase, get_number_of_chars: F) -> usize
where
F: Fn(&OTPElement) -> usize,
{
otp_database
.elements
.iter()
.map(get_number_of_chars)
.max()
.unwrap_or_default()
+ 3
}

0 comments on commit c182195

Please sign in to comment.