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 cb901f2
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 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,7 @@ 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 description_margin = names.iter().map(|s| s.len()).max().unwrap_or_default() + 4;

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

Expand All @@ -53,12 +52,7 @@ 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
}
Some(desc) => format!("{name: <description_margin$}# {desc}"),
None => name,
};
let mut fragments = line.split(query).peekable();
Expand Down

0 comments on commit cb901f2

Please sign in to comment.