Skip to content

Commit

Permalink
use temp_file transmission medium for kitty img
Browse files Browse the repository at this point in the history
Use one of those conf:

kitty_graphics_transmission: chunks
kitty_graphics_transmission: temp_file

The faster temp_file mode is the current default.
  • Loading branch information
Canop committed Feb 11, 2024
1 parent d1dba3c commit 8a759e3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "broot"
version = "1.33.1"
version = "1.33.2-dev"
authors = ["dystroy <denys.seguret@gmail.com>"]
repository = "https://github.com/Canop/broot"
homepage = "https://dystroy.org/broot"
Expand Down
5 changes: 5 additions & 0 deletions src/app/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
errors::*,
file_sum,
icon::*,
kitty::TransmissionMedium,
pattern::SearchModeMap,
path::SpecialPaths,
skin::ExtColorMap,
Expand Down Expand Up @@ -118,6 +119,8 @@ pub struct AppContext {
/// the execution of a terminal program.
/// This is determined by app::run on launching the event source.
pub keyboard_enhanced: bool,

pub kitty_graphics_transmission: TransmissionMedium,
}

impl AppContext {
Expand Down Expand Up @@ -216,6 +219,8 @@ impl AppContext {
terminal_title_pattern,
update_work_dir: config.update_work_dir.unwrap_or(true),
keyboard_enhanced: false,
kitty_graphics_transmission: config.kitty_graphics_transmission
.unwrap_or_default(),
})
}
/// Return the --cmd argument, coming from the launch arguments (prefered)
Expand Down
5 changes: 5 additions & 0 deletions src/conf/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
app::Mode,
display::ColsConf,
errors::{ConfError, ProgramError},
kitty::TransmissionMedium,
path::{
path_from,
PathAnchor,
Expand Down Expand Up @@ -122,6 +123,9 @@ pub struct Conf {
#[serde(alias="enable-keyboard-enhancements")]
pub enable_kitty_keyboard: Option<bool>,

#[serde(alias="kitty-graphics-transmission")]
pub kitty_graphics_transmission: Option<TransmissionMedium>,

// BEWARE: entries added here won't be usable unless also
// added in read_file!
}
Expand Down Expand Up @@ -208,6 +212,7 @@ impl Conf {
overwrite!(self, terminal_title, conf);
overwrite!(self, update_work_dir, conf);
overwrite!(self, enable_kitty_keyboard, conf);
overwrite!(self, kitty_graphics_transmission, conf);
self.verbs.append(&mut conf.verbs);
// the following maps are "additive": we can add entries from several
// config files and they still make sense
Expand Down
2 changes: 1 addition & 1 deletion src/image/image_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl ImageView {
}

self.kitty_image_id = kitty_manager
.try_print_image(w, &self.source_img, area, bg, disc.count)?;
.try_print_image(w, &self.source_img, area, bg, disc.count, &disc.con)?;

if self.kitty_image_id.is_some() {
return Ok(());
Expand Down
17 changes: 14 additions & 3 deletions src/kitty/image_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use {
RgbImage,
RgbaImage,
},
serde::Deserialize,
std::{
io::{self, Write},
},
Expand All @@ -35,12 +36,17 @@ use {
///
/// Note that I didn't test yet the named shared memory
/// solution offered by kitty.
#[derive(Debug)]
///
/// Documentation:
/// https://sw.kovidgoyal.net/kitty/graphics-protocol/#the-transmission-medium
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TransmissionMedium {
/// write a temp file, then give its path to kitty
/// in the payload of the escape sequence. It's quite
/// fast on SSD but a big downside is that it doesn't
/// work if you're distant
#[default]
TempFile,
/// send the whole rgb or rgba data, encoded in base64,
/// in the payloads of several escape sequence (each one
Expand All @@ -49,6 +55,10 @@ pub enum TransmissionMedium {
Chunks,
}

pub struct KittyImageRendererOptions {
pub transmission_medium: TransmissionMedium,
}

enum ImageData<'i> {
RgbRef(&'i RgbImage),
RgbaRef(&'i RgbaImage),
Expand Down Expand Up @@ -203,7 +213,7 @@ impl<'i> KittyImage<'i> {

impl KittyImageRenderer {
/// Called only once (at most) by the KittyManager
pub fn new() -> Option<Self> {
pub fn new(options: &KittyImageRendererOptions) -> Option<Self> {
if !is_kitty_graphics_protocol_supported() {
return None;
}
Expand All @@ -213,7 +223,7 @@ impl KittyImageRenderer {
cell_width,
cell_height,
next_id: 1,
transmission_medium: TransmissionMedium::Chunks,
transmission_medium: options.transmission_medium,
})
}
/// return a new image id
Expand All @@ -240,6 +250,7 @@ impl KittyImageRenderer {

let img = KittyImage::new(src, area, self);
debug!("transmission medium: {:?}", self.transmission_medium);
w.flush()?;
match self.transmission_medium {
TransmissionMedium::TempFile => img.print_with_temp_file(w)?,
TransmissionMedium::Chunks => img.print_with_chunks(w)?,
Expand Down
14 changes: 11 additions & 3 deletions src/kitty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use {

use {
crate::{
app::AppContext,
display::W,
errors::ProgramError,
image::SourceImage,
Expand Down Expand Up @@ -64,15 +65,21 @@ impl KittyManager {
_ => None,
}
}
pub fn renderer(&mut self) -> Option<&mut KittyImageRenderer> {
pub fn renderer(
&mut self,
con: &AppContext,
) -> Option<&mut KittyImageRenderer> {
if matches!(self.renderer, MaybeRenderer::Disabled) {
return None;
}
if matches!(self.renderer, MaybeRenderer::Enabled { .. }) {
return self.renderer_if_tested();
}
let options = KittyImageRendererOptions {
transmission_medium: con.kitty_graphics_transmission,
};
// we're in the Untested branch
match KittyImageRenderer::new() {
match KittyImageRenderer::new(&options) {
Some(renderer) => {
self.renderer = MaybeRenderer::Enabled { renderer };
self.renderer_if_tested()
Expand Down Expand Up @@ -101,8 +108,9 @@ impl KittyManager {
area: &Area,
bg: Color,
drawing_count: usize,
con: &AppContext,
) -> Result<Option<KittyImageId>, ProgramError> {
if let Some(renderer) = self.renderer() {
if let Some(renderer) = self.renderer(con) {
let img = src.optimal()?;
let new_id = renderer.print(w, &img, area, bg)?;
self.rendered_images.push(RenderedImage {
Expand Down

0 comments on commit 8a759e3

Please sign in to comment.