返回 CodeWhale
verification.rs
根目录 / vendor / codewhale-ratatui / src / components / verification.rs
1 //! The Engine's verification tick, distinct from its working swell.
2 //!
3 //! The frame table and cadence come from `Hmbown/CodeWhale`
4 //! `a79ce5c4d5ed1a5f7032185710c27343a900351c`,
5 //! `crates/tui/src/tui/spinner.rs::verification_tick_frame`. Earned delay,
6 //! ASCII rotation, elapsed words and quiet-mode marks share this kit's
7 //! [`Spinner`]. The caller supplies elapsed time and owns all scheduling.
8
9 use std::time::Duration;
10
11 use crate::{MotionMode, Paint, Role, Spinner, Theme, spin, text};
12 use ratatui::{
13 buffer::Buffer,
14 layout::Rect,
15 text::{Line, Span},
16 };
17
18 /// A checking phase uses a round tick while working uses a swell.
19 ///
20 /// Construct it only for verification the host has actually observed. The
21 /// verb is caller-owned, and the elapsed time is measured rather than a
22 /// progress estimate. Reduced and still motion show a static mark and word.
23 #[derive(Clone, Debug, PartialEq, Eq)]
24 pub struct VerificationSpinner {
25 pub verb: String,
26 pub elapsed: Duration,
27 pub motion: MotionMode,
28 /// Optional detail label shown after the verb (`Checking · cargo test`).
29 pub detail: Option<String>,
30 }
31
32 impl VerificationSpinner {
33 /// The Engine's eight adjacent verification frames.
34 pub const FRAMES: [&'static str; 8] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧"];
35
36 #[must_use]
37 pub fn new(verb: impl Into<String>, elapsed: Duration, motion: MotionMode) -> Self {
38 Self {
39 verb: verb.into(),
40 elapsed,
41 motion,
42 detail: None,
43 }
44 }
45
46 /// Attach an optional detail label shown after the verb (`Checking · cargo test`).
47 #[must_use]
48 pub fn detail(mut self, detail: impl Into<String>) -> Self {
49 let value = detail.into();
50 self.detail = (!value.is_empty()).then_some(value);
51 self
52 }
53
54 /// Sample a verification frame without reading a clock.
55 #[must_use]
56 pub fn frame(elapsed: Duration, motion: MotionMode, ascii: bool) -> &'static str {
57 if !motion.animates() || ascii || elapsed < spin::EARN_DELAY {
58 return spin::frame(elapsed, motion, ascii);
59 }
60 let step = (elapsed - spin::EARN_DELAY).as_millis() / spin::FRAME_INTERVAL.as_millis();
61 Self::FRAMES[(step % Self::FRAMES.len() as u128) as usize]
62 }
63
64 /// The shared five-step cadence; quiet motion asks for no redraws.
65 #[must_use]
66 pub fn next_frame_in(elapsed: Duration, motion: MotionMode) -> Option<Duration> {
67 spin::next_frame_in(elapsed, motion)
68 }
69
70 /// A tick, the caller's sanitized verb, optional detail and measured-time words.
71 #[must_use]
72 pub fn spans(&self, theme: &Theme) -> Vec<Span<'static>> {
73 self.spans_for_width(None, theme)
74 }
75
76 fn spans_for_width(&self, max_width: Option<usize>, theme: &Theme) -> Vec<Span<'static>> {
77 let mut spans = Spinner::new(&self.verb, self.elapsed, self.motion).spans(theme);
78 spans[0].content = Self::frame(self.elapsed, self.motion, theme.ascii()).into();
79 let Some(raw_detail) = self.detail.as_deref() else {
80 return spans;
81 };
82 let detail = text::display_safe(raw_detail);
83 if detail.is_empty() {
84 return spans;
85 }
86 let sep = if theme.ascii() { " - " } else { " · " };
87 let sep_w = text::width(sep);
88 let fitted = match max_width {
89 Some(limit) => {
90 let base_w: usize = spans.iter().map(Span::width).sum();
91 let room = limit.saturating_sub(base_w.saturating_add(sep_w));
92 if room == 0 {
93 return spans;
94 }
95 text::truncate(&detail, room, theme.ascii()).into_owned()
96 }
97 None => detail.into_owned(),
98 };
99 if fitted.is_empty() {
100 return spans;
101 }
102 let insert_at = 3.min(spans.len());
103 let muted = theme.fg(Role::Muted);
104 spans.insert(insert_at, Span::styled(fitted, muted));
105 spans.insert(insert_at, Span::styled(sep, muted));
106 spans
107 }
108 }
109
110 impl Paint for VerificationSpinner {
111 fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) {
112 let area = area.intersection(buf.area);
113 if !area.is_empty() {
114 // Buffer::set_line also handles a right edge of u16::MAX, where
115 // the widget Line renderer can overflow while clipping a span.
116 let spans = self.spans_for_width(Some(usize::from(area.width)), theme);
117 super::workbench::row(area, buf, &Line::from(spans));
118 }
119 }
120 }
121
121 lines RUST