| 1 | //! Calm semantic notices: a mark, a title, optional body, and optional actions. |
| 2 | |
| 3 | use std::borrow::Cow; |
| 4 | |
| 5 | use ratatui::{ |
| 6 | buffer::Buffer, |
| 7 | layout::Rect, |
| 8 | style::Modifier, |
| 9 | text::{Line, Span}, |
| 10 | widgets::Widget, |
| 11 | }; |
| 12 | |
| 13 | use crate::{KeyHint, Paint, Role, Theme, glyphs, text}; |
| 14 | |
| 15 | /// Semantic tone of a [`Notice`]. Each tone pairs a distinct 1-cell mark with |
| 16 | /// a semantic [`Role`] so meaning never relies on color alone. |
| 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 18 | pub enum NoticeTone { |
| 19 | /// Neutral informational banner. |
| 20 | Info, |
| 21 | /// Caution or attention required. |
| 22 | Warning, |
| 23 | /// Confirmed positive outcome. |
| 24 | Success, |
| 25 | /// Failure or blocked state. |
| 26 | Error, |
| 27 | } |
| 28 | |
| 29 | impl NoticeTone { |
| 30 | /// Every notice tone in canonical order. |
| 31 | pub const ALL: [NoticeTone; 4] = [ |
| 32 | NoticeTone::Info, |
| 33 | NoticeTone::Warning, |
| 34 | NoticeTone::Success, |
| 35 | NoticeTone::Error, |
| 36 | ]; |
| 37 | |
| 38 | /// Semantic theme role for this tone. |
| 39 | #[must_use] |
| 40 | pub const fn role(self) -> Role { |
| 41 | match self { |
| 42 | NoticeTone::Info => Role::Accent, |
| 43 | NoticeTone::Warning => Role::Attention, |
| 44 | NoticeTone::Success => Role::Live, |
| 45 | NoticeTone::Error => Role::Danger, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// 1-cell Unicode glyph for this tone. |
| 50 | #[must_use] |
| 51 | pub const fn glyph(self) -> &'static str { |
| 52 | match self { |
| 53 | NoticeTone::Info => glyphs::CURRENT, |
| 54 | NoticeTone::Warning => glyphs::ATTENTION, |
| 55 | NoticeTone::Success => glyphs::DONE, |
| 56 | NoticeTone::Error => glyphs::FAILED, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Short English label for this tone. |
| 61 | #[must_use] |
| 62 | pub const fn label(self) -> &'static str { |
| 63 | match self { |
| 64 | NoticeTone::Info => "Info", |
| 65 | NoticeTone::Warning => "Warning", |
| 66 | NoticeTone::Success => "Success", |
| 67 | NoticeTone::Error => "Error", |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// Inline semantic notice banner matching Desktop `packages/ui` `<Notice>`. |
| 73 | /// |
| 74 | /// Paints a calm tonal [`Role::Surface`] ground where terminal grounds differ |
| 75 | /// and a semantic left rail on every profile, ensuring clear separation on |
| 76 | /// Ansi16, `NO_COLOR`, and ASCII-safe terminals. |
| 77 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 78 | pub struct Notice<'a> { |
| 79 | /// Semantic tone determining icon and accent role. |
| 80 | pub tone: NoticeTone, |
| 81 | /// Primary single-line title. |
| 82 | pub title: Cow<'a, str>, |
| 83 | /// Optional supporting prose body. |
| 84 | pub body: Option<Cow<'a, str>>, |
| 85 | /// Optional primary action hint. |
| 86 | pub action: Option<KeyHint>, |
| 87 | /// Optional dismiss cue (e.g. `Esc dismiss`). |
| 88 | pub dismiss: Option<KeyHint>, |
| 89 | } |
| 90 | |
| 91 | impl<'a> Notice<'a> { |
| 92 | /// Create a notice with the given `tone` and `title`. |
| 93 | #[must_use] |
| 94 | pub fn new(tone: NoticeTone, title: impl Into<Cow<'a, str>>) -> Self { |
| 95 | Self { |
| 96 | tone, |
| 97 | title: title.into(), |
| 98 | body: None, |
| 99 | action: None, |
| 100 | dismiss: None, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// Create an informational notice. |
| 105 | #[must_use] |
| 106 | pub fn info(title: impl Into<Cow<'a, str>>) -> Self { |
| 107 | Self::new(NoticeTone::Info, title) |
| 108 | } |
| 109 | |
| 110 | /// Create a warning notice. |
| 111 | #[must_use] |
| 112 | pub fn warning(title: impl Into<Cow<'a, str>>) -> Self { |
| 113 | Self::new(NoticeTone::Warning, title) |
| 114 | } |
| 115 | |
| 116 | /// Create a success notice. |
| 117 | #[must_use] |
| 118 | pub fn success(title: impl Into<Cow<'a, str>>) -> Self { |
| 119 | Self::new(NoticeTone::Success, title) |
| 120 | } |
| 121 | |
| 122 | /// Create an error notice. |
| 123 | #[must_use] |
| 124 | pub fn error(title: impl Into<Cow<'a, str>>) -> Self { |
| 125 | Self::new(NoticeTone::Error, title) |
| 126 | } |
| 127 | |
| 128 | /// Attach optional supporting body copy. |
| 129 | #[must_use] |
| 130 | pub fn body(mut self, body: impl Into<Cow<'a, str>>) -> Self { |
| 131 | self.body = Some(body.into()); |
| 132 | self |
| 133 | } |
| 134 | |
| 135 | /// Attach an optional action key hint. |
| 136 | #[must_use] |
| 137 | pub fn action(mut self, action: KeyHint) -> Self { |
| 138 | self.action = Some(action); |
| 139 | self |
| 140 | } |
| 141 | |
| 142 | /// Toggle a default `Esc dismiss` cue. |
| 143 | #[must_use] |
| 144 | pub fn dismissable(mut self, dismissable: bool) -> Self { |
| 145 | self.dismiss = dismissable.then(|| KeyHint::new("Esc", "dismiss")); |
| 146 | self |
| 147 | } |
| 148 | |
| 149 | /// Attach a custom or localized dismiss cue. |
| 150 | #[must_use] |
| 151 | pub fn dismiss_hint(mut self, dismiss: KeyHint) -> Self { |
| 152 | self.dismiss = Some(dismiss); |
| 153 | self |
| 154 | } |
| 155 | |
| 156 | fn has_footer(&self) -> bool { |
| 157 | self.action.is_some() || self.dismiss.is_some() |
| 158 | } |
| 159 | |
| 160 | fn body_lines(&self, content_width: usize) -> Vec<String> { |
| 161 | let Some(body) = self.body.as_deref() else { |
| 162 | return Vec::new(); |
| 163 | }; |
| 164 | if content_width == 0 { |
| 165 | return Vec::new(); |
| 166 | } |
| 167 | let safe = text::display_safe(body); |
| 168 | wrap_words(&safe, content_width) |
| 169 | } |
| 170 | |
| 171 | fn content_width(width: u16) -> usize { |
| 172 | usize::from(width).saturating_sub(2) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | impl Paint for Notice<'_> { |
| 177 | fn height(&self, width: u16, _theme: &Theme) -> u16 { |
| 178 | if width == 0 { |
| 179 | return 0; |
| 180 | } |
| 181 | let content_width = Self::content_width(width); |
| 182 | let body_rows = self.body_lines(content_width).len(); |
| 183 | let footer_rows = usize::from(self.has_footer()); |
| 184 | u16::try_from(1 + body_rows + footer_rows).unwrap_or(u16::MAX) |
| 185 | } |
| 186 | |
| 187 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 188 | let area = area.intersection(buf.area); |
| 189 | if area.is_empty() { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | let ascii = theme.ascii(); |
| 194 | let tonal = theme.grounds_differ(Role::Surface, Role::Background); |
| 195 | let surface_bg = if tonal { |
| 196 | theme.bg(Role::Surface) |
| 197 | } else { |
| 198 | ratatui::style::Style::default() |
| 199 | }; |
| 200 | |
| 201 | if tonal { |
| 202 | for y in area.y..area.y.saturating_add(area.height) { |
| 203 | for x in area.x..area.x.saturating_add(area.width) { |
| 204 | if let Some(cell) = buf.cell_mut((x, y)) { |
| 205 | cell.set_style(surface_bg); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | let rail = glyphs::pick("▏", ascii); |
| 212 | let rail_style = theme.fg(self.tone.role()).patch(surface_bg); |
| 213 | let mark = glyphs::pick(self.tone.glyph(), ascii); |
| 214 | let mark_style = theme.fg(self.tone.role()).patch(surface_bg); |
| 215 | let title_style = theme |
| 216 | .fg(Role::Foreground) |
| 217 | .add_modifier(Modifier::BOLD) |
| 218 | .patch(surface_bg); |
| 219 | let body_style = theme.fg(Role::Muted).patch(surface_bg); |
| 220 | |
| 221 | let content_width = Self::content_width(area.width); |
| 222 | let body_lines = self.body_lines(content_width); |
| 223 | let total_rows = area.height; |
| 224 | |
| 225 | let mut row = 0u16; |
| 226 | if row < total_rows { |
| 227 | let y = area.y + row; |
| 228 | if area.width <= 2 { |
| 229 | let spans = if area.width == 1 { |
| 230 | vec![Span::styled(mark, mark_style)] |
| 231 | } else { |
| 232 | vec![ |
| 233 | Span::styled(rail, rail_style), |
| 234 | Span::styled(mark, mark_style), |
| 235 | ] |
| 236 | }; |
| 237 | Line::from(spans).render( |
| 238 | Rect { |
| 239 | x: area.x, |
| 240 | y, |
| 241 | width: area.width, |
| 242 | height: 1, |
| 243 | }, |
| 244 | buf, |
| 245 | ); |
| 246 | } else { |
| 247 | let mut spans = vec![ |
| 248 | Span::styled(rail, rail_style), |
| 249 | Span::styled(" ", surface_bg), |
| 250 | ]; |
| 251 | if content_width > 0 { |
| 252 | spans.push(Span::styled(mark, mark_style)); |
| 253 | let title_budget = content_width.saturating_sub(text::width(mark) + 1); |
| 254 | if title_budget > 0 { |
| 255 | let safe_title = text::display_safe(&self.title); |
| 256 | let clipped = text::truncate_words(&safe_title, title_budget, ascii); |
| 257 | spans.push(Span::styled(" ", surface_bg)); |
| 258 | spans.push(Span::styled(clipped.into_owned(), title_style)); |
| 259 | } |
| 260 | } |
| 261 | Line::from(spans).render( |
| 262 | Rect { |
| 263 | x: area.x, |
| 264 | y, |
| 265 | width: area.width, |
| 266 | height: 1, |
| 267 | }, |
| 268 | buf, |
| 269 | ); |
| 270 | } |
| 271 | row += 1; |
| 272 | } |
| 273 | |
| 274 | let reserved_footer = u16::from(self.has_footer() && total_rows > 1); |
| 275 | let body_limit = total_rows.saturating_sub(row + reserved_footer); |
| 276 | for line in body_lines.into_iter().take(usize::from(body_limit)) { |
| 277 | let y = area.y + row; |
| 278 | let clipped = text::truncate_words(&line, content_width, ascii); |
| 279 | let spans = vec![ |
| 280 | Span::styled(rail, rail_style), |
| 281 | Span::styled(" ", surface_bg), |
| 282 | Span::styled(clipped.into_owned(), body_style), |
| 283 | ]; |
| 284 | Line::from(spans).render( |
| 285 | Rect { |
| 286 | x: area.x, |
| 287 | y, |
| 288 | width: area.width, |
| 289 | height: 1, |
| 290 | }, |
| 291 | buf, |
| 292 | ); |
| 293 | row += 1; |
| 294 | } |
| 295 | |
| 296 | if self.has_footer() && row < total_rows { |
| 297 | let y = area.y + row; |
| 298 | let sep = if ascii { " " } else { " · " }; |
| 299 | let mut parts = Vec::new(); |
| 300 | if let Some(action) = &self.action { |
| 301 | parts.push(format_hint(action)); |
| 302 | } |
| 303 | if let Some(dismiss) = &self.dismiss { |
| 304 | parts.push(format_hint(dismiss)); |
| 305 | } |
| 306 | let joined = parts.join(sep); |
| 307 | let clipped = text::truncate(&joined, content_width, ascii); |
| 308 | let spans = vec![ |
| 309 | Span::styled(rail, rail_style), |
| 310 | Span::styled(" ", surface_bg), |
| 311 | Span::styled(clipped.into_owned(), body_style), |
| 312 | ]; |
| 313 | Line::from(spans).render( |
| 314 | Rect { |
| 315 | x: area.x, |
| 316 | y, |
| 317 | width: area.width, |
| 318 | height: 1, |
| 319 | }, |
| 320 | buf, |
| 321 | ); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | fn format_hint(hint: &KeyHint) -> String { |
| 327 | let keys = text::display_safe(&hint.keys); |
| 328 | let verb = text::display_safe(&hint.verb); |
| 329 | format!("{keys} {verb}") |
| 330 | } |
| 331 | |
| 332 | fn wrap_words(input: &str, max_width: usize) -> Vec<String> { |
| 333 | if max_width == 0 { |
| 334 | return Vec::new(); |
| 335 | } |
| 336 | let mut lines = Vec::new(); |
| 337 | for raw_line in input.lines() { |
| 338 | let mut current = String::new(); |
| 339 | let mut current_w = 0usize; |
| 340 | for word in raw_line.split_whitespace() { |
| 341 | let w = text::width(word); |
| 342 | if current.is_empty() { |
| 343 | current.push_str(word); |
| 344 | current_w = w; |
| 345 | } else if current_w + 1 + w <= max_width { |
| 346 | current.push(' '); |
| 347 | current.push_str(word); |
| 348 | current_w += 1 + w; |
| 349 | } else { |
| 350 | lines.push(current); |
| 351 | current = word.to_owned(); |
| 352 | current_w = w; |
| 353 | } |
| 354 | } |
| 355 | if !current.is_empty() { |
| 356 | lines.push(current); |
| 357 | } |
| 358 | } |
| 359 | lines |
| 360 | } |
| 361 |