Skip to content

Commit

Permalink
Improve the margin calculation for the search command's UI
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Oct 28, 2023
1 parent 708383d commit 7ef2689
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/cargo/ops/registry/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! [1]: https://doc.rust-lang.org/nightly/cargo/reference/registry-web-api.html#search

use std::cmp;
use std::iter::repeat;

use anyhow::Context as _;
use url::Url;
Expand Down Expand Up @@ -35,7 +34,8 @@ pub fn search(
.map(|krate| format!("{} = \"{}\"", krate.name, krate.max_version))
.collect::<Vec<String>>();

let description_margin = names.iter().map(|s| s.len() + 4).max().unwrap_or_default();
let mut description_margin = names.iter().map(|s| s.len()).max().unwrap_or_default();
description_margin += 4;

let description_length = cmp::max(80, 128 - description_margin);

Expand All @@ -54,10 +54,12 @@ pub fn search(
for (name, description) in names.into_iter().zip(descriptions) {
let line = match description {
Some(desc) => {
let space = repeat(' ')
.take(description_margin - name.len())
.collect::<String>();
name + &space + "# " + &desc
format!(
"{name: <margin$}# {desc}",
name = name,
margin = description_margin,
desc = desc
)
}
None => name,
};
Expand Down

0 comments on commit 7ef2689

Please sign in to comment.