From ef9e96ecfe2f4a1026e7546dc3cc49c4443df820 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Thu, 28 Mar 2024 21:07:56 +0900 Subject: [PATCH] Separate stdout and file options --- wayshot/src/cli.rs | 7 +++++- wayshot/src/wayshot.rs | 56 ++++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index af585f4c..2bab1942 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -9,10 +9,15 @@ pub struct Cli { /// Custom screenshot file path can be of the following types: /// 1. Directory (Default naming scheme is used for the image screenshot file). /// 2. Path (Encoding is automatically inferred from the extension). - /// 3. `-` (Indicates writing to terminal [stdout]). + /// 3. None (no screenshot will be saved on filesystem/drive) #[arg(value_name = "FILE", verbatim_doc_comment)] pub file: Option, + /// Write screenshot to terminal/stdout. + /// Defaults to config value (`false`) + #[arg(long, verbatim_doc_comment)] + pub stdout: Option, + /// Copy image to clipboard. Can be used simultaneously with [FILE] or stdout. /// Wayshot persists in the background offering the image till the clipboard is overwritten. /// Defaults to config value (`true`) diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index f74917c9..0510b4aa 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -5,7 +5,8 @@ use eyre::{bail, Result}; use libwayshot::{region::LogicalRegion, WayshotConnection}; use nix::unistd::{fork, ForkResult}; use std::{ - io::{self, stdout, BufWriter, Cursor, Write}, + env, + io::{self, BufWriter, Cursor, Write}, process::Command, }; use wl_clipboard_rs::copy::{MimeType, Options, Source}; @@ -77,6 +78,26 @@ fn main() -> Result<()> { } } + let stdout_print = cli.stdout.unwrap_or(screenshot.stdout.unwrap_or_default()); + let file = match cli.file { + Some(f) => { + if f.is_dir() { + Some(utils::get_full_file_name(&f, &filename_format, encoding)) + } else { + Some(f) + } + } + _ => { + if screenshot.fs.unwrap_or_default() { + // default to current dir + let dir = fs.path.unwrap_or(env::current_dir().unwrap_or_default()); + Some(utils::get_full_file_name(&dir, &filename_format, encoding)) + } else { + None + } + } + }; + let output = cli.output.or(screenshot.output); let wayshot_conn = WayshotConnection::new()?; @@ -89,6 +110,7 @@ fn main() -> Result<()> { return Ok(()); } + // take a screenshot let image_buffer = if let Some(slurp_region) = cli.slurp { let slurp_region = slurp_region.clone(); wayshot_conn.screenshot_freeze( @@ -127,36 +149,16 @@ fn main() -> Result<()> { wayshot_conn.screenshot_all(cursor)? }; - let mut stdout_print = false; - let file = match cli.file { - Some(mut pathbuf) => { - if pathbuf.to_string_lossy() == "-" { - stdout_print = true; - None - } else { - if pathbuf.is_dir() { - pathbuf.push(utils::get_default_file_name(&filename_format, encoding)); - } - Some(pathbuf) - } - } - None => { - if clipboard { - None - } else { - Some(utils::get_default_file_name(&filename_format, encoding)) - } - } - }; - // save the screenshot data let mut image_buf: Option>> = None; - if let Some(file) = file { - image_buffer.save(file)?; - } else if stdout_print { + if let Some(file_path) = file { + image_buffer.save(file_path)?; + } + + if stdout_print { let mut buffer = Cursor::new(Vec::new()); image_buffer.write_to(&mut buffer, encoding)?; - let stdout = stdout(); + let stdout = io::stdout(); let mut writer = BufWriter::new(stdout.lock()); writer.write_all(buffer.get_ref())?; image_buf = Some(buffer);