From c729edd2013f34dc03874945126d4855d8165422 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Thu, 5 Nov 2026 01:26:25 +0100 Subject: [PATCH] feat(cli-rs): let config and leader timeout change at runtime Resolved::with_info re-resolves a reloaded Info in the environment of the first resolve (HERDR_ENV), and TuiKeymap::set_leader_timeout changes the leader timeout the next leader key waits for. The live config uses both. --- .../cli-rs/crates/local/src/cli_config.rs | 17 ++++++++++++-- packages/cli-rs/crates/ui/src/keymap/mod.rs | 23 +++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/cli-rs/crates/local/src/cli_config.rs b/packages/cli-rs/crates/local/src/cli_config.rs index 31894f1afcf..0653ff5582f 100644 --- a/packages/cli-rs/crates/local/src/cli_config.rs +++ b/packages/cli-rs/crates/local/src/cli_config.rs @@ -728,6 +728,8 @@ pub struct Resolved { pub permissions: Permissions, pub tps: bool, pub tabs: ResolvedTabs, + /// `HERDR_ENV=1` when resolved, so [`Resolved::with_info`] resolves a reload the same way. + herdr: bool, } #[derive(Clone, Debug, PartialEq)] @@ -761,6 +763,7 @@ impl Resolved { Some(true) => TabsMode::On, Some(false) => TabsMode::Off, }); + let herdr = environment("HERDR_ENV").as_deref() == Some("1"); Self { attention: ResolvedAttention { notifications: attention.notifications.unwrap_or(false), @@ -785,16 +788,23 @@ impl Resolved { tps: session.tps.unwrap_or(true), tabs: ResolvedTabs { mode, - enabled: mode == TabsMode::On - || (mode == TabsMode::Auto && environment("HERDR_ENV").as_deref() != Some("1")), + enabled: mode == TabsMode::On || (mode == TabsMode::Auto && !herdr), scope: tabs.scope.unwrap_or(TabsScope::Cwd), layout: tabs.layout.unwrap_or(TabsLayout::Horizontal), indicators: tabs.indicators.unwrap_or(TabsIndicators::Status), }, info, + herdr, } } + /// `info` resolved in the environment this config was resolved in, for a live update. + pub fn with_info(&self, info: Info) -> Self { + Self::new(info, |name| { + (name == "HERDR_ENV" && self.herdr).then(|| "1".to_owned()) + }) + } + /// The `keybinds` overrides for `Keybinds::resolve`. pub fn keybinds(&self) -> Map { self.info.keybinds.clone().unwrap_or_default().0 @@ -901,5 +911,8 @@ mod tests { (name == "HERDR_ENV").then(|| "1".to_string()) }); assert!(!herdr.tabs.enabled); + // A reload keeps the environment of the first resolve. + assert!(!herdr.with_info(Info::default()).tabs.enabled); + assert!(resolved.with_info(Info::default()).tabs.enabled); } } diff --git a/packages/cli-rs/crates/ui/src/keymap/mod.rs b/packages/cli-rs/crates/ui/src/keymap/mod.rs index 06664b9bb5a..a3926feb730 100644 --- a/packages/cli-rs/crates/ui/src/keymap/mod.rs +++ b/packages/cli-rs/crates/ui/src/keymap/mod.rs @@ -224,6 +224,8 @@ pub struct TuiKeymap { keymap: Keymap, keybinds: Rc>, leader: Option>, + /// The timeout `leader` was built with, and the configured one now ([`Self::set_leader_timeout`]). + leader_timeouts: (Duration, Cell), leader_stroke: Option, modes: Rc>>, next_mode: Cell, @@ -279,6 +281,7 @@ impl TuiKeymap { keymap, keybinds: Rc::new(keybinds), leader, + leader_timeouts: (options.leader_timeout, Cell::new(options.leader_timeout)), leader_stroke, modes: Rc::new(RefCell::new(Vec::new())), next_mode: Cell::new(0), @@ -318,14 +321,26 @@ impl TuiKeymap { /// When the pending leader sequence expires; schedule a timer and call [`Self::expire_leader`]. pub fn leader_deadline(&self) -> Option { - self.leader.as_ref()?.deadline() + let (built, current) = (self.leader_timeouts.0, self.leader_timeouts.1.get()); + let deadline = self.leader.as_ref()?.deadline()?; + Some(deadline - built + current) } /// Clears the leader sequence if `now` is past its deadline. Returns whether it did. pub fn expire_leader(&self, now: Instant) -> bool { - self.leader - .as_ref() - .is_some_and(|leader| leader.expire(now)) + if self.leader_deadline().is_none_or(|deadline| now < deadline) { + return false; + } + // The addon keeps the timeout it was built with, so a shorter configured one clears the sequence here. + if !self.leader.as_ref().is_some_and(|leader| leader.expire(now)) { + self.keymap.clear_pending_sequence(); + } + true + } + + /// `leader.timeout` after a config change; the next leader key takes it. + pub fn set_leader_timeout(&self, timeout: Duration) { + self.leader_timeouts.1.set(timeout); } /// Whether `event` is the configured leader key.