From e86699c68c8bbc9685bf14466add52b09835f91a Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 8 Jul 2026 14:39:48 +0200 Subject: truncate search results Signed-off-by: salmonik --- src/commands/autocomplete.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'src/commands') diff --git a/src/commands/autocomplete.rs b/src/commands/autocomplete.rs index 422ab2d..97a18ec 100644 --- a/src/commands/autocomplete.rs +++ b/src/commands/autocomplete.rs @@ -8,19 +8,22 @@ type Context<'a> = poise::Context<'a, Data, Error>; const MAX_CHOICE_LEN: usize = 99; +fn truncate_choice(s: &str) -> String { + if s.chars().count() <= MAX_CHOICE_LEN { + return s.to_string(); + } + let mut t: String = s.chars().take(MAX_CHOICE_LEN - 1).collect(); + t.push('\u{2026}'); + t +} + fn filename_label(raw_path: &str) -> Option { let raw_name = raw_path.rsplit('/').next()?; if raw_name.is_empty() { return None; } let decoded = html_decode(&percent_decode(raw_name)); - if decoded.len() > MAX_CHOICE_LEN { - let mut s: String = decoded.chars().take(MAX_CHOICE_LEN - 1).collect(); - s.push('\u{2026}'); - Some(s) - } else { - Some(decoded) - } + Some(truncate_choice(&decoded)) } pub async fn audio_file<'a>( @@ -49,13 +52,13 @@ pub async fn audio_file<'a>( let Some(label) = filename_label(&rp) else { continue; }; - // Stash under the choice VALUE (`rp`), not the display label: - // the command receives `rp` as its argument and looks the hit - // up by that same key, so the cached link is reused instead of - // triggering a fresh copyparty search. - ctx.data().ac_stash(ctx.author().id, rp.clone(), hit).await; - eprintln!("[DEBUG] -> cached as {rp:?}"); - choices.push(serenity::AutocompleteChoice::new(label.clone(), rp)); + + let value = truncate_choice(&rp); + ctx.data() + .ac_stash(ctx.author().id, value.clone(), hit) + .await; + eprintln!("[DEBUG] -> cached as {value:?}"); + choices.push(serenity::AutocompleteChoice::new(label, value)); if choices.len() >= 25 { break; } -- cgit v1.2.3