From 43cce03845021ddfcf9b143b251999a73df183f9 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 12 Jan 2025 02:42:43 +0200 Subject: [PATCH] add label formatter --- src/app/browser/window/header/bar/menu.rs | 24 ++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/app/browser/window/header/bar/menu.rs b/src/app/browser/window/header/bar/menu.rs index 024605aa..56224e3d 100644 --- a/src/app/browser/window/header/bar/menu.rs +++ b/src/app/browser/window/header/bar/menu.rs @@ -9,6 +9,7 @@ use std::rc::Rc; // Config options const RECENT_BOOKMARKS: usize = 50; +const LABEL_MAX_LENGTH: usize = 32; pub struct Menu { pub menu_button: MenuButton, @@ -176,13 +177,13 @@ impl Menu { let window_action = window_action.clone(); move |_| { main_bookmarks.remove_all(); - for bookmark in profile.bookmark.memory.recent(RECENT_BOOKMARKS) { - let menu_item = gio::MenuItem::new(Some(&bookmark), None); + for request in profile.bookmark.memory.recent(RECENT_BOOKMARKS) { + let menu_item = gio::MenuItem::new(Some(&label(&request, LABEL_MAX_LENGTH)), None); menu_item.set_action_and_target_value(Some(&format!( "{}.{}", window_action.id, window_action.open.simple_action.name() - )), Some(&bookmark.to_variant())); + )), Some(&request.to_variant())); main_bookmarks.append_item(&menu_item); } @@ -199,3 +200,20 @@ impl Menu { } } } + +/// Format dynamically generated strings for menu item labels +/// * trim gemini scheme prefix +/// * trim slash postfix +/// * crop resulting string at the middle position on new `value` longer than `limit` +fn label(value: &str, limit: usize) -> String { + let value = value.replace("gemini://", ""); + let value = value.trim_end_matches('/'); + + if value.len() <= limit { + return value.to_string(); + } + + let length = (limit - 2) / 2; + + format!("{}..{}", &value[..length], &value[value.len() - length..]) +}