Skip to main content

otto_cli/output/
history.rs

1use crate::model::{RunSource, RunStatus};
2use crate::output::{accent, failure, format_duration_ms, info, number, success};
3use std::io::Write;
4use time::OffsetDateTime;
5
6#[derive(Debug, Clone)]
7pub struct HistoryRow {
8    pub name: String,
9    pub source: RunSource,
10    pub status: RunStatus,
11    pub exit_code: i32,
12    pub started_at: OffsetDateTime,
13    pub duration_ms: i64,
14}
15
16pub fn print_history(mut w: impl Write, rows: &[HistoryRow]) -> std::io::Result<()> {
17    if rows.is_empty() {
18        writeln!(w, "{} No run history yet.", info("i"))?;
19        return Ok(());
20    }
21
22    for (idx, row) in rows.iter().enumerate() {
23        let source = match row.source {
24            RunSource::Task => "task",
25            RunSource::Inline => "inline",
26        };
27
28        let status = match row.status {
29            RunStatus::Success => success("ok success"),
30            RunStatus::Failed => failure("x failed"),
31        };
32
33        let started = row
34            .started_at
35            .format(&time::macros::format_description!(
36                "[year]-[month]-[day] [hour]:[minute]:[second]"
37            ))
38            .unwrap_or_else(|_| "-".to_string());
39
40        writeln!(w, "{} {}", accent(&row.name), status)?;
41        writeln!(w, "  source: {}", source)?;
42        writeln!(w, "  exit: {}", number(&row.exit_code.to_string()))?;
43        writeln!(w, "  started (UTC): {}", started)?;
44        writeln!(
45            w,
46            "  duration: {}",
47            number(&format_duration_ms(row.duration_ms))
48        )?;
49
50        if idx + 1 < rows.len() {
51            writeln!(w)?;
52        }
53    }
54
55    Ok(())
56}