mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 23:24:00 +03:00
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.
This commit is contained in:
@@ -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<String, Value> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,6 +224,8 @@ pub struct TuiKeymap<T> {
|
||||
keymap: Keymap<T>,
|
||||
keybinds: Rc<Keybinds<T>>,
|
||||
leader: Option<TimedLeader<T>>,
|
||||
/// The timeout `leader` was built with, and the configured one now ([`Self::set_leader_timeout`]).
|
||||
leader_timeouts: (Duration, Cell<Duration>),
|
||||
leader_stroke: Option<Stroke>,
|
||||
modes: Rc<RefCell<Vec<ModeEntry>>>,
|
||||
next_mode: Cell<u64>,
|
||||
@@ -279,6 +281,7 @@ impl<T: Clone + PartialEq + 'static> TuiKeymap<T> {
|
||||
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<T: Clone + PartialEq + 'static> TuiKeymap<T> {
|
||||
|
||||
/// When the pending leader sequence expires; schedule a timer and call [`Self::expire_leader`].
|
||||
pub fn leader_deadline(&self) -> Option<Instant> {
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user