Skip to content

Commit

Permalink
fix: start code index from one as done in dashboard and in edit subco…
Browse files Browse the repository at this point in the history
…mmand (#458)

Fixes #457
  • Loading branch information
replydev authored Jul 24, 2024
2 parents d35a94b + c182195 commit 6a36c8d
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 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}",
index,
"{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 6a36c8d

Please sign in to comment.