Skip to content

Commit

Permalink
Fix formatting issues in CLI "List" table (#16)
Browse files Browse the repository at this point in the history
This commit ensures that the border line (composed of '=' characters)
under the header of the "List" table in the CLI is not shorter than the
header text. The border line would be shorter, for example, if there is
only a single entry with no username or a short username. Moreover,
it ensures that the '|' separator for a (service, username) entry always
aligns with the header's '|'.
  • Loading branch information
OTheDev authored May 6, 2024
1 parent f90438d commit 6e42f27
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,20 @@ fn list_passwords(pwd_manager: &PwdManager) -> Result<()> {
if uids.is_empty() {
println!("No passwords stored.");
} else {
let max_service_len =
uids.iter().map(|uid| uid.service.len()).max().unwrap_or(0);
let max_service_len = uids
.iter()
.map(|uid| uid.service.len())
.max()
.unwrap_or(0)
.max("Service".len());
let max_username_len = uids
.iter()
.filter_map(|uid| uid.username.as_ref().map(String::len))
.max()
.unwrap_or(0);
let separator_len = " | ".len();
let header_len = max_service_len + separator_len + "Username".len();

println!(
"\n{0:<pad$} | {1}",
"Service".cyan(),
Expand All @@ -334,7 +341,10 @@ fn list_passwords(pwd_manager: &PwdManager) -> Result<()> {
println!(
"{:=<pad$}",
"",
pad = max_service_len + max_username_len + 3
pad = std::cmp::max(
max_service_len + max_username_len + separator_len,
header_len
)
);
for uid in uids {
println!(
Expand Down

0 comments on commit 6e42f27

Please sign in to comment.