Skip to content

Commit

Permalink
Correctly calculate character widths (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro committed Apr 8, 2023
1 parent 2b03c89 commit 8963749
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -35,6 +35,7 @@ serde = { version = "1.0.159", features = ["derive"] }
serde_yaml = "0.9.21"
dns_common_derive = { version = "0.2.1" }
dns_common = { version = "0.2.1", default-features = false, features = ["yaml", "json"] }
unicode-width = "0.1.10"

[lib]
name = "navi"
Expand Down
16 changes: 13 additions & 3 deletions src/deser/mod.rs
@@ -1,4 +1,5 @@
use crate::prelude::*;
use unicode_width::UnicodeWidthStr;

pub mod raycast;
pub mod terminal;
Expand Down Expand Up @@ -26,9 +27,18 @@ pub fn fix_newlines(txt: &str) -> String {
}

fn limit_str(text: &str, length: usize) -> String {
if text.len() > length {
format!("{}…", text.chars().take(length - 1).collect::<String>())
let len = UnicodeWidthStr::width(text);
if len <= length {
format!("{}{}", text, " ".repeat(length - len))
} else {
format!("{:width$}", text, width = length)
let mut new_length = length;
let mut actual_length = 9999;
let mut txt = text.to_owned();
while actual_length > length {
txt = txt.chars().take(new_length - 1).collect::<String>();
actual_length = UnicodeWidthStr::width(txt.as_str());
new_length -= 1;
}
format!("{}…", txt)
}
}

1 comment on commit 8963749

@lacygoill
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big issue, but this commit seems to introduce a regression if we have this config (in ~/.config/navi/config.yaml):

    style:
      snippet:
        width_percentage: 0
        min_width: 0

The UI doesn't show anything. We have to press CTRL-D to quit navi, then CTRL-C to kill the parent shell.

Again, not a big issue, but it was working before.

Please sign in to comment.