Skip to content

Commit 5f59656

Browse files
committed
Automatically export frames based on path
1 parent d55294b commit 5f59656

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod vt;
77

88
use std::fmt::{Debug, Display};
99
use std::fs::File;
10-
use std::io::{BufRead, Write};
10+
use std::io::BufRead;
1111
use std::{iter, thread, time::Instant};
1212

1313
use anyhow::{anyhow, Context, Result};
@@ -199,13 +199,17 @@ pub fn run<I: BufRead>(input: I, config: Config) -> Result<()> {
199199
let start_time = Instant::now();
200200

201201
if config.output_frames {
202-
// check for existance of the output directory to provide a clear error message
203-
if std::fs::exists(&config.output_filename)? {
204-
return Err(anyhow!("Frame output path {:?} already exists", config.output_filename));
202+
let dir_path = std::path::PathBuf::from(&config.output_filename);
203+
204+
// create directory if it does not already exist
205+
if !dir_path.try_exists()? {
206+
std::fs::create_dir(&dir_path)?;
205207
}
206208

207-
// create the directory in advance
208-
std::fs::create_dir(&config.output_filename)?;
209+
// make sure output directory is empty
210+
if !dir_path.read_dir()?.next().is_none() {
211+
return Err(anyhow!("Output directory is not empty"));
212+
}
209213

210214
let mut pr: Box<dyn ProgressReporter> = if config.show_progress_bar {
211215
Box::new(ProgressBar::new(count))
@@ -216,12 +220,8 @@ pub fn run<I: BufRead>(input: I, config: Config) -> Result<()> {
216220
for (i, frame) in frames.enumerate() {
217221
let (_, lines, cursor) = frame?;
218222

219-
let frame_file_path = std::path::PathBuf::new()
220-
.join(&config.output_filename)
221-
.join(format!("{}.png", i));
222-
223223
let svg = renderer.render_pixmap(lines, cursor);
224-
svg.save_png(frame_file_path)
224+
svg.save_png(dir_path.join(format!("{}.png", i)))
225225
.with_context(|| anyhow!("Could not encode frame {i}"))?;
226226

227227
if !pr.increase() {

src/main.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ impl clap::builder::TypedValueParser for ThemeValueParser {
4848
#[clap(author, version, about, long_about = None)]
4949
struct Cli {
5050
/// asciicast path/filename or URL
51-
input_filename_or_url: String,
51+
#[clap(value_name = "INPUT_FILENAME_OR_URL")]
52+
input: String,
5253

53-
/// GIF path/filename (output directory if outputting frames)
54-
output_filename: String,
55-
56-
/// Output each frame as PNG image
57-
#[clap(long)]
58-
output_frames: bool,
54+
/// GIF path/filename
55+
///
56+
/// If path ends with a slash or is a directory frames are saved instead
57+
#[clap(value_name = "OUTPUT_FILENAME_OR_DIR")]
58+
output: String,
5959

6060
/// Select frame rendering backend
6161
#[clap(long, arg_enum, default_value_t = agg::Renderer::default())]
@@ -176,6 +176,10 @@ fn main() -> Result<()> {
176176
.format_timestamp(None)
177177
.init();
178178

179+
// output frames if path ends with a path separator or is a directory
180+
let output_frames = cli.output.ends_with(std::path::MAIN_SEPARATOR_STR)
181+
|| std::fs::metadata(&cli.output).is_ok_and(|x| x.is_dir());
182+
179183
let config = agg::Config {
180184
cols: cli.cols,
181185
font_dirs: cli.font_dir,
@@ -191,17 +195,17 @@ fn main() -> Result<()> {
191195
speed: cli.speed,
192196
theme: cli.theme.map(|theme| theme.0),
193197
show_progress_bar: !cli.quiet,
194-
output_frames: cli.output_frames,
195-
output_filename: cli.output_filename.clone(),
198+
output_frames: output_frames,
199+
output_filename: cli.output.clone(),
196200
};
197201

198-
let input = BufReader::new(reader(&cli.input_filename_or_url)?);
202+
let input = BufReader::new(reader(&cli.input)?);
199203
match agg::run(input, config) {
200204
Ok(ok) => Ok(ok),
201205
Err(err) => {
202206
// do not try to delete a directory
203-
if !cli.output_frames {
204-
std::fs::remove_file(&cli.output_filename)?;
207+
if !output_frames {
208+
std::fs::remove_file(&cli.output)?;
205209
}
206210

207211
Err(err)

0 commit comments

Comments
 (0)