返回 CodeWhale
grammar.rs
根目录 / crates / palette / src / grammar.rs
1 //! Closed color vocabulary for status-bar chrome.
2 //!
3 //! Five visual families: surface, neutral text, action, live/outcome, and
4 //! attention. Semantic roles stay explicit even when they share a hue;
5 //! modes and outcomes are named by their labels, not extra rainbow lanes.
6 //! Attention preserves each theme's permission, warning and danger shades.
7 //!
8 //! Contract: `docs/design/STATUS_BAR_COLOR_GRAMMAR.md`.
9
10 use ratatui::style::{Color, Style};
11
12 use super::themes::UiTheme;
13
14 /// The five visual families. Surface is the canvas, not a foreground ink.
15 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
16 pub enum SemanticFamily {
17 Surface,
18 Neutral,
19 Action,
20 Live,
21 Attention,
22 }
23
24 impl SemanticFamily {
25 #[cfg(test)]
26 pub const ALL: [Self; 5] = [
27 Self::Surface,
28 Self::Neutral,
29 Self::Action,
30 Self::Live,
31 Self::Attention,
32 ];
33 }
34
35 /// Named status-bar inks. Each variant is an existing `UiTheme` slot, not a
36 /// new theme. Adding a variant requires assigning one of the five families.
37 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
38 pub enum ChromeInk {
39 Outcome,
40 PermissionAsk,
41 PermissionAutoReview,
42 PermissionFullAccess,
43 Waiting,
44 Attention,
45 Active,
46 PolicyAct,
47 PolicyPlan,
48 PolicyOperate,
49 Identity,
50 Info,
51 MetadataValue,
52 Metadata,
53 MetadataHint,
54 MetadataDim,
55 Failure,
56 }
57
58 impl ChromeInk {
59 #[cfg(test)]
60 pub const ALL: [Self; 17] = [
61 Self::Outcome,
62 Self::PermissionAsk,
63 Self::PermissionAutoReview,
64 Self::PermissionFullAccess,
65 Self::Waiting,
66 Self::Attention,
67 Self::Active,
68 Self::PolicyAct,
69 Self::PolicyPlan,
70 Self::PolicyOperate,
71 Self::Identity,
72 Self::Info,
73 Self::MetadataValue,
74 Self::Metadata,
75 Self::MetadataHint,
76 Self::MetadataDim,
77 Self::Failure,
78 ];
79
80 #[must_use]
81 pub const fn family(self) -> SemanticFamily {
82 match self {
83 Self::Outcome | Self::Active => SemanticFamily::Live,
84 Self::PermissionAsk
85 | Self::PermissionAutoReview
86 | Self::PermissionFullAccess
87 | Self::Waiting
88 | Self::Attention
89 | Self::Failure => SemanticFamily::Attention,
90 Self::PolicyAct
91 | Self::PolicyPlan
92 | Self::PolicyOperate
93 | Self::Identity
94 | Self::Info => SemanticFamily::Action,
95 Self::MetadataValue | Self::Metadata | Self::MetadataHint | Self::MetadataDim => {
96 SemanticFamily::Neutral
97 }
98 }
99 }
100
101 /// Resolve through the live theme. Ordinary navigation and mode share
102 /// its action hue; active and completed work share its live hue. Safety
103 /// retains the existing permission and warning/error distinctions.
104 #[must_use]
105 pub fn color(self, theme: &UiTheme) -> Color {
106 match self {
107 Self::Outcome => theme.status_working,
108 Self::PermissionAsk => theme.permission_ask,
109 Self::PermissionAutoReview => theme.permission_auto_review,
110 Self::PermissionFullAccess => theme.permission_full_access,
111 Self::Waiting => theme.accent_action,
112 Self::Attention => theme.warning,
113 Self::Active => theme.status_working,
114 Self::PolicyAct => theme.accent_primary,
115 Self::PolicyPlan => theme.accent_primary,
116 Self::PolicyOperate => theme.accent_primary,
117 Self::Identity => theme.accent_primary,
118 Self::Info => theme.accent_primary,
119 Self::MetadataValue => theme.text_soft,
120 Self::Metadata => theme.text_muted,
121 Self::MetadataHint => theme.text_hint,
122 Self::MetadataDim => theme.text_dim,
123 Self::Failure => theme.error_fg,
124 }
125 }
126 }
127
128 #[must_use]
129 pub fn chrome_style(theme: &UiTheme, ink: ChromeInk) -> Style {
130 Style::default().fg(ink.color(theme))
131 }
132
133 #[cfg(test)]
134 mod tests {
135 use super::*;
136 use crate::ids::SELECTABLE_THEMES;
137
138 #[test]
139 fn chrome_has_five_visual_families_with_explicit_safety_roles() {
140 assert_eq!(SemanticFamily::ALL.len(), 5);
141 for ink in ChromeInk::ALL {
142 assert!(SemanticFamily::ALL.contains(&ink.family()));
143 }
144 assert_eq!(ChromeInk::Failure.family(), SemanticFamily::Attention);
145 assert_eq!(
146 ChromeInk::PermissionFullAccess.family(),
147 SemanticFamily::Attention
148 );
149 assert_eq!(ChromeInk::PolicyOperate.family(), SemanticFamily::Action);
150 assert_eq!(ChromeInk::Outcome.family(), SemanticFamily::Live);
151 }
152
153 #[test]
154 fn every_selectable_theme_limits_ordinary_chrome_to_action_and_live() {
155 for id in SELECTABLE_THEMES {
156 let theme = id.ui_theme();
157 // Changing a mode or finishing work must not add a third hue.
158 for ink in [
159 ChromeInk::Identity,
160 ChromeInk::Info,
161 ChromeInk::PolicyAct,
162 ChromeInk::PolicyPlan,
163 ChromeInk::PolicyOperate,
164 ] {
165 assert_eq!(
166 ink.color(&theme),
167 theme.accent_primary,
168 "{} {ink:?}",
169 id.name()
170 );
171 }
172 for ink in [ChromeInk::Active, ChromeInk::Outcome] {
173 assert_eq!(
174 ink.color(&theme),
175 theme.status_working,
176 "{} {ink:?}",
177 id.name()
178 );
179 }
180 assert_eq!(ChromeInk::Metadata.color(&theme), theme.text_muted);
181 }
182 }
183
184 #[test]
185 fn every_selectable_theme_preserves_permission_and_failure_meaning() {
186 for id in SELECTABLE_THEMES {
187 let theme = id.ui_theme();
188 let permissions = [
189 ChromeInk::PermissionAsk.color(&theme),
190 ChromeInk::PermissionAutoReview.color(&theme),
191 ChromeInk::PermissionFullAccess.color(&theme),
192 ];
193 assert_eq!(
194 permissions,
195 [
196 theme.permission_ask,
197 theme.permission_auto_review,
198 theme.permission_full_access
199 ],
200 "{} permission authority",
201 id.name()
202 );
203 assert_ne!(
204 permissions[0],
205 permissions[1],
206 "{} Ask/Auto-Review",
207 id.name()
208 );
209 assert_ne!(
210 permissions[1],
211 permissions[2],
212 "{} Auto-Review/Full Access",
213 id.name()
214 );
215 assert_ne!(
216 permissions[0],
217 permissions[2],
218 "{} Ask/Full Access",
219 id.name()
220 );
221 assert_eq!(ChromeInk::Attention.color(&theme), theme.warning);
222 assert_eq!(ChromeInk::Failure.color(&theme), theme.error_fg);
223 assert_eq!(
224 chrome_style(&theme, ChromeInk::Failure).fg,
225 Some(theme.error_fg)
226 );
227 }
228 }
229 }
230
230 lines RUST