Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Remove `lazy_static` dependency in favor of `std::sync::LazyLock`
- MSRV is now 1.80
- Use sixel if found in device attributes instead of static TERM list
- Dont Error in kitty if the temporary file has been deleted by the terminal. (Now `KittySupport::Local` is possible again)

## 0.9.2
- Use iterm and sixel in more terminals
Expand Down
22 changes: 17 additions & 5 deletions src/printer/kitty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::printer::{adjust_offset, find_best_fit, Printer, ReadKey};
use crate::Config;
use base64::{engine::general_purpose, Engine};
use console::{Key, Term};
use std::io::Error;
use std::io::Write;
use std::io::{Error, ErrorKind};
use std::sync::LazyLock;
use tempfile::NamedTempFile;

Expand Down Expand Up @@ -72,6 +72,20 @@ fn check_kitty_support() -> KittySupport {
KittySupport::None
}

/// Close the temporary file that was created, filtering out [`NotFound`](ErrorKind::NotFound) errors.
fn close_tmp_file(temp_file: NamedTempFile) -> ViuResult {
// Explicitly clean up when finished with the file because destructor, OS and Kitty are not deterministic.
if let Err(err) = temp_file.close() {
// Proper Kitty terminals *will delete* the file after fully reading it, if it is in a known temporary directory
// so we dont want to error if the file does not exist anymore
if err.kind() != ErrorKind::NotFound {
return Err(err.into());
}
}

Ok(())
}

/// Query the terminal whether it can display an image from a file
fn has_local_support(stdin: &impl ReadKey, stdout: &mut impl Write) -> ViuResult {
// create a temp file that will hold a 1x1 image
Expand Down Expand Up @@ -106,8 +120,7 @@ fn has_local_support(stdin: &impl ReadKey, stdout: &mut impl Write) -> ViuResult
}
}

// Explicitly clean up when finished with the file because destructor, OS and Kitty are not deterministic.
temp_file.close()?;
close_tmp_file(temp_file)?;

// Kitty response should end with these 3 Keys if it was successful
let expected = [
Expand Down Expand Up @@ -157,8 +170,7 @@ fn print_local(
writeln!(stdout)?;
stdout.flush()?;

// Explicitly clean up when finished with the file because destructor, OS and Kitty are not deterministic.
temp_file.close()?;
close_tmp_file(temp_file)?;

Ok((w, h))
}
Expand Down