| 1 | //! Small marks, one cell each: sonar, tide, shell, kelp, and the v5.5.0 ADE marks. |
| 2 | //! |
| 3 | //! Drawn in the spirit of Susan Kare's icons: precise, friendly, and each |
| 4 | //! with a word for screen readers and an ASCII form for plain terminals. They |
| 5 | //! are marks, not characters: the whale is the only character, and none of |
| 6 | //! these stands for a product concept on its own. Each sits beside words. |
| 7 | |
| 8 | use std::time::Duration; |
| 9 | |
| 10 | use ratatui::{buffer::Buffer, layout::Rect, text::Span, widgets::Widget}; |
| 11 | |
| 12 | use crate::{MotionMode, Paint, Role, Theme}; |
| 13 | |
| 14 | /// A small mark. |
| 15 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 16 | pub enum Icon { |
| 17 | /// Searching: a ping that widens while the search runs. |
| 18 | Sonar { |
| 19 | elapsed: Duration, |
| 20 | motion: MotionMode, |
| 21 | }, |
| 22 | /// Real progress, drawn only when the total is a fact. |
| 23 | Tide { done: u32, total: u32 }, |
| 24 | /// A receipt: something finished and left a record. |
| 25 | Shell, |
| 26 | /// A quiet divider between items on one line. |
| 27 | Kelp, |
| 28 | /// Fast action or high-energy trigger (Design 5.5.0 ADE icon). |
| 29 | Bolt, |
| 30 | /// Compute, model runtime, or local engine (Design 5.5.0 ADE icon). |
| 31 | Cpu, |
| 32 | /// Stacked context, workspaces, or layers (Design 5.5.0 ADE icon). |
| 33 | Layers, |
| 34 | /// Orbiting subagents or background loop (Design 5.5.0 ADE icon). |
| 35 | Orbit, |
| 36 | /// Discovery, scan, or live telemetry sweep (Design 5.5.0 ADE icon). |
| 37 | Radar, |
| 38 | /// AI assistance or generated suggestion (Design 5.5.0 ADE icon). |
| 39 | Sparkle, |
| 40 | } |
| 41 | |
| 42 | /// A ping widening: small ring, dotted ring, ring with its centre. None of |
| 43 | /// these is a state mark (`○` is "ready", `·` a separator). |
| 44 | const SONAR: [&str; 3] = ["∘", "◌", "◎"]; |
| 45 | const TIDE: [&str; 8] = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"]; |
| 46 | /// `.` and `#` are the ASCII marks for "working" and "stopped", so the |
| 47 | /// ASCII tide rises through other marks. |
| 48 | const TIDE_ASCII: [&str; 5] = ["_", ",", "-", "=", "%"]; |
| 49 | |
| 50 | impl Icon { |
| 51 | /// A tide mark for `done` of `total`, or `None` when the total is |
| 52 | /// unknown: no fake progress. |
| 53 | #[must_use] |
| 54 | pub fn tide(done: u32, total: u32) -> Option<Self> { |
| 55 | (total > 0).then_some(Self::Tide { |
| 56 | done: done.min(total), |
| 57 | total, |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | /// The word a screen reader hears. |
| 62 | #[must_use] |
| 63 | pub fn label(&self) -> String { |
| 64 | match self { |
| 65 | Icon::Sonar { .. } => "searching".into(), |
| 66 | Icon::Tide { done, total } => format!("{done} of {total}"), |
| 67 | Icon::Shell => "receipt".into(), |
| 68 | Icon::Kelp => "divider".into(), |
| 69 | Icon::Bolt => "bolt".into(), |
| 70 | Icon::Cpu => "cpu".into(), |
| 71 | Icon::Layers => "layers".into(), |
| 72 | Icon::Orbit => "orbit".into(), |
| 73 | Icon::Radar => "radar".into(), |
| 74 | Icon::Sparkle => "sparkle".into(), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[must_use] |
| 79 | pub fn role(&self) -> Role { |
| 80 | match self { |
| 81 | Icon::Sonar { .. } | Icon::Tide { .. } | Icon::Radar => Role::Live, |
| 82 | Icon::Bolt => Role::Attention, |
| 83 | Icon::Orbit | Icon::Sparkle => Role::Accent, |
| 84 | Icon::Shell | Icon::Cpu | Icon::Layers => Role::Muted, |
| 85 | Icon::Kelp => Role::Border, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// The glyph for this terminal and moment. |
| 90 | #[must_use] |
| 91 | pub fn glyph(&self, theme: &Theme) -> &'static str { |
| 92 | let ascii = theme.ascii(); |
| 93 | match *self { |
| 94 | Icon::Sonar { elapsed, motion } => { |
| 95 | if ascii { |
| 96 | // `o` is "ready" in ASCII; a sound wave is not. |
| 97 | ")" |
| 98 | } else if motion.animates() { |
| 99 | SONAR[((elapsed.as_millis() / 250) % SONAR.len() as u128) as usize] |
| 100 | } else { |
| 101 | SONAR[SONAR.len() - 1] |
| 102 | } |
| 103 | } |
| 104 | Icon::Tide { done, total } => { |
| 105 | let table: &[&str] = if ascii { &TIDE_ASCII } else { &TIDE }; |
| 106 | let last = table.len() - 1; |
| 107 | let idx = (u64::from(done) * last as u64 + u64::from(total) / 2) / u64::from(total); |
| 108 | table[idx as usize] |
| 109 | } |
| 110 | Icon::Shell => { |
| 111 | if ascii { |
| 112 | "=" |
| 113 | } else { |
| 114 | "◒" |
| 115 | } |
| 116 | } |
| 117 | Icon::Kelp => { |
| 118 | if ascii { |
| 119 | "|" |
| 120 | } else { |
| 121 | "┊" |
| 122 | } |
| 123 | } |
| 124 | Icon::Bolt => { |
| 125 | if ascii { |
| 126 | "/" |
| 127 | } else { |
| 128 | "↯" |
| 129 | } |
| 130 | } |
| 131 | Icon::Cpu => { |
| 132 | if ascii { |
| 133 | "+" |
| 134 | } else { |
| 135 | "⊞" |
| 136 | } |
| 137 | } |
| 138 | Icon::Layers => { |
| 139 | if ascii { |
| 140 | ":" |
| 141 | } else { |
| 142 | "≡" |
| 143 | } |
| 144 | } |
| 145 | Icon::Orbit => { |
| 146 | if ascii { |
| 147 | "@" |
| 148 | } else { |
| 149 | "⊕" |
| 150 | } |
| 151 | } |
| 152 | Icon::Radar => { |
| 153 | if ascii { |
| 154 | "^" |
| 155 | } else { |
| 156 | "⌖" |
| 157 | } |
| 158 | } |
| 159 | Icon::Sparkle => { |
| 160 | if ascii { |
| 161 | "&" |
| 162 | } else { |
| 163 | "✦" |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[must_use] |
| 170 | pub fn span(&self, theme: &Theme) -> Span<'static> { |
| 171 | Span::styled(self.glyph(theme), theme.fg(self.role())) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | impl Paint for Icon { |
| 176 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 177 | self.span(theme).render(area, buf); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | #[cfg(test)] |
| 182 | mod tests { |
| 183 | use super::*; |
| 184 | use crate::testing::Profile; |
| 185 | |
| 186 | #[test] |
| 187 | fn tide_needs_a_real_total() { |
| 188 | assert_eq!(Icon::tide(3, 0), None); |
| 189 | let theme = Profile::DarkTrue.theme(); |
| 190 | assert_eq!(Icon::tide(0, 5).unwrap().glyph(&theme), "▁"); |
| 191 | assert_eq!(Icon::tide(5, 5).unwrap().glyph(&theme), "█"); |
| 192 | assert_eq!(Icon::tide(9, 5).unwrap().label(), "5 of 5"); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn every_icon_has_a_word_and_an_ascii_form() { |
| 197 | let ascii = Profile::Ascii.theme(); |
| 198 | let mut icons: Vec<Icon> = (0..1000) |
| 199 | .step_by(250) |
| 200 | .map(|ms| Icon::Sonar { |
| 201 | elapsed: Duration::from_millis(ms), |
| 202 | motion: MotionMode::Full, |
| 203 | }) |
| 204 | .collect(); |
| 205 | icons.extend((0..=4).map(|done| Icon::tide(done, 4).unwrap())); |
| 206 | icons.extend([ |
| 207 | Icon::Shell, |
| 208 | Icon::Kelp, |
| 209 | Icon::Bolt, |
| 210 | Icon::Cpu, |
| 211 | Icon::Layers, |
| 212 | Icon::Orbit, |
| 213 | Icon::Radar, |
| 214 | Icon::Sparkle, |
| 215 | ]); |
| 216 | let state_marks: Vec<&str> = crate::State::ALL |
| 217 | .iter() |
| 218 | .flat_map(|s| [s.glyph(), crate::glyphs::pick(s.glyph(), true)]) |
| 219 | .collect(); |
| 220 | for icon in icons { |
| 221 | for profile in [Profile::DarkTrue, Profile::Ascii] { |
| 222 | let glyph = icon.glyph(&profile.theme()); |
| 223 | assert!( |
| 224 | !state_marks.contains(&glyph), |
| 225 | "{icon:?} borrows the state mark {glyph}" |
| 226 | ); |
| 227 | } |
| 228 | assert!(!icon.label().is_empty()); |
| 229 | assert!(icon.glyph(&ascii).is_ascii(), "{icon:?}"); |
| 230 | assert_eq!( |
| 231 | crate::text::width(icon.glyph(&Profile::DarkTrue.theme())), |
| 232 | 1 |
| 233 | ); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 |