| 1 | use ratatui::style::Style; |
| 2 | use ratatui::text::{Line, Span}; |
| 3 | use unicode_width::UnicodeWidthStr; |
| 4 | |
| 5 | use crate::tui::color_compat::ascii_safe_enabled; |
| 6 | use codewhale_palette as palette; |
| 7 | |
| 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 | pub enum ApprovalReceiptOutcome { |
| 10 | AllowedOnce, |
| 11 | AllowedInConversation, |
| 12 | AlwaysAllowedInRepo, |
| 13 | Denied, |
| 14 | } |
| 15 | |
| 16 | impl ApprovalReceiptOutcome { |
| 17 | #[must_use] |
| 18 | pub fn label(self) -> &'static str { |
| 19 | match self { |
| 20 | Self::AllowedOnce => "Allowed once", |
| 21 | Self::AllowedInConversation => "Allowed in this conversation", |
| 22 | Self::AlwaysAllowedInRepo => "Always allowed in this repo", |
| 23 | Self::Denied => "Denied", |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn glyph(self, ascii: bool) -> &'static str { |
| 28 | match (self, ascii) { |
| 29 | (Self::Denied, true) => "x", |
| 30 | (Self::Denied, false) => "✕", |
| 31 | (_, true) => "v", |
| 32 | (_, false) => "✓", |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 38 | pub struct ApprovalReceiptCell { |
| 39 | pub outcome: ApprovalReceiptOutcome, |
| 40 | pub subject: String, |
| 41 | } |
| 42 | |
| 43 | impl ApprovalReceiptCell { |
| 44 | #[must_use] |
| 45 | pub fn new(outcome: ApprovalReceiptOutcome, subject: impl Into<String>) -> Self { |
| 46 | Self { |
| 47 | outcome, |
| 48 | subject: subject.into(), |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[must_use] |
| 53 | pub fn plain_summary(&self) -> String { |
| 54 | let subject = codewhale_ratatui::visible_text(&self.subject, false); |
| 55 | format!("{} · {subject}", self.outcome.label()) |
| 56 | } |
| 57 | |
| 58 | pub(crate) fn render(&self, width: u16) -> Vec<Line<'static>> { |
| 59 | let ascii = ascii_safe_enabled(); |
| 60 | let leader = if ascii { "- " } else { "─ " }; |
| 61 | let sep = if ascii { " - " } else { " · " }; |
| 62 | let prefix = format!( |
| 63 | "{leader}{} {}{sep}", |
| 64 | self.outcome.glyph(ascii), |
| 65 | self.outcome.label() |
| 66 | ); |
| 67 | let prefix_width = UnicodeWidthStr::width(prefix.as_str()); |
| 68 | let budget = usize::from(width); |
| 69 | if budget <= prefix_width { |
| 70 | let truncated = codewhale_localization::truncate_to_width(&prefix, budget); |
| 71 | return vec![Line::from(Span::styled( |
| 72 | truncated, |
| 73 | Style::default().fg(palette::TEXT_MUTED), |
| 74 | ))]; |
| 75 | } |
| 76 | let subject_budget = budget.saturating_sub(prefix_width); |
| 77 | let visible_subject = codewhale_ratatui::visible_text(&self.subject, ascii); |
| 78 | let subject = codewhale_localization::truncate_to_width(&visible_subject, subject_budget); |
| 79 | vec![Line::from(vec![ |
| 80 | Span::styled(prefix, Style::default().fg(palette::TEXT_MUTED)), |
| 81 | Span::styled(subject, Style::default().fg(palette::TEXT_PRIMARY)), |
| 82 | ])] |
| 83 | } |
| 84 | } |
| 85 |