返回 CodeWhale
checks.rs
1 //! Semantic CI/check summary badges (`✓ Passing`, `✕ Failing (2)`).
2
3 use std::borrow::Cow;
4
5 use ratatui::{
6 buffer::Buffer,
7 layout::Rect,
8 text::{Line, Span},
9 widgets::Widget,
10 };
11
12 use crate::{Paint, Role, Theme, glyphs, text};
13
14 /// Outcome of a set of automated checks.
15 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16 pub enum ChecksState {
17 /// All checks succeeded.
18 Passing,
19 /// One or more checks failed.
20 Failing,
21 /// Checks are still running or queued.
22 Pending,
23 /// Checks were skipped or not configured.
24 Skipped,
25 }
26
27 impl ChecksState {
28 /// Every check state in canonical order.
29 pub const ALL: [ChecksState; 4] = [
30 ChecksState::Passing,
31 ChecksState::Failing,
32 ChecksState::Pending,
33 ChecksState::Skipped,
34 ];
35
36 /// Semantic theme [`Role`] for this check state.
37 #[must_use]
38 pub const fn role(self) -> Role {
39 match self {
40 ChecksState::Passing => Role::Live,
41 ChecksState::Failing => Role::Danger,
42 ChecksState::Pending => Role::Attention,
43 ChecksState::Skipped => Role::Muted,
44 }
45 }
46
47 /// Distinct 1-cell Unicode glyph for this check state.
48 #[must_use]
49 pub const fn glyph(self) -> &'static str {
50 match self {
51 ChecksState::Passing => glyphs::DONE,
52 ChecksState::Failing => glyphs::FAILED,
53 ChecksState::Pending => glyphs::ATTENTION,
54 ChecksState::Skipped => glyphs::READY,
55 }
56 }
57
58 /// Default English word for this check state.
59 #[must_use]
60 pub const fn word(self) -> &'static str {
61 ChecksWords::english(self)
62 }
63 }
64
65 /// Localized words for [`ChecksMark`]. `Default` is English.
66 #[derive(Clone, Debug, PartialEq, Eq)]
67 pub struct ChecksWords {
68 /// Word for [`ChecksState::Passing`].
69 pub passing: Cow<'static, str>,
70 /// Word for [`ChecksState::Failing`].
71 pub failing: Cow<'static, str>,
72 /// Word for [`ChecksState::Pending`].
73 pub pending: Cow<'static, str>,
74 /// Word for [`ChecksState::Skipped`].
75 pub skipped: Cow<'static, str>,
76 }
77
78 impl ChecksWords {
79 /// English default word for `state`.
80 #[must_use]
81 pub const fn english(state: ChecksState) -> &'static str {
82 match state {
83 ChecksState::Passing => "Passing",
84 ChecksState::Failing => "Failing",
85 ChecksState::Pending => "Pending",
86 ChecksState::Skipped => "Skipped",
87 }
88 }
89
90 /// Look up the word for `state`.
91 #[must_use]
92 pub fn get(&self, state: ChecksState) -> &str {
93 match state {
94 ChecksState::Passing => &self.passing,
95 ChecksState::Failing => &self.failing,
96 ChecksState::Pending => &self.pending,
97 ChecksState::Skipped => &self.skipped,
98 }
99 }
100 }
101
102 impl Default for ChecksWords {
103 fn default() -> Self {
104 Self {
105 passing: Cow::Borrowed(Self::english(ChecksState::Passing)),
106 failing: Cow::Borrowed(Self::english(ChecksState::Failing)),
107 pending: Cow::Borrowed(Self::english(ChecksState::Pending)),
108 skipped: Cow::Borrowed(Self::english(ChecksState::Skipped)),
109 }
110 }
111 }
112
113 /// Semantic CI/check badge matching Desktop `packages/ui` `<ChecksMark>`.
114 ///
115 /// Always pairs a distinct 1-cell glyph with a word (and optional count), so
116 /// check status never relies on color alone.
117 #[derive(Clone, Debug, PartialEq, Eq)]
118 pub struct ChecksMark {
119 /// Semantic check outcome.
120 pub state: ChecksState,
121 /// Displayed label word.
122 pub word: Cow<'static, str>,
123 /// Optional check count shown beside the word (`(12)`).
124 pub count: Option<usize>,
125 /// Paint the word in the state's semantic role instead of [`Role::Foreground`].
126 pub tinted: bool,
127 }
128
129 impl ChecksMark {
130 /// Create a check badge with the default English word for `state`.
131 #[must_use]
132 pub fn new(state: ChecksState) -> Self {
133 Self {
134 state,
135 word: Cow::Borrowed(state.word()),
136 count: None,
137 tinted: false,
138 }
139 }
140
141 /// Create a check badge using localized [`ChecksWords`].
142 #[must_use]
143 pub fn with_words(state: ChecksState, words: &ChecksWords) -> Self {
144 Self {
145 state,
146 word: Cow::Owned(words.get(state).to_owned()),
147 count: None,
148 tinted: false,
149 }
150 }
151
152 /// Override the displayed word.
153 #[must_use]
154 pub fn word(mut self, word: impl Into<Cow<'static, str>>) -> Self {
155 self.word = word.into();
156 self
157 }
158
159 /// Attach an optional check count.
160 #[must_use]
161 pub const fn count(mut self, count: usize) -> Self {
162 self.count = Some(count);
163 self
164 }
165
166 /// Tint the word with the state's semantic role.
167 #[must_use]
168 pub const fn tinted(mut self, tinted: bool) -> Self {
169 self.tinted = tinted;
170 self
171 }
172
173 /// Total display width in cells when rendered without truncation.
174 #[must_use]
175 pub fn width(&self, ascii: bool) -> usize {
176 let mark_w = text::width(glyphs::pick(self.state.glyph(), ascii));
177 let word_w = text::width(&text::display_safe(&self.word));
178 let count_w = self.count.map_or(0, |n| text::width(&format!(" ({n})")));
179 mark_w + 1 + word_w + count_w
180 }
181
182 /// Styled spans for inline composition.
183 #[must_use]
184 pub fn spans(&self, theme: &Theme) -> Vec<Span<'static>> {
185 let mark = glyphs::pick(self.state.glyph(), theme.ascii());
186 let word = text::display_safe(&self.word).into_owned();
187 let word_role = if self.tinted {
188 self.state.role()
189 } else {
190 Role::Foreground
191 };
192 let mut spans = vec![
193 Span::styled(mark, theme.fg(self.state.role())),
194 Span::raw(" "),
195 Span::styled(word, theme.fg(word_role)),
196 ];
197 if let Some(count) = self.count {
198 spans.push(Span::styled(format!(" ({count})"), theme.fg(Role::Muted)));
199 }
200 spans
201 }
202 }
203
204 impl Paint for ChecksMark {
205 fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) {
206 let area = area.intersection(buf.area);
207 if area.is_empty() {
208 return;
209 }
210 let ascii = theme.ascii();
211 let mark = glyphs::pick(self.state.glyph(), ascii);
212 let mark_w = text::width(mark);
213 let max_w = usize::from(area.width);
214 let row_rect = Rect {
215 x: area.x,
216 y: area.y,
217 width: area.width,
218 height: 1,
219 };
220
221 if max_w <= mark_w + 1 {
222 Line::from(Span::styled(mark, theme.fg(self.state.role()))).render(row_rect, buf);
223 return;
224 }
225
226 let word_role = if self.tinted {
227 self.state.role()
228 } else {
229 Role::Foreground
230 };
231 let rest_budget = max_w.saturating_sub(mark_w + 1);
232 let safe_word = text::display_safe(&self.word);
233 let suffix = self.count.map(|n| format!(" ({n})"));
234 let full_rest = match &suffix {
235 Some(s) => format!("{safe_word}{s}"),
236 None => safe_word.into_owned(),
237 };
238
239 let spans = if text::width(&full_rest) <= rest_budget {
240 self.spans(theme)
241 } else {
242 let clipped = text::truncate(&full_rest, rest_budget, ascii);
243 vec![
244 Span::styled(mark, theme.fg(self.state.role())),
245 Span::raw(" "),
246 Span::styled(clipped.into_owned(), theme.fg(word_role)),
247 ]
248 };
249
250 Line::from(spans).render(row_rect, buf);
251 }
252 }
253
253 lines RUST