Skip to content

Commit

Permalink
bin/ogle: migrate to output_sequence as it's equivalent now
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Jan 29, 2025
1 parent 416454f commit 18af90e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 347 deletions.
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ mod output_simple;
mod cli;
mod orchestrator;
mod progbar;
mod runner;
mod stream;
mod timewrap;

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
color_eyre::install()?;
let args = cli::Cli::parse();
runner::run_loop(&args).await?;
let output = output_sequence::OutputSequence::new(&args);
orchestrator::run(&args, output).await?;
Ok(())
}
201 changes: 0 additions & 201 deletions src/progbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

use color_eyre::Report;
use color_eyre::Result;
use console::Term;

use crate::misc::term_clear_line;
use crate::misc::term_width;
use crate::timewrap::Duration;
use crate::timewrap::Instant;

const SPINNERS: [char; 4] = ['/', '-', '\\', '|'];

// Basic functions:

pub fn progbar_sleeping(
Expand Down Expand Up @@ -86,198 +80,3 @@ pub fn progbar_running(
};
Ok(msg)
}

// Progbar object:

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Mode {
None,
Running,
Sleeping,
}

#[derive(Debug)]
pub struct Progbar {
mode: Mode,
mode_wanted: Mode,
shown: bool,
start: Instant,
duration: Duration,
refresh_delay: Duration,
ispinner: usize,
lastrun: Instant,
term: Term,
}

impl Default for Progbar {
fn default() -> Progbar {
Progbar {
mode: Mode::None,
mode_wanted: Mode::None,
shown: false,
start: Instant::now(),
duration: Duration::default(),
refresh_delay: Duration::milliseconds(250),
ispinner: 0,
lastrun: Instant::now(),
term: Term::stdout(),
}
}
}

impl Progbar {
pub fn set_running(&mut self, duration: Duration) {
self.mode_wanted = Mode::Running;
self.duration = duration;
self.start = Instant::now();
}

pub fn set_sleep(&mut self, duration: Duration) {
self.mode_wanted = Mode::Sleeping;
self.duration = duration;
self.start = Instant::now();
}

fn spinner(&mut self) -> char {
self.ispinner = (self.ispinner + 1) % 4;
SPINNERS[self.ispinner]
}

pub fn hide(&mut self) -> Result<()> {
if self.shown {
term_clear_line(&self.term)?;
self.shown = false;
}
Ok(())
}

pub fn show(&mut self) -> Result<()> {
self.refresh()
}

pub fn refresh(&mut self) -> Result<()> {
if self.mode != self.mode_wanted {
self.mode = self.mode_wanted;
}
self.hide()?;
match self.mode {
Mode::None => {
return Ok(());
}
Mode::Sleeping => {
let msg =
progbar_sleeping(&self.lastrun, &Instant::now(), &self.start, &self.duration)?;
self.term.write_line(&msg).map_err(Report::new)?;
}
Mode::Running => {
let spinner = self.spinner();
let msg = progbar_running(
term_width(&self.term),
&self.lastrun,
&Instant::now(),
&self.start,
&Some(self.duration),
&self.refresh_delay,
spinner,
)?;
self.term.write_line(&msg).map_err(Report::new)?;
}
}
self.term.flush()?;
self.shown = true;
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn sleeping() {
let start = Instant::epoch();
let now = &start + &Duration::milliseconds(1);
let string = progbar_sleeping(&now, &now, &start, &Duration::seconds(1)).unwrap();
assert_eq!(string, "=> 1970-01-01 00:00:00 sleeping");
let string = progbar_sleeping(&now, &now, &start, &Duration::seconds(10)).unwrap();
assert_eq!(string, "=> 1970-01-01 00:00:00 sleeping for 10s");
let now = &start + &Duration::seconds(10);
let string = progbar_sleeping(&now, &now, &start, &Duration::seconds(10)).unwrap();
assert_eq!(string, "=> 1970-01-01 00:00:10 sleeping for 1s");
}

#[test]
fn running_fast() {
let start = Instant::epoch();
let now = &start + &Duration::milliseconds(1);
let string = progbar_running(
80,
&now,
&now,
&start,
&Some(Duration::seconds(3)),
&Duration::default(),
'X',
)
.unwrap();
assert_eq!(string, "=> 1970-01-01 00:00:00 running [X]");
}

#[test]
fn running_shortbar() {
let start = Instant::epoch();
let f = |n| {
let now = &start + &Duration::milliseconds(1);
let now = &now + &Duration::seconds(n);
progbar_running(
80,
&start,
&now,
&start,
&Some(Duration::seconds(4)),
&Duration::seconds(1),
'X',
)
.unwrap()
};
let string = f(0);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [> ] [X]");
let string = f(1);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [=> ] [X]");
let string = f(2);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [==> ] [X]");
let string = f(3);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [===>] [X]");
let string = f(4);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [====] [X]");
}

#[test]
fn running_longbar() {
let start = Instant::epoch();
let f = |n| {
let now = &start + &Duration::milliseconds(1);
let now = &now + &Duration::seconds(n);
progbar_running(
40,
&start,
&now,
&start,
&Some(Duration::seconds(40)),
&Duration::seconds(1),
'X',
)
.unwrap()
};
let string = f(0);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [> ] [X]");
let string = f(10);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [=> ] [X]");
let string = f(20);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [==> ] [X]");
let string = f(30);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [===>] [X]");
let string = f(40);
assert_eq!(string, "=> 1970-01-01 00:00:00 running [====] [X]");
}
}
144 changes: 0 additions & 144 deletions src/runner.rs

This file was deleted.

0 comments on commit 18af90e

Please sign in to comment.