fix: avoid splitting graphemes in skills list (#12074)
This commit is contained in:
Generated
+1
@@ -5384,6 +5384,7 @@ dependencies = [
|
||||
"tokio-util",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"unicode-segmentation",
|
||||
"url",
|
||||
"urlencoding",
|
||||
"webbrowser",
|
||||
|
||||
@@ -34,6 +34,7 @@ rmcp = { workspace = true }
|
||||
clap = { workspace = true }
|
||||
cliclack = { version = "0.5", default-features = false }
|
||||
console = { version = "0.16", default-features = false, features = ["std"] }
|
||||
unicode-segmentation = { version = "1.12", default-features = false }
|
||||
dotenvy = { workspace = true }
|
||||
bat = { version = "0.26", default-features = false, features = ["regex-onig"] }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
@@ -2,6 +2,7 @@ use anyhow::Result;
|
||||
use console::{measure_text_width, Term};
|
||||
use goose::skills::list_installed_skills;
|
||||
use goose::token_counter::create_token_counter;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
const DESCRIPTION_PREVIEW_CHARS: usize = 50;
|
||||
const SEPARATOR: &str = " | ";
|
||||
@@ -207,15 +208,17 @@ fn truncate_to_display_width(text: &str, max_width: usize) -> String {
|
||||
return ".".repeat(max_width);
|
||||
}
|
||||
|
||||
let mut output = String::new();
|
||||
let suffix_width = measure_text_width("...");
|
||||
let mut output = String::new();
|
||||
let mut output_width = 0;
|
||||
|
||||
for ch in text.chars() {
|
||||
output.push(ch);
|
||||
if measure_text_width(&output) + suffix_width > max_width {
|
||||
output.pop();
|
||||
for grapheme in text.graphemes(true) {
|
||||
let grapheme_width = measure_text_width(grapheme);
|
||||
if output_width + grapheme_width + suffix_width > max_width {
|
||||
break;
|
||||
}
|
||||
output.push_str(grapheme);
|
||||
output_width += grapheme_width;
|
||||
}
|
||||
|
||||
output.push_str("...");
|
||||
@@ -226,3 +229,21 @@ fn pad_to_display_width(text: &str, width: usize) -> String {
|
||||
let padding = width.saturating_sub(measure_text_width(text));
|
||||
format!("{}{}", text, " ".repeat(padding))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn truncate_to_display_width_keeps_grapheme_clusters_intact() {
|
||||
assert_eq!(truncate_to_display_width("🏳️🌈abcd", 5), "🏳️🌈...");
|
||||
assert_eq!(truncate_to_display_width("👩💻abcd", 5), "👩💻...");
|
||||
assert_eq!(truncate_to_display_width("a\u{301}bcde", 4), "a\u{301}...");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_to_display_width_drops_cluster_that_cannot_fit() {
|
||||
assert_eq!(truncate_to_display_width("🏳️🌈abc", 4), "...");
|
||||
assert_eq!(truncate_to_display_width("界界界", 5), "界...");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user