| 1 | //! Status marks: a glyph and a word, never color alone. |
| 2 | //! |
| 3 | //! Replaces the engine's `menu_style::StatusMark` (`crates/tui/src/tui/ |
| 4 | //! menu_style.rs`, `Hmbown/CodeWhale` `58b1dd3dd`). The constructor takes a |
| 5 | //! [`State`], and the word comes with it, so there is no way to build a |
| 6 | //! colored dot that means nothing without its color. |
| 7 | |
| 8 | use std::borrow::Cow; |
| 9 | |
| 10 | use ratatui::{buffer::Buffer, layout::Rect, text::Span, widgets::Widget}; |
| 11 | |
| 12 | use crate::{Paint, Role, Theme, glyphs, text}; |
| 13 | |
| 14 | /// Product state. One hue, one mark and one word each, on every surface. |
| 15 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 16 | pub enum State { |
| 17 | Working, |
| 18 | Done, |
| 19 | NeedsYou, |
| 20 | Failed, |
| 21 | Stopped, |
| 22 | Ready, |
| 23 | /// Nobody reported a state. Shown as unknown, never as a failure. |
| 24 | Unknown, |
| 25 | /// Blocked on a hard precondition or external gate. |
| 26 | Blocked, |
| 27 | /// Resting calmly between active runs. |
| 28 | Resting, |
| 29 | /// Asleep or suspended. |
| 30 | Asleep, |
| 31 | } |
| 32 | |
| 33 | impl State { |
| 34 | pub const ALL: [State; 10] = [ |
| 35 | State::Working, |
| 36 | State::Done, |
| 37 | State::NeedsYou, |
| 38 | State::Failed, |
| 39 | State::Stopped, |
| 40 | State::Ready, |
| 41 | State::Unknown, |
| 42 | State::Blocked, |
| 43 | State::Resting, |
| 44 | State::Asleep, |
| 45 | ]; |
| 46 | |
| 47 | #[must_use] |
| 48 | pub const fn role(self) -> Role { |
| 49 | match self { |
| 50 | State::Working | State::Done => Role::Live, |
| 51 | State::NeedsYou => Role::Attention, |
| 52 | State::Failed | State::Blocked => Role::Danger, |
| 53 | State::Stopped | State::Ready | State::Unknown | State::Resting | State::Asleep => { |
| 54 | Role::Muted |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #[must_use] |
| 60 | pub const fn glyph(self) -> &'static str { |
| 61 | match self { |
| 62 | State::Working => glyphs::CURRENT, |
| 63 | State::Done => glyphs::DONE, |
| 64 | State::NeedsYou => glyphs::ATTENTION, |
| 65 | State::Failed => glyphs::FAILED, |
| 66 | State::Stopped => glyphs::STOPPED, |
| 67 | State::Ready => glyphs::READY, |
| 68 | State::Unknown => glyphs::UNKNOWN, |
| 69 | State::Blocked => glyphs::BLOCKED, |
| 70 | State::Resting => glyphs::RESTING, |
| 71 | State::Asleep => glyphs::ASLEEP, |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// The English default word, from [`StateWords::english`]. Hosts pass |
| 76 | /// their own words with [`StatusMark::with_words`] or |
| 77 | /// [`StatusMark::word`]. |
| 78 | #[must_use] |
| 79 | pub const fn word(self) -> &'static str { |
| 80 | StateWords::english(self) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// The word shown beside each [`State`]'s mark. The kit owns no copy beyond |
| 85 | /// this default: a host fills one per locale and passes it in. `Default` is |
| 86 | /// English. |
| 87 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 88 | pub struct StateWords { |
| 89 | pub working: Cow<'static, str>, |
| 90 | pub done: Cow<'static, str>, |
| 91 | pub needs_you: Cow<'static, str>, |
| 92 | pub failed: Cow<'static, str>, |
| 93 | pub stopped: Cow<'static, str>, |
| 94 | pub ready: Cow<'static, str>, |
| 95 | pub unknown: Cow<'static, str>, |
| 96 | } |
| 97 | |
| 98 | impl StateWords { |
| 99 | /// The English word for `state`: the only copy in the kit's status |
| 100 | /// marks, and what [`StateWords::default`] holds. |
| 101 | #[must_use] |
| 102 | pub const fn english(state: State) -> &'static str { |
| 103 | match state { |
| 104 | State::Working => "Working", |
| 105 | State::Done => "Done", |
| 106 | State::NeedsYou => "Needs you", |
| 107 | State::Failed => "Failed", |
| 108 | State::Stopped => "Stopped", |
| 109 | State::Ready => "Ready", |
| 110 | State::Unknown => "Unknown", |
| 111 | State::Blocked => "Blocked", |
| 112 | State::Resting => "Resting", |
| 113 | State::Asleep => "Asleep", |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /// The word for `state`. |
| 118 | #[must_use] |
| 119 | pub fn get(&self, state: State) -> &str { |
| 120 | match state { |
| 121 | State::Working => &self.working, |
| 122 | State::Done => &self.done, |
| 123 | State::NeedsYou => &self.needs_you, |
| 124 | State::Failed => &self.failed, |
| 125 | State::Stopped => &self.stopped, |
| 126 | State::Ready => &self.ready, |
| 127 | State::Unknown => &self.unknown, |
| 128 | State::Blocked | State::Resting | State::Asleep => Self::english(state), |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl Default for StateWords { |
| 134 | fn default() -> Self { |
| 135 | let english = |state| Cow::Borrowed(Self::english(state)); |
| 136 | Self { |
| 137 | working: english(State::Working), |
| 138 | done: english(State::Done), |
| 139 | needs_you: english(State::NeedsYou), |
| 140 | failed: english(State::Failed), |
| 141 | stopped: english(State::Stopped), |
| 142 | ready: english(State::Ready), |
| 143 | unknown: english(State::Unknown), |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// `● Working`, `✓ Done`, `◆ Needs you`: the mark takes the state's hue; the |
| 149 | /// word stays `Foreground` so it reads at full contrast. |
| 150 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 151 | pub struct StatusMark { |
| 152 | pub state: State, |
| 153 | pub word: Cow<'static, str>, |
| 154 | /// Optional quiet detail clause rendered after `word` as `word · detail`. |
| 155 | pub detail: Option<Cow<'static, str>>, |
| 156 | /// Paint the word in the state's hue too, for a mark that stands alone. |
| 157 | pub tinted: bool, |
| 158 | } |
| 159 | |
| 160 | impl StatusMark { |
| 161 | #[must_use] |
| 162 | pub fn new(state: State) -> Self { |
| 163 | Self { |
| 164 | state, |
| 165 | word: Cow::Borrowed(state.word()), |
| 166 | detail: None, |
| 167 | tinted: false, |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /// A mark whose word comes from the host's [`StateWords`]. |
| 172 | #[must_use] |
| 173 | pub fn with_words(state: State, words: &StateWords) -> Self { |
| 174 | Self { |
| 175 | state, |
| 176 | word: Cow::Owned(words.get(state).to_owned()), |
| 177 | detail: None, |
| 178 | tinted: false, |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[must_use] |
| 183 | pub fn word(mut self, word: impl Into<Cow<'static, str>>) -> Self { |
| 184 | self.word = word.into(); |
| 185 | self |
| 186 | } |
| 187 | |
| 188 | /// Attach a quiet detail clause rendered as `word · detail`. |
| 189 | #[must_use] |
| 190 | pub fn detail(mut self, detail: impl Into<Cow<'static, str>>) -> Self { |
| 191 | self.detail = Some(detail.into()); |
| 192 | self |
| 193 | } |
| 194 | |
| 195 | #[must_use] |
| 196 | pub fn tinted(mut self) -> Self { |
| 197 | self.tinted = true; |
| 198 | self |
| 199 | } |
| 200 | |
| 201 | /// The mark's glyph for this terminal. |
| 202 | #[must_use] |
| 203 | pub fn glyph(&self, theme: &Theme) -> &'static str { |
| 204 | glyphs::pick(self.state.glyph(), theme.ascii()) |
| 205 | } |
| 206 | |
| 207 | #[must_use] |
| 208 | pub fn spans(&self, theme: &Theme) -> Vec<Span<'static>> { |
| 209 | self.spans_for_width(theme, None) |
| 210 | } |
| 211 | |
| 212 | fn spans_for_width(&self, theme: &Theme, max_width: Option<usize>) -> Vec<Span<'static>> { |
| 213 | let hue = theme.fg(self.state.role()); |
| 214 | let word_style = if self.tinted { |
| 215 | hue |
| 216 | } else { |
| 217 | theme.fg(Role::Foreground) |
| 218 | }; |
| 219 | let glyph = self.glyph(theme); |
| 220 | let safe_word = text::display_safe(&self.word).into_owned(); |
| 221 | let mut spans = vec![ |
| 222 | Span::styled(glyph, hue), |
| 223 | Span::raw(" "), |
| 224 | Span::styled(safe_word.clone(), word_style), |
| 225 | ]; |
| 226 | if let Some(raw_detail) = self.detail.as_deref() { |
| 227 | let safe_detail = text::display_safe(raw_detail); |
| 228 | let trimmed = safe_detail.trim(); |
| 229 | if !trimmed.is_empty() { |
| 230 | let sep = format!(" {} ", glyphs::pick(glyphs::NEUTRAL, theme.ascii())); |
| 231 | if let Some(limit) = max_width { |
| 232 | let prefix_width = text::width(glyph) + 1 + text::width(&safe_word); |
| 233 | let sep_width = text::width(&sep); |
| 234 | if let Some(avail) = limit.checked_sub(prefix_width + sep_width) |
| 235 | && avail > 0 |
| 236 | { |
| 237 | let fitted = text::truncate_words(trimmed, avail, theme.ascii()); |
| 238 | if !fitted.is_empty() { |
| 239 | spans.push(Span::styled(sep, theme.fg(Role::Muted))); |
| 240 | spans.push(Span::styled(fitted.into_owned(), theme.fg(Role::Muted))); |
| 241 | } |
| 242 | } |
| 243 | } else { |
| 244 | spans.push(Span::styled(sep, theme.fg(Role::Muted))); |
| 245 | spans.push(Span::styled(trimmed.to_owned(), theme.fg(Role::Muted))); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | spans |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | impl Paint for StatusMark { |
| 254 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 255 | let max_width = self.detail.as_ref().map(|_| usize::from(area.width)); |
| 256 | ratatui::text::Line::from(self.spans_for_width(theme, max_width)).render(area, buf); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | #[cfg(test)] |
| 261 | mod tests { |
| 262 | use super::*; |
| 263 | use crate::testing::Profile; |
| 264 | |
| 265 | #[test] |
| 266 | fn every_state_has_a_distinct_mark_and_a_word() { |
| 267 | for profile in [Profile::DarkTrue, Profile::Ascii] { |
| 268 | let theme = profile.theme(); |
| 269 | let marks: Vec<_> = State::ALL |
| 270 | .iter() |
| 271 | .map(|s| StatusMark::new(*s).glyph(&theme)) |
| 272 | .collect(); |
| 273 | for (i, a) in marks.iter().enumerate() { |
| 274 | assert!(!a.trim().is_empty()); |
| 275 | for b in &marks[i + 1..] { |
| 276 | assert_ne!(a, b, "{profile:?}: two states share {a}"); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | for state in State::ALL { |
| 281 | assert!(!state.word().is_empty()); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /// The default words are the English ones, and a host's words reach the |
| 286 | /// painted mark. |
| 287 | #[test] |
| 288 | fn words_are_parameters_with_english_defaults() { |
| 289 | let english = StateWords::default(); |
| 290 | for state in State::ALL { |
| 291 | assert_eq!(english.get(state), state.word()); |
| 292 | assert_eq!( |
| 293 | StatusMark::new(state), |
| 294 | StatusMark::with_words(state, &english) |
| 295 | ); |
| 296 | } |
| 297 | let host = StateWords { |
| 298 | needs_you: "Braucht dich".into(), |
| 299 | ..StateWords::default() |
| 300 | }; |
| 301 | let theme = Profile::DarkTrue.theme(); |
| 302 | let buf = crate::testing::render(20, 1, |area, buf| { |
| 303 | StatusMark::with_words(State::NeedsYou, &host).paint(area, buf, &theme); |
| 304 | }); |
| 305 | assert_eq!(crate::testing::text(&buf), "\u{25c6} Braucht dich"); |
| 306 | assert_eq!(host.get(State::Done), "Done"); |
| 307 | } |
| 308 | |
| 309 | #[test] |
| 310 | fn detail_renders_and_truncates_safely_in_unicode_and_ascii() { |
| 311 | let dark = Profile::DarkTrue.theme(); |
| 312 | let ascii = Profile::Ascii.theme(); |
| 313 | let mark = StatusMark::new(State::Blocked).detail("waiting on CI approval"); |
| 314 | |
| 315 | let wide = crate::testing::render(40, 1, |area, buf| mark.paint(area, buf, &dark)); |
| 316 | assert_eq!( |
| 317 | crate::testing::text(&wide), |
| 318 | "\u{2298} Blocked \u{00b7} waiting on CI approval" |
| 319 | ); |
| 320 | |
| 321 | let narrow = crate::testing::render(22, 1, |area, buf| mark.paint(area, buf, &dark)); |
| 322 | let narrow_text = crate::testing::text(&narrow); |
| 323 | assert!( |
| 324 | narrow_text.starts_with("\u{2298} Blocked \u{00b7} "), |
| 325 | "{narrow_text}" |
| 326 | ); |
| 327 | assert!(narrow_text.contains('\u{2026}'), "{narrow_text}"); |
| 328 | |
| 329 | let ascii_buf = crate::testing::render(22, 1, |area, buf| mark.paint(area, buf, &ascii)); |
| 330 | let ascii_text = crate::testing::text(&ascii_buf); |
| 331 | assert!(ascii_text.is_ascii(), "{ascii_text}"); |
| 332 | assert!(ascii_text.starts_with("! Blocked . "), "{ascii_text}"); |
| 333 | |
| 334 | let tiny = crate::testing::render(9, 1, |area, buf| mark.paint(area, buf, &dark)); |
| 335 | assert_eq!(crate::testing::text(&tiny), "\u{2298} Blocked"); |
| 336 | } |
| 337 | } |
| 338 |