Skip to content

Commit

Permalink
Separate stdout and file options
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigas002 committed Mar 28, 2024
1 parent 895bcf7 commit ef9e96e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
7 changes: 6 additions & 1 deletion wayshot/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,

/// Write screenshot to terminal/stdout.
/// Defaults to config value (`false`)
#[arg(long, verbatim_doc_comment)]
pub stdout: Option<bool>,

/// 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`)
Expand Down
56 changes: 29 additions & 27 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()?;
Expand All @@ -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(
Expand Down Expand Up @@ -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<Cursor<Vec<u8>>> = 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);
Expand Down

0 comments on commit ef9e96e

Please sign in to comment.