mirror of
https://github.com/denisidoro/navi
synced 2024-11-21 19:13:07 +00:00
Correctly calculate character widths (#820)
This commit is contained in:
parent
2b03c89034
commit
8963749e6a
3 changed files with 21 additions and 3 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -446,6 +446,7 @@ dependencies = [
|
|||
"shellwords",
|
||||
"strip-ansi-escapes",
|
||||
"thiserror",
|
||||
"unicode-width",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
|
@ -973,6 +974,12 @@ version = "1.0.8"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::prelude::*;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
pub mod raycast;
|
||||
pub mod terminal;
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue