mod bars;
mod compose;
use std::collections::VecDeque;
use std::sync::mpsc::{Receiver, RecvTimeoutError};
use std::time::{Duration, Instant};
use crate::frame::TermSize;
use crate::progress::events::{GroupMap, ProgressEvent, ProgressSpec, SummaryView};
use crate::progress::model::Model;
use crate::theme::Theme;
use crate::tty::{self, Input, KeyEvent, Tty};
use compose::{compose, View};
const TICK_MS: u64 = 120;
const LOG_CAP: usize = 64;
const JOURNAL_CAP: usize = 12;
const AUTO_DISMISS: Duration = Duration::from_secs(120);
struct State {
model: Model,
active: Option<usize>,
logs: VecDeque<String>,
journal: VecDeque<String>,
started: Option<Instant>,
summary: Option<Box<dyn SummaryView>>,
finished: bool,
}
impl State {
fn new(groups: GroupMap) -> Self {
State {
model: Model::new(groups),
active: None,
logs: VecDeque::with_capacity(LOG_CAP),
journal: VecDeque::with_capacity(JOURNAL_CAP),
started: None,
summary: None,
finished: false,
}
}
fn focus(&mut self, group: Option<usize>) {
if group.is_some() && group != self.active {
self.active = group;
self.logs.clear();
}
}
fn apply(&mut self, ev: ProgressEvent) {
match ev {
ProgressEvent::StepStarted { id } => {
if self.started.is_none() {
self.started = Some(Instant::now());
}
self.focus(self.model.group_index(&id));
self.model.start(&id);
}
ProgressEvent::StepResult { id, outcome } => {
self.model.record(&id, &outcome);
}
ProgressEvent::SubProgress { id, done, total } => {
self.model.set_sub(&id, done, total);
}
ProgressEvent::Activity { id, message } => {
self.model.set_activity(&id, message);
}
ProgressEvent::SubLog { id, line } => {
self.focus(self.model.group_index(&id));
push(&mut self.logs, line, LOG_CAP);
}
ProgressEvent::ByteProgress {
id,
done,
total,
label,
} => {
self.model.set_bytes(&id, done, total, label);
}
ProgressEvent::Log { line, .. } => {
push(&mut self.journal, line, JOURNAL_CAP);
}
ProgressEvent::Summary(s) => self.summary = Some(s),
ProgressEvent::Finished => self.finished = true,
}
}
fn view<'a>(&'a self, title: &'a str, spec: &'a ProgressSpec, spinner: usize) -> View<'a> {
View {
title,
footer: spec.footer,
skipped: spec.skipped,
model: &self.model,
active: self.active,
logs: &self.logs,
spinner,
elapsed: self.started.map(|s| s.elapsed()),
}
}
}
pub fn run(
mut tty: Tty,
rx: Receiver<ProgressEvent>,
theme: Theme,
header: String,
spec: ProgressSpec,
interactive: bool,
) {
let title = title_from_header(&header);
let mut st = State::new(spec.groups);
let mut spinner = 0usize;
let mut interrupted = false;
tty.force_clear_next();
tty.enable_signals();
paint(&mut tty, &theme, &st.view(&title, &spec, spinner));
while !st.finished {
match rx.recv_timeout(Duration::from_millis(TICK_MS)) {
Ok(ev) => {
st.apply(ev);
while let Ok(ev) = rx.try_recv() {
st.apply(ev);
}
}
Err(RecvTimeoutError::Timeout) => spinner = spinner.wrapping_add(1),
Err(RecvTimeoutError::Disconnected) => break,
}
tty.poll_interrupt();
if tty::take_interrupt() {
interrupted = true;
break;
}
paint(&mut tty, &theme, &st.view(&title, &spec, spinner));
}
if interactive && !interrupted {
if let Some(s) = st.summary.as_deref() {
if !s.failed() {
present_completion(&mut tty, &theme, s, &st.journal);
}
}
}
drop(tty);
if interrupted {
eprintln!("\n{}", spec.interrupted);
std::process::exit(130);
}
if let Some(s) = st.summary {
for line in s.rich_lines(&theme, &st.journal) {
eprintln!("{line}");
}
}
}
pub fn sample_grid(
theme: &Theme,
size: TermSize,
spec: &ProgressSpec,
title: &str,
events: Vec<ProgressEvent>,
spinner: usize,
elapsed_secs: u64,
) -> Vec<String> {
let mut st = State::new(spec.groups);
for ev in events {
st.apply(ev);
}
let mut view = st.view(title, spec, spinner);
view.elapsed = Some(Duration::from_secs(elapsed_secs));
compose(theme, &view, size)
}
fn paint_completion(tty: &mut Tty, theme: &Theme, s: &dyn SummaryView, journal: &VecDeque<String>) {
let size = tty.size();
let grid = s.fullscreen_grid(theme, journal, size.cols, size.rows);
tty.present(&grid, theme);
}
fn present_completion(
tty: &mut Tty,
theme: &Theme,
s: &dyn SummaryView,
journal: &VecDeque<String>,
) {
let deadline = Instant::now() + AUTO_DISMISS;
tty.flush_input();
paint_completion(tty, theme, s, journal);
loop {
match tty.read_input(200) {
Input::Key(KeyEvent::Resize) => paint_completion(tty, theme, s, journal),
Input::Key(_) => break,
Input::Tick => {
if tty::take_interrupt() || Instant::now() >= deadline {
break;
}
}
}
}
}
fn title_from_header(header: &str) -> String {
header.trim_end_matches(['.', '…', ' ']).to_string()
}
fn push(ring: &mut VecDeque<String>, line: String, cap: usize) {
if ring.len() == cap {
ring.pop_front();
}
ring.push_back(line);
}
fn paint(tty: &mut Tty, theme: &Theme, view: &View) {
let grid = compose(theme, view, tty.size());
tty.present(&grid, theme);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::outcome::StepOutcome;
use crate::progress::TEST_SPEC;
use crate::theme::ColorTier;
#[test]
fn sample_grid_replays_events_into_a_full_frame() {
let theme = Theme {
ascii: false,
tier: ColorTier::None,
};
let events = vec![
ProgressEvent::StepStarted { id: "deps".into() },
ProgressEvent::StepResult {
id: "deps".into(),
outcome: StepOutcome::Ok,
},
ProgressEvent::StepStarted { id: "units".into() },
ProgressEvent::Activity {
id: "units".into(),
message: "writing unit files".into(),
},
];
let size = TermSize {
cols: 110,
rows: 28,
};
let grid = sample_grid(&theme, size, &TEST_SPEC, "Installing", events, 0, 252);
assert_eq!(grid.len(), 28);
let joined = grid.join("\n");
assert!(joined.contains("Registering services"), "checklist label");
assert!(joined.contains("writing unit files"), "activity headline");
assert!(joined.contains("4:12"), "fixed elapsed clock");
assert!(joined.contains(TEST_SPEC.footer), "footer");
}
#[test]
fn title_drops_trailing_ellipsis() {
assert_eq!(
title_from_header("Installing the agent…"),
"Installing the agent"
);
assert_eq!(title_from_header("Deploying..."), "Deploying");
}
}