diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c2012d..3090287 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/printer/kitty.rs b/src/printer/kitty.rs index db693d3..992285f 100644 --- a/src/printer/kitty.rs +++ b/src/printer/kitty.rs @@ -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; @@ -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 @@ -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 = [ @@ -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)) }