diff --git a/src/cli.rs b/src/cli.rs index 53af05d..4990470 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -228,14 +228,16 @@ pub struct Cat { #[derive(Debug, Args)] pub struct Convert { + /// File to convert from, in asciicast format (use - for stdin) #[arg(value_name = "INPUT_FILENAME_OR_URL")] pub input_filename: String, + /// File to convert to (use - for stdout) pub output_filename: String, /// Output file format [default: asciicast-v3] - #[arg(short, long, value_enum)] - pub format: Option, + #[arg(short = 'f', long, value_enum, value_name = "FORMAT")] + pub output_format: Option, /// Overwrite target file if it already exists #[arg(long)] diff --git a/src/cmd/convert.rs b/src/cmd/convert.rs index 9d19f70..cc1266f 100644 --- a/src/cmd/convert.rs +++ b/src/cmd/convert.rs @@ -13,16 +13,17 @@ use crate::util; impl cli::Convert { pub fn run(self, _config: &Config) -> Result<()> { - let path = util::get_local_path(&self.input_filename)?; - let cast = asciicast::open_from_path(&*path)?; + let input_path = self.get_input_path()?; + let output_path = self.get_output_path(); + let cast = asciicast::open_from_path(&*input_path)?; let mut encoder = self.get_encoder(); - let mut file = self.open_file()?; + let mut output_file = self.open_output_file(output_path)?; - encoder.encode_to_file(cast, &mut file) + encoder.encode_to_file(cast, &mut output_file) } fn get_encoder(&self) -> Box { - let format = self.format.unwrap_or_else(|| { + let format = self.output_format.unwrap_or_else(|| { if self.output_filename.to_lowercase().ends_with(".txt") { Format::Txt } else { @@ -38,22 +39,38 @@ impl cli::Convert { } } - fn open_file(&self) -> Result { - let overwrite = self.get_mode()?; + fn get_input_path(&self) -> Result>> { + if self.input_filename == "-" { + Ok(Box::new(Path::new("/dev/stdin"))) + } else { + util::get_local_path(&self.input_filename) + } + } + + fn get_output_path(&self) -> String { + if self.output_filename == "-" { + "/dev/stdout".to_owned() + } else { + self.output_filename.clone() + } + } + + fn open_output_file(&self, path: String) -> Result { + let overwrite = self.get_mode(&path)?; let file = fs::OpenOptions::new() .write(true) .create(overwrite) .create_new(!overwrite) .truncate(overwrite) - .open(&self.output_filename)?; + .open(&path)?; Ok(file) } - fn get_mode(&self) -> Result { + fn get_mode(&self, path: &str) -> Result { let mut overwrite = self.overwrite; - let path = Path::new(&self.output_filename); + let path = Path::new(path); if path.exists() { let metadata = fs::metadata(path)?;