Skip to content

Commit

Permalink
feat(clipboard): reduce buffer allocations for encoding image
Browse files Browse the repository at this point in the history
  • Loading branch information
CheerfulPianissimo committed Mar 24, 2024
1 parent fae2bc5 commit fede62e
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,45 +127,54 @@ fn main() -> Result<()> {
}
};

let mut buffer = Cursor::new(Vec::new());
image_buffer.write_to(&mut buffer, requested_encoding)?;

if let Some(file) = file {
let mut file = File::create(file)?;
file.write_all(buffer.get_ref())?;
} else {
if stdout_print {
let stdout = stdout();
let mut writer = BufWriter::new(stdout.lock());
writer.write_all(buffer.get_ref())?;
image_buffer.save(file)?;
} else if stdout_print {
let mut buffer = Cursor::new(Vec::new());
image_buffer.write_to(&mut buffer, requested_encoding)?;
let stdout = stdout();
let mut writer = BufWriter::new(stdout.lock());
writer.write_all(buffer.get_ref())?;
if cli.clipboard {
clipboard_daemonize(buffer)?;
}
}

if cli.clipboard {
let mut opts = Options::new();
match unsafe { fork() } {
// Having the image persistently available on the clipboard requires a wayshot process to be alive.
// Fork the process with a child detached from the main process and have the parent exit
Ok(ForkResult::Parent { .. }) => {
return Ok(());
}
Ok(ForkResult::Child) => {
opts.foreground(true); // Offer the image till something else is available on the clipboard
opts.copy(
Source::Bytes(buffer.into_inner().into()),
MimeType::Autodetect,
)?;
}
Err(e) => {
tracing::warn!("Fork failed with error: {e}, couldn't offer image on the clipboard persistently.
Use a clipboard manager to record screenshot.");
opts.copy(
Source::Bytes(buffer.into_inner().into()),
MimeType::Autodetect,
)?;
}
}
if !stdout_print && cli.clipboard {
let mut buffer = Cursor::new(Vec::new());
image_buffer.write_to(&mut buffer, requested_encoding)?;
clipboard_daemonize(buffer)?;
}

Ok(())
}

/// Daemonize and copy the given buffer containing the encoded image to the clipboard
fn clipboard_daemonize(buffer: Cursor<Vec<u8>>) -> Result<()> {
let mut opts = Options::new();
match unsafe { fork() } {
// Having the image persistently available on the clipboard requires a wayshot process to be alive.
// Fork the process with a child detached from the main process and have the parent exit
Ok(ForkResult::Parent { .. }) => {
return Ok(());
}
Ok(ForkResult::Child) => {
opts.foreground(true); // Offer the image till something else is available on the clipboard
opts.copy(
Source::Bytes(buffer.into_inner().into()),
MimeType::Autodetect,
)?;
}
Err(e) => {
tracing::warn!(
"Fork failed with error: {e}, couldn't offer image on the clipboard persistently.
Use a clipboard manager to record screenshot."
);
opts.copy(
Source::Bytes(buffer.into_inner().into()),
MimeType::Autodetect,
)?;
}
}
Ok(())
}

0 comments on commit fede62e

Please sign in to comment.