返回 CodeWhale
footer_hints.rs
根目录 / crates / tui / src / tui / footer_hints.rs
1 //! Footer key hints stand down once their binding has been used.
2 //!
3 //! The posture bar teaches each chord only until the user has pressed it
4 //! [`USES_TO_RETIRE`] times; after that the chip goes bare (or the hint
5 //! goes away) and the row stays quiet. Counts persist in [`Settings`]
6 //! beside `behavioral_tip_impressions`, so a learned binding stays learned
7 //! across sessions.
8 //!
9 //! [`Settings`]: codewhale_runtime::settings::Settings
10
11 use std::collections::BTreeMap;
12
13 use crate::tui::app::App;
14 use codewhale_runtime::settings::Settings;
15
16 /// Uses of a binding after which its footer hint retires.
17 pub(crate) const USES_TO_RETIRE: u8 = 2;
18
19 /// Stable hint keys. The footer shows a hint until its key reaches
20 /// [`USES_TO_RETIRE`] uses, then renders the bare state.
21 pub(crate) const PERMISSION_CYCLE: &str = "permission_cycle";
22 pub(crate) const ESC_INTERRUPT: &str = "esc_interrupt";
23 pub(crate) const AGENT_ARROWS: &str = "agent_arrows";
24 /// The `/help` route hint on the metrics line. It retires like every other
25 /// hint so the row stops advertising a route the user already knows
26 /// (founder, 2026-09-08: an always-on key hint is noise).
27 pub(crate) const HELP_ROUTE: &str = "help_route";
28
29 /// Whether the hint for `key` has been used often enough to retire.
30 pub(crate) fn retired(uses: &BTreeMap<String, u8>, key: &str) -> bool {
31 uses.get(key).copied().unwrap_or(0) >= USES_TO_RETIRE
32 }
33
34 impl App {
35 /// Record one use of the binding behind footer hint `key`.
36 ///
37 /// Persistence is best-effort. Once the in-memory count (seeded from
38 /// settings at startup) retires the hint, a use touches no file at all;
39 /// before that each of at most [`USES_TO_RETIRE`] uses per key runs one
40 /// settings transaction on this thread. A read-only home still retires
41 /// the hint for the running session.
42 pub(crate) fn note_footer_hint_used(&mut self, key: &str) {
43 if cfg!(test) {
44 // Tests never touch the settings file here, so only the
45 // in-memory count moves. Outside tests the read and the bump are
46 // one transaction, exactly like the behavioral-tip impressions.
47 let count = self.footer_hint_uses.entry(key.to_string()).or_default();
48 *count = count.saturating_add(1);
49 return;
50 }
51 if retired(&self.footer_hint_uses, key) {
52 return;
53 }
54 let owned = key.to_string();
55 let persisted = Settings::transact_opt(|settings| {
56 let count = settings.footer_hint_uses.get(&owned).copied().unwrap_or(0);
57 if count >= USES_TO_RETIRE {
58 return Ok(None);
59 }
60 let next = count.saturating_add(1);
61 settings.footer_hint_uses.insert(owned.clone(), next);
62 Ok(Some(next))
63 });
64 match persisted {
65 Ok(next) => {
66 // `None` means already retired on disk: pin the in-memory
67 // count at the retire line so the next frame stands down.
68 let count = self.footer_hint_uses.entry(owned).or_default();
69 *count = (*count).max(next.unwrap_or(USES_TO_RETIRE));
70 }
71 Err(err) => {
72 tracing::warn!(hint = key, error = %err, "footer hint use was not persisted");
73 let count = self.footer_hint_uses.entry(owned).or_default();
74 *count = count.saturating_add(1);
75 }
76 }
77 }
78 }
79
80 #[cfg(test)]
81 mod tests {
82 use super::*;
83
84 #[test]
85 fn hints_retire_after_two_uses() {
86 let uses = BTreeMap::new();
87 assert!(!retired(&uses, PERMISSION_CYCLE));
88 for key in [PERMISSION_CYCLE, ESC_INTERRUPT, AGENT_ARROWS] {
89 let mut uses = BTreeMap::new();
90 uses.insert(key.to_string(), 1);
91 assert!(!retired(&uses, key), "{key} at 1 use still shows");
92 uses.insert(key.to_string(), USES_TO_RETIRE);
93 assert!(retired(&uses, key), "{key} at 2 uses is gone");
94 uses.insert(key.to_string(), u8::MAX);
95 assert!(retired(&uses, key), "{key} stays retired");
96 }
97 }
98
99 #[test]
100 fn recorded_uses_accumulate_per_key() {
101 let mut app = crate::tui::test_support::test_app_with_options(
102 crate::tui::test_support::test_tui_options(std::path::PathBuf::from(".")),
103 );
104 app.note_footer_hint_used(PERMISSION_CYCLE);
105 assert_eq!(app.footer_hint_uses.get(PERMISSION_CYCLE).copied(), Some(1));
106 assert!(!retired(&app.footer_hint_uses, PERMISSION_CYCLE));
107 app.note_footer_hint_used(PERMISSION_CYCLE);
108 assert!(retired(&app.footer_hint_uses, PERMISSION_CYCLE));
109 // Other keys are unaffected.
110 assert!(!retired(&app.footer_hint_uses, ESC_INTERRUPT));
111 }
112 }
113
113 lines RUST