From 7dcfaabd94c6274265c1579e7b6d6f5fb690885b Mon Sep 17 00:00:00 2001 From: CheerfulPianissimo Date: Sun, 24 Mar 2024 20:57:07 +0530 Subject: [PATCH] feat(clipboard): improve code quality --- wayshot/src/wayshot.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index c6f446b1..45c905c9 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -127,6 +127,7 @@ fn main() -> Result<()> { } }; + let mut image_buf: Option>> = None; if let Some(file) = file { image_buffer.save(file)?; } else if stdout_print { @@ -135,14 +136,18 @@ fn main() -> Result<()> { let stdout = stdout(); let mut writer = BufWriter::new(stdout.lock()); writer.write_all(buffer.get_ref())?; - if cli.clipboard { - clipboard_daemonize(buffer)?; - } + image_buf = Some(buffer); } - if !stdout_print && cli.clipboard { - let mut buffer = Cursor::new(Vec::new()); - image_buffer.write_to(&mut buffer, requested_encoding)?; + if cli.clipboard { + let buffer = match image_buf { + Some(buf) => buf, + None => { + let mut buffer = Cursor::new(Vec::new()); + image_buffer.write_to(&mut buffer, requested_encoding)?; + buffer + } + }; clipboard_daemonize(buffer)?; }