Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clipboard): implement clipboard integration #91

146 changes: 143 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions wayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ image = { version = "0.24", default-features = false, features = [
dialoguer = { version = "0.11.0", features = ["fuzzy-select"] }
eyre = "0.6.8"

wl-clipboard-rs = "0.8.0"

[[bin]]
name = "wayshot"
path = "src/wayshot.rs"
6 changes: 5 additions & 1 deletion wayshot/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ use clap::builder::TypedValueParser;
#[derive(Parser)]
#[command(version, about)]
pub struct Cli {
/// Where to save the screenshot, "-" for stdout. Defaults to "$UNIX_TIMESTAMP-wayshot.$EXTENSION".
/// Where to save the screenshot, "-" for stdout. Defaults to "$UNIX_TIMESTAMP-wayshot.$EXTENSION" unless --clipboard is present.
#[arg(value_name = "OUTPUT")]
pub file: Option<PathBuf>,

/// Copy image to clipboard along with [OUTPUT] or stdout
#[arg(long)]
pub clipboard: bool,

/// Log level to be used for printing to stderr
#[arg(long, default_value = "info", value_parser = clap::builder::PossibleValuesParser::new(["trace", "debug", "info", "warn", "error"]).map(|s| -> tracing::Level{ s.parse().wrap_err_with(|| format!("Failed to parse log level: {}", s)).unwrap()}))]
pub log_level: tracing::Level,
Expand Down
48 changes: 33 additions & 15 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mod utils;
use dialoguer::{theme::ColorfulTheme, FuzzySelect};
use utils::EncodingFormat;

use wl_clipboard_rs::copy::{MimeType, Options, Source};

fn select_ouput<T>(ouputs: &[T]) -> Option<usize>
where
T: ToString,
Expand Down Expand Up @@ -52,17 +54,6 @@ fn main() -> Result<()> {
}
}

let file = match cli.file {
Some(pathbuf) => {
if pathbuf.to_string_lossy() == "-" {
None
} else {
Some(pathbuf)
}
}
None => Some(utils::get_default_file_name(requested_encoding)),
};

let wayshot_conn = WayshotConnection::new()?;

if cli.list_outputs {
Expand Down Expand Up @@ -111,16 +102,43 @@ fn main() -> Result<()> {
wayshot_conn.screenshot_all(cli.cursor)?
};

let mut stdout_print = false;
let file = match cli.file {
Some(pathbuf) => {
if pathbuf.to_string_lossy() == "-" {
stdout_print = true;
None
} else {
Some(pathbuf)
}
}
None => {
if cli.clipboard {
None
} else {
Some(utils::get_default_file_name(requested_encoding))
}
}
};

if let Some(file) = file {
image_buffer.save(file)?;
} else {
let stdout = stdout();
let mut buffer = Cursor::new(Vec::new());

let mut writer = BufWriter::new(stdout.lock());
image_buffer.write_to(&mut buffer, requested_encoding)?;

writer.write_all(buffer.get_ref())?;
if stdout_print {
let stdout = stdout();
let mut writer = BufWriter::new(stdout.lock());
writer.write_all(buffer.get_ref())?;
}
if cli.clipboard {
let opts = Options::new();
opts.copy(
Source::Bytes(buffer.into_inner().into()),
MimeType::Autodetect,
)?;
}
}

Ok(())
Expand Down
Loading