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

Clipboard integration #89

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 142 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions wayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ image = { version = "0.24", default-features = false, features = [

dialoguer = { version = "0.11.0", features = ["fuzzy-select"] }

wl-clipboard-rs = "0.8.0"
[[bin]]
name = "wayshot"
path = "src/wayshot.rs"
10 changes: 10 additions & 0 deletions wayshot/src/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn set_flags() -> Command {
arg!(-f - -file <FILE_PATH>)
.required(false)
.conflicts_with("stdout")
.conflicts_with("clipboard")
.action(ArgAction::Set)
.help("Mention a custom file path"),
)
Expand All @@ -34,9 +35,18 @@ pub fn set_flags() -> Command {
arg!(--stdout)
.required(false)
.conflicts_with("file")
.conflicts_with("clipboard")
.action(ArgAction::SetTrue)
.help("Output the image data to standard out"),
)
.arg(
arg!(-p - -clipboard)
.required(false)
.conflicts_with("file")
.conflicts_with("stdout")
.action(ArgAction::SetTrue)
.help("Output the image data to clipboard"),
)
.arg(
arg!(-e --extension <FILE_EXTENSION>)
.required(false)
Expand Down
7 changes: 7 additions & 0 deletions wayshot/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ pub fn parse_geometry(g: &str) -> Option<CaptureRegion> {
})
}

/// Supported locations to save the screenshot to.
pub enum SaveLocation {
File(String),
StdOut,
Clipboard,
}

/// Supported image encoding formats.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum EncodingFormat {
Expand Down
49 changes: 32 additions & 17 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ mod utils;
use dialoguer::{theme::ColorfulTheme, FuzzySelect};
use tracing::Level;

use crate::utils::EncodingFormat;
use wl_clipboard_rs::copy::{MimeType, Options, Source};

use crate::utils::{EncodingFormat, SaveLocation};

fn select_ouput<T>(ouputs: &[T]) -> Option<usize>
where
Expand Down Expand Up @@ -59,16 +61,15 @@ fn main() -> Result<(), Box<dyn Error>> {
EncodingFormat::Png
};

let mut file_is_stdout: bool = false;
let mut file_path: Option<String> = None;

if args.get_flag("stdout") {
file_is_stdout = true;
let save_location = if args.get_flag("stdout") {
SaveLocation::StdOut
} else if args.get_flag("clipboard") {
SaveLocation::Clipboard
} else if let Some(filepath) = args.get_one::<String>("file") {
file_path = Some(filepath.trim().to_string());
SaveLocation::File(filepath.trim().to_string())
} else {
file_path = Some(utils::get_default_file_name(extension));
}
SaveLocation::File(utils::get_default_file_name(extension))
};

let wayshot_conn = WayshotConnection::new()?;

Expand Down Expand Up @@ -116,16 +117,30 @@ fn main() -> Result<(), Box<dyn Error>> {
wayshot_conn.screenshot_all(cursor_overlay)?
};

if file_is_stdout {
let stdout = stdout();
let mut buffer = Cursor::new(Vec::new());
match save_location {
SaveLocation::File(filepath) => {
image_buffer.save(filepath)?;
}
SaveLocation::StdOut => {
let stdout = stdout();
let mut buffer = Cursor::new(Vec::new());

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

writer.write_all(buffer.get_ref())?;
} else {
image_buffer.save(file_path.unwrap())?;
writer.write_all(buffer.get_ref())?;
}
SaveLocation::Clipboard => {
let mut opts = Options::new();

let mut buffer = Cursor::new(Vec::new());
image_buffer.write_to(&mut buffer, extension)?;
opts.serve_requests(wl_clipboard_rs::copy::ServeRequests::Only(1));
opts.copy(
Source::Bytes(buffer.into_inner().into()),
MimeType::Autodetect,
)?;
}
}

Ok(())
Expand Down
Loading