Skip to content

Commit

Permalink
Merge pull request #8 from asciinema/idle-time-limit
Browse files Browse the repository at this point in the history
Support idle time limit
  • Loading branch information
ku1ik authored Aug 22, 2022
2 parents 592dc80 + 0c60dd0 commit 919f54d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
51 changes: 37 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,43 @@ agg --theme monokai --font-size 20 --speed 2 demo.cast demo.gif
Run `agg -h` to see all available options. Current options are:

```
--font-dir <FONT_DIR> Use additional font directory
--font-family <FONT_FAMILY> Specify font family [default: "JetBrains Mono,Fira Code,SF
Mono,Menlo,Consolas,DejaVu Sans Mono,Liberation Mono"]
--font-size <FONT_SIZE> Specify font size (in pixels) [default: 14]
--fps-cap <FPS_CAP> Set FPS cap [default: 30]
-h, --help Print help information
--line-height <LINE_HEIGHT> Specify line height [default: 1.4]
--renderer <RENDERER> Select frame rendering backend [default: fontdue] [possible
values: fontdue, resvg]
--speed <SPEED> Adjust playback speed [default: 1]
--theme <THEME> Select color theme [possible values: asciinema, monokai,
solarized-dark, solarized-light, custom]
-v, --verbose Enable verbose logging
-V, --version Print version information
--font-dir <FONT_DIR>
Use additional font directory
--font-family <FONT_FAMILY>
Specify font family [default: "JetBrains Mono,Fira Code,SF Mono,Menlo,Consolas,DejaVu
Sans Mono,Liberation Mono"]
--font-size <FONT_SIZE>
Specify font size (in pixels) [default: 14]
--fps-cap <FPS_CAP>
Set FPS cap [default: 30]
-h, --help
Print help information
--idle-time-limit <IDLE_TIME_LIMIT>
Limit idle time to max number of seconds [default: 5]
--line-height <LINE_HEIGHT>
Specify line height [default: 1.4]
--renderer <RENDERER>
Select frame rendering backend [default: fontdue] [possible values: fontdue, resvg]
--speed <SPEED>
Adjust playback speed [default: 1]
--theme <THEME>
Select color theme [possible values: asciinema, dracula, monokai, solarized-dark,
solarized-light, custom]
-v, --verbose
Enable verbose logging
-V, --version
Print version information
```

### Color themes
Expand Down
3 changes: 3 additions & 0 deletions src/asciicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ pub struct V2Theme {
pub struct V2Header {
pub width: usize,
pub height: usize,
pub idle_time_limit: Option<f64>,
pub theme: Option<V2Theme>,
}

pub struct Header {
pub terminal_size: (usize, usize),
pub idle_time_limit: Option<f64>,
pub theme: Option<Theme>,
}

Expand Down Expand Up @@ -78,6 +80,7 @@ impl TryInto<Header> for V2Header {

Ok(Header {
terminal_size: (self.width, self.height),
idle_time_limit: self.idle_time_limit,
theme,
})
}
Expand Down
40 changes: 40 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ pub fn accelerate(events: impl Iterator<Item = Event>, speed: f64) -> impl Itera
events.map(move |(time, data)| (time / speed, data))
}

pub fn limit_idle_time(
events: impl Iterator<Item = Event>,
limit: f64,
) -> impl Iterator<Item = Event> {
let mut prev_time = 0.0;
let mut offset = 0.0;

events.map(move |(time, data)| {
let delay = time - prev_time;
let excess = delay - limit;

if excess > 0.0 {
offset += excess;
}

prev_time = time;

(time - offset, data)
})
}

#[cfg(test)]
mod tests {
#[test]
Expand Down Expand Up @@ -105,4 +126,23 @@ mod tests {
assert_eq!(&stdout[1], &(0.066, "baz".to_owned()));
assert_eq!(&stdout[2], &(1.0, "qux".to_owned()));
}

#[test]
fn limit_idle_time() {
let stdout = [
(0.0, "foo".to_owned()),
(1.0, "bar".to_owned()),
(3.5, "baz".to_owned()),
(4.0, "qux".to_owned()),
(7.5, "quux".to_owned()),
];

let stdout = super::limit_idle_time(stdout.into_iter(), 2.0).collect::<Vec<_>>();

assert_eq!(&stdout[0], &(0.0, "foo".to_owned()));
assert_eq!(&stdout[1], &(1.0, "bar".to_owned()));
assert_eq!(&stdout[2], &(3.0, "baz".to_owned()));
assert_eq!(&stdout[3], &(3.5, "qux".to_owned()));
assert_eq!(&stdout[4], &(5.5, "quux".to_owned()));
}
}
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ struct Cli {
#[clap(long, default_value_t = 1.0)]
speed: f64,

/// Limit idle time to max number of seconds [default: 5]
#[clap(long)]
idle_time_limit: Option<f64>,

/// Set FPS cap
#[clap(long, default_value_t = 30)]
fps_cap: u8,
Expand All @@ -178,6 +182,13 @@ fn main() -> Result<()> {

let (header, events) = asciicast::open(&cli.input_filename)?;
let stdout = asciicast::stdout(events);

let itl = cli
.idle_time_limit
.or(header.idle_time_limit)
.unwrap_or(5.0);

let stdout = events::limit_idle_time(stdout, itl);
let stdout = events::accelerate(stdout, cli.speed);
let stdout = events::batch(stdout, cli.fps_cap);
let stdout = stdout.collect::<Vec<_>>();
Expand Down

0 comments on commit 919f54d

Please sign in to comment.