|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +#[derive(Debug)] |
| 4 | +#[cfg_attr(test, derive(strum::EnumIter))] |
| 5 | +pub enum Icon { |
| 6 | + // Log level prefixes |
| 7 | + /// Group header prefix (›) |
| 8 | + GroupArrow, |
| 9 | + /// Task completion checkmark (✓) |
| 10 | + Checkmark, |
| 11 | + /// Error indicator (✗) |
| 12 | + Error, |
| 13 | + /// Warning indicator (⚠) |
| 14 | + Warning, |
| 15 | + /// Debug bullet point (·) |
| 16 | + Bullet, |
| 17 | + |
| 18 | + // Impact / result indicators |
| 19 | + /// Impact improved / up arrow (↑) |
| 20 | + ImpactUp, |
| 21 | + /// Impact regressed / down arrow (↓) |
| 22 | + ImpactDown, |
| 23 | + /// Impact neutral / filled circle (●) |
| 24 | + ImpactNeutral, |
| 25 | + /// No impact data / empty circle (○) |
| 26 | + ImpactNoData, |
| 27 | + |
| 28 | + // Executor icons |
| 29 | + /// CPU simulation executor (⚙) |
| 30 | + ExecutorValgrind, |
| 31 | + /// Walltime executor (⏱) |
| 32 | + ExecutorWallTime, |
| 33 | + /// Memory executor (▤) |
| 34 | + ExecutorMemory, |
| 35 | + |
| 36 | + // Box-drawing (used by rolling buffer) |
| 37 | + /// Top-left arc corner (╭) |
| 38 | + BoxTopLeft, |
| 39 | + /// Top-right arc corner (╮) |
| 40 | + BoxTopRight, |
| 41 | + /// Bottom-left arc corner (╰) |
| 42 | + BoxBottomLeft, |
| 43 | + /// Bottom-right arc corner (╯) |
| 44 | + BoxBottomRight, |
| 45 | + /// Horizontal line (─) |
| 46 | + BoxHorizontal, |
| 47 | + /// Vertical line (│) |
| 48 | + BoxVertical, |
| 49 | + /// T-junction pointing down (┬) |
| 50 | + BoxTDown, |
| 51 | + /// T-junction pointing right (├) |
| 52 | + BoxTRight, |
| 53 | + /// T-junction pointing left (┤) |
| 54 | + BoxTLeft, |
| 55 | + |
| 56 | + // Miscellaneous |
| 57 | + /// Horizontal separator line character (─), same glyph as BoxHorizontal |
| 58 | + Separator, |
| 59 | + /// Truncation ellipsis (…) |
| 60 | + Ellipsis, |
| 61 | +} |
| 62 | + |
| 63 | +impl Icon { |
| 64 | + /// Return the icon as a `char`. Panics if the icon's string is not a single character |
| 65 | + /// (all current icons are single codepoints, so this is always safe). |
| 66 | + pub fn as_char(&self) -> char { |
| 67 | + self.to_string() |
| 68 | + .chars() |
| 69 | + .next() |
| 70 | + .expect("icon must be a single character") |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl fmt::Display for Icon { |
| 75 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 76 | + let ch = match self { |
| 77 | + Icon::GroupArrow => "\u{203a}", // › |
| 78 | + Icon::Checkmark => "\u{2713}", // ✓ |
| 79 | + Icon::Error => "\u{2717}", // ✗ |
| 80 | + Icon::Warning => "\u{26a0}", // ⚠ |
| 81 | + Icon::Bullet => "\u{00b7}", // · |
| 82 | + Icon::ImpactUp => "\u{2191}", // ↑ |
| 83 | + Icon::ImpactDown => "\u{2193}", // ↓ |
| 84 | + Icon::ImpactNeutral => "\u{25cf}", // ● |
| 85 | + Icon::ImpactNoData => "\u{25cb}", // ○ |
| 86 | + Icon::ExecutorValgrind => "\u{2699}", // ⚙ |
| 87 | + Icon::ExecutorWallTime => "\u{23f1}", // ⏱ |
| 88 | + Icon::ExecutorMemory => "\u{25a4}", // ▤ |
| 89 | + Icon::BoxTopLeft => "\u{256d}", // ╭ |
| 90 | + Icon::BoxTopRight => "\u{256e}", // ╮ |
| 91 | + Icon::BoxBottomLeft => "\u{2570}", // ╰ |
| 92 | + Icon::BoxBottomRight => "\u{256f}", // ╯ |
| 93 | + Icon::BoxHorizontal => "\u{2500}", // ─ |
| 94 | + Icon::BoxVertical => "\u{2502}", // │ |
| 95 | + Icon::BoxTDown => "\u{252c}", // ┬ |
| 96 | + Icon::BoxTRight => "\u{251c}", // ├ |
| 97 | + Icon::BoxTLeft => "\u{2524}", // ┤ |
| 98 | + Icon::Separator => "\u{2500}", // ─ |
| 99 | + Icon::Ellipsis => "\u{2026}", // … |
| 100 | + }; |
| 101 | + f.write_str(ch) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use strum::IntoEnumIterator; |
| 108 | + |
| 109 | + use super::*; |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_icon_rendering() { |
| 113 | + let rendered = Icon::iter() |
| 114 | + .map(|icon| format!("{icon:?}: {icon}")) |
| 115 | + .collect::<Vec<_>>() |
| 116 | + .join("\n"); |
| 117 | + |
| 118 | + insta::assert_snapshot!(rendered); |
| 119 | + } |
| 120 | +} |
0 commit comments