diff --git a/Cargo.lock b/Cargo.lock index dabcd98..32eb086 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1311,7 +1311,7 @@ dependencies = [ [[package]] name = "trotp" -version = "1.1.4" +version = "1.1.5" dependencies = [ "arboard", "chrono", diff --git a/Cargo.toml b/Cargo.toml index e5b5634..51fcf31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trotp" -version = "1.1.4" +version = "1.1.5" edition = "2021" authors = ["Krakaw <41575888+Krakaw@users.noreply.github.com>"] license = "MIT" diff --git a/src/main.rs b/src/main.rs index 0fc5513..8996f3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -278,13 +278,51 @@ fn main() -> Result<(), TotpError> { if format == &OutputFormat::Json { println!("{}", serde_json::to_string(&storage.accounts()?)?); } else if format == &OutputFormat::List { + let mut id_length = 2; + let mut account_length = 30; + let mut token_length = 20; + let mut user_length = 10; + let mut password_length = 10; + let mut note_length = 10; + for record in storage.accounts()? { + id_length = id_length.max(format!("{}", record.id).len()); + account_length = + account_length.max(record.account.unwrap_or_else(|| "".to_string()).len()); + token_length = token_length.max( + record + .token + .map(|t| format!("{}", t)) + .unwrap_or("".to_string()) + .len(), + ); + user_length = + user_length.max(record.user.unwrap_or_else(|| "".to_string()).len()); + password_length = password_length + .max(record.password.unwrap_or_else(|| "".to_string()).len()); + note_length = + note_length.max(record.note.unwrap_or_else(|| "".to_string()).len()); + } + + // Use the above lengths to set the format lengths println!( - "{: <10} | {: <10} | {: <10} | {: <10} | {: <10} | {: <10}", - "ID", "Account", "Token", "User", "Password", "Note", + "{: Result<(), TotpError> { record.user.unwrap_or_else(|| "".to_string()), record.password.unwrap_or_else(|| "".to_string()), record.note.unwrap_or_else(|| "".to_string()), + id_length = id_length, + account_length = account_length, + token_length = token_length, + user_length = user_length, + password_length = password_length, ); } }