-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmod.rs
More file actions
338 lines (300 loc) · 10.8 KB
/
mod.rs
File metadata and controls
338 lines (300 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use std::collections::VecDeque;
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
use super::{
CODSPEED_U8_COLOR_CODE, IS_TTY, SPINNER, SPINNER_TICKS, TICK_INTERVAL_MS, format_checkmark,
icons::Icon,
};
use console::{Term, style};
use std::sync::LazyLock;
const INDENT: &str = " ";
/// Global shared rolling buffer, set by `activate_rolling_buffer` and
/// consumed by `log_tee` in `run_command_with_log_pipe`.
pub(crate) static ROLLING_BUFFER: LazyLock<Mutex<Option<RollingBuffer>>> =
LazyLock::new(|| Mutex::new(None));
/// Stop signal for the tick thread.
///
/// The rolling buffer manages its own background tick thread rather than using
/// `ProgressBar` because it renders a multi-line frame (title + bordered log box)
/// via direct terminal cursor manipulation. `ProgressBar` only manages a single
/// line and would conflict with the rolling buffer's cursor movements.
static TICK_STOP: AtomicBool = AtomicBool::new(false);
pub struct RollingBuffer {
lines: VecDeque<String>,
max_lines: usize,
total_lines: usize,
/// Number of lines currently drawn on screen
/// (title + top_delim + content lines + bottom_delim)
rendered_count: usize,
term: Term,
term_width: usize,
active: bool,
title: String,
start: Instant,
finished: bool,
}
impl RollingBuffer {
fn new(title: &str) -> Self {
let term = Term::stderr();
let (rows, cols) = term.size();
let rows = rows as usize;
let cols = cols as usize;
let active = *IS_TTY && rows >= 5;
// Reserve space for title + delimiters
let max_lines = if active {
20.min(rows.saturating_sub(6))
} else {
0
};
Self {
lines: VecDeque::with_capacity(max_lines),
max_lines,
total_lines: 0,
rendered_count: 0,
term,
term_width: cols,
active,
title: title.to_string(),
start: Instant::now(),
finished: false,
}
}
pub fn is_active(&self) -> bool {
self.active
}
/// Ingest text into the rolling buffer, splitting on newlines and
/// maintaining the max_lines window.
fn ingest(&mut self, text: &str) {
for line in text.split('\n') {
if line.is_empty() {
continue;
}
let line = line.trim_end_matches('\r');
self.total_lines += 1;
self.lines.push_back(line.to_string());
while self.lines.len() > self.max_lines {
self.lines.pop_front();
}
}
}
pub fn push_lines(&mut self, text: &str) {
if !self.active {
return;
}
self.ingest(text);
self.redraw();
}
fn truncated_count(&self) -> usize {
self.total_lines.saturating_sub(self.lines.len())
}
fn spinner_tick(&self) -> &'static str {
let elapsed_ms = self.start.elapsed().as_millis();
let idx = (elapsed_ms / TICK_INTERVAL_MS as u128) as usize % SPINNER_TICKS.len();
SPINNER_TICKS[idx]
}
fn render_title_line(&self) -> String {
let tick = self.spinner_tick();
let tick_styled = style(tick).color256(CODSPEED_U8_COLOR_CODE).dim();
let title_styled = style(&self.title).color256(CODSPEED_U8_COLOR_CODE);
let line = format!(" {tick_styled} {title_styled}");
console::truncate_str(&line, self.term_width, &Icon::Ellipsis.to_string()).into_owned()
}
fn render_top_delimiter(&self) -> String {
let truncated = self.truncated_count();
let label = if truncated > 0 {
format!(
" {} lines above ",
style(truncated).color256(CODSPEED_U8_COLOR_CODE).dim()
)
} else {
String::new()
};
let prefix = format!("{INDENT}{}{}", Icon::BoxTopLeft, Icon::BoxHorizontal);
let suffix = Icon::BoxTopRight.to_string();
let label_visible_len = if truncated > 0 {
format!(" {truncated} lines above ").len()
} else {
0
};
let used = console::measure_text_width(&prefix)
+ label_visible_len
+ console::measure_text_width(&suffix);
let remaining = self.term_width.saturating_sub(used);
let bar = Icon::BoxHorizontal.to_string().repeat(remaining);
format!(
"{}{}{}",
style(prefix.to_string()).dim(),
label,
style(format!("{bar}{suffix}")).dim()
)
}
fn render_bottom_delimiter(&self) -> String {
let prefix = format!("{INDENT}{}", Icon::BoxBottomLeft);
let suffix = Icon::BoxBottomRight.to_string();
let used = console::measure_text_width(&prefix) + console::measure_text_width(&suffix);
let remaining = self.term_width.saturating_sub(used);
let bar = Icon::BoxHorizontal.to_string().repeat(remaining);
format!("{}", style(format!("{prefix}{bar}{suffix}")).dim())
}
fn render_content_line(&self, line: &str) -> String {
let inner_indent = format!("{INDENT}{} ", Icon::BoxVertical);
let right_border = Icon::BoxVertical.to_string();
let chrome_width =
console::measure_text_width(&inner_indent) + console::measure_text_width(&right_border);
let max_content_width = self.term_width.saturating_sub(chrome_width);
let truncated = if max_content_width > 0 {
console::truncate_str(line, max_content_width, &Icon::Ellipsis.to_string())
} else {
std::borrow::Cow::Borrowed("")
};
let content_visible_len = console::measure_text_width(&truncated);
let padding = max_content_width.saturating_sub(content_visible_len);
format!(
"{}{}{}{}",
style(&inner_indent).dim(),
style(&*truncated).dim(),
" ".repeat(padding),
style(&right_border).dim()
)
}
/// Return the full rendered frame as a vector of strings.
fn render_frame(&self) -> Vec<String> {
let mut frame = Vec::new();
frame.push(self.render_title_line());
frame.push(self.render_top_delimiter());
for line in &self.lines {
frame.push(self.render_content_line(line));
}
frame.push(self.render_bottom_delimiter());
frame
}
/// Render the finished frame (checkmark title instead of spinner).
fn render_finished_frame(&self) -> Vec<String> {
let mut frame = Vec::new();
frame.push(format_checkmark(&self.title, false));
frame.push(self.render_top_delimiter());
for line in &self.lines {
frame.push(self.render_content_line(line));
}
frame.push(self.render_bottom_delimiter());
frame
}
/// Write a frame to the terminal, clearing and replacing any previously rendered lines.
fn draw_frame(&mut self, frame: &[String]) {
// Move cursor up to erase all previously rendered lines
if self.rendered_count > 0 {
self.term.move_cursor_up(self.rendered_count).ok();
}
// Flush deferred logs above the frame so they become permanent output
// and the rolling buffer shifts down naturally.
super::flush_deferred_logs(&self.term);
for line in frame {
self.term.clear_line().ok();
self.term.write_line(line).ok();
}
let new_count = frame.len();
// Clear any extra lines from previous render
for _ in new_count..self.rendered_count {
self.term.clear_line().ok();
self.term.write_line("").ok();
}
// Move cursor back if we rendered fewer lines than before
if new_count < self.rendered_count {
self.term
.move_cursor_up(self.rendered_count - new_count)
.ok();
}
self.rendered_count = new_count;
}
/// Redraw only the title line (for spinner animation ticks).
fn redraw_title(&mut self) {
if self.rendered_count == 0 || self.finished {
return;
}
// Move up to the title line, rewrite it, then move back down
self.term.move_cursor_up(self.rendered_count).ok();
self.term.clear_line().ok();
self.term.write_line(&self.render_title_line()).ok();
let rest = self.rendered_count - 1;
if rest > 0 {
self.term.move_cursor_down(rest).ok();
}
}
fn redraw(&mut self) {
let frame = self.render_frame();
self.draw_frame(&frame);
}
/// Finish the rolling display, replacing the spinner title with a checkmark
/// and leaving the last content lines visible on screen.
pub fn finish(&mut self) {
if self.finished || self.rendered_count == 0 {
self.finished = true;
return;
}
self.finished = true;
let frame = self.render_finished_frame();
self.draw_frame(&frame);
self.rendered_count = 0;
}
}
impl Drop for RollingBuffer {
fn drop(&mut self) {
if !self.finished {
self.finish();
}
}
}
/// Activate a rolling buffer for the current executor run.
///
/// Suspends the group spinner and installs a shared rolling buffer that
/// `run_command_with_log_pipe` will automatically pick up. Starts a background
/// tick thread to keep the spinner animating.
pub fn activate_rolling_buffer(title: &str) {
if !*IS_TTY {
return;
}
let rb = RollingBuffer::new(title);
if !rb.is_active() {
return;
}
// Suspend the group spinner so it doesn't interfere with rolling output
if let Ok(mut spinner) = SPINNER.lock() {
if let Some(pb) = spinner.take() {
pb.suspend(|| eprintln!());
pb.finish_and_clear();
}
}
*ROLLING_BUFFER.lock().unwrap() = Some(rb);
// Start a background thread that redraws periodically to animate the spinner
TICK_STOP.store(false, Ordering::Relaxed);
std::thread::spawn(|| {
while !TICK_STOP.load(Ordering::Relaxed) {
std::thread::sleep(Duration::from_millis(TICK_INTERVAL_MS));
if TICK_STOP.load(Ordering::Relaxed) {
break;
}
if let Ok(mut guard) = ROLLING_BUFFER.try_lock() {
if let Some(rb) = guard.as_mut() {
if rb.finished {
break;
}
rb.redraw_title();
}
}
}
});
}
/// Finish and deactivate the current rolling buffer.
pub fn deactivate_rolling_buffer() {
// Stop the tick thread first
TICK_STOP.store(true, Ordering::Relaxed);
if let Ok(mut guard) = ROLLING_BUFFER.lock() {
if let Some(rb) = guard.as_mut() {
rb.finish();
}
*guard = None;
}
}
#[cfg(test)]
mod tests;