diff --git a/termwiz/Cargo.toml b/termwiz/Cargo.toml index b74e9b6d0..743cf4e23 100644 --- a/termwiz/Cargo.toml +++ b/termwiz/Cargo.toml @@ -33,7 +33,7 @@ pest.workspace = true pest_derive.workspace = true phf.workspace = true serde = {workspace=true, features = ["rc", "derive"], optional=true} -sha2.workspace = true +sha2 = {workspace=true, optional=true} siphasher.workspace = true terminfo.workspace = true thiserror.workspace = true @@ -41,16 +41,18 @@ ucd-trie.workspace = true unicode-segmentation.workspace = true vtparse.workspace = true wezterm-bidi.workspace = true -wezterm-blob-leases.workspace = true +wezterm-blob-leases = {workspace=true, optional=true} wezterm-color-types.workspace = true wezterm-dynamic.workspace = true wezterm-input-types.workspace = true [features] +default = ["image"] widgets = ["cassowary", "fnv"] use_serde = ["serde", "wezterm-color-types/use_serde", "wezterm-blob-leases/serde", "bitflags/serde", "wezterm-input-types/serde"] -use_image = ["image"] -docs = ["widgets", "use_serde"] +use_image = ["image", "dep:image"] +image = ["sha2", "wezterm-blob-leases"] +docs = ["widgets", "use_serde", "image"] [dev-dependencies] criterion.workspace = true diff --git a/termwiz/src/cell.rs b/termwiz/src/cell.rs index 58e240285..25fdb6051 100644 --- a/termwiz/src/cell.rs +++ b/termwiz/src/cell.rs @@ -3,6 +3,7 @@ use crate::color::{ColorAttribute, PaletteIndex}; pub use crate::emoji::Presentation; use crate::emoji_variation::WCWIDTH_TABLE; pub use crate::escape::osc::Hyperlink; +#[cfg(feature = "image")] use crate::image::ImageCell; use crate::widechar_width::WcWidth; use finl_unicode::grapheme_clusters::Graphemes; @@ -81,6 +82,7 @@ struct FatAttributes { /// The hyperlink content, if any hyperlink: Option>, /// The image data, if any + #[cfg(feature = "image")] image: Vec>, /// The color of the underline. If None, then /// the foreground color is to be used @@ -94,6 +96,7 @@ impl FatAttributes { if let Some(link) = &self.hyperlink { link.compute_shape_hash(hasher); } + #[cfg(feature = "image")] for cell in &self.image { cell.compute_shape_hash(hasher); } @@ -384,6 +387,7 @@ impl CellAttributes { if self.fat.is_none() { self.fat.replace(Box::new(FatAttributes { hyperlink: None, + #[cfg(feature = "image")] image: vec![], underline_color: ColorAttribute::Default, foreground: ColorAttribute::Default, @@ -397,8 +401,13 @@ impl CellAttributes { .fat .as_ref() .map(|fat| { - fat.image.is_empty() - && fat.hyperlink.is_none() + #[cfg(feature = "image")] + { + if !fat.image.is_empty() { + return false; + } + } + fat.hyperlink.is_none() && fat.underline_color == ColorAttribute::Default && fat.foreground == ColorAttribute::Default && fat.background == ColorAttribute::Default @@ -419,7 +428,10 @@ impl CellAttributes { self } } +} +#[cfg(feature = "image")] +impl CellAttributes { /// Assign a single image to a cell. pub fn set_image(&mut self, image: Box) -> &mut Self { self.allocate_fat_attributes(); @@ -458,7 +470,9 @@ impl CellAttributes { } self } +} +impl CellAttributes { pub fn set_underline_color>( &mut self, underline_color: C, @@ -523,6 +537,7 @@ impl CellAttributes { /// Returns the list of attached images in z-index order. /// Returns None if there are no attached images; will /// never return Some(vec![]). + #[cfg(feature = "image")] pub fn images(&self) -> Option> { let fat = self.fat.as_ref()?; if fat.image.is_empty() { diff --git a/termwiz/src/error.rs b/termwiz/src/error.rs index 902c99658..175d96161 100644 --- a/termwiz/src/error.rs +++ b/termwiz/src/error.rs @@ -72,6 +72,7 @@ pub enum InternalError { #[error(transparent)] FileDescriptor(#[from] filedescriptor::Error), + #[cfg(feature = "image")] #[error(transparent)] BlobLease(#[from] wezterm_blob_leases::Error), diff --git a/termwiz/src/lib.rs b/termwiz/src/lib.rs index 812321a32..add1ce898 100644 --- a/termwiz/src/lib.rs +++ b/termwiz/src/lib.rs @@ -53,6 +53,7 @@ pub mod color; pub mod error; pub mod escape; pub mod hyperlink; +#[cfg(feature = "image")] pub mod image; pub mod input; pub mod istty; diff --git a/termwiz/src/render/terminfo.rs b/termwiz/src/render/terminfo.rs index 41746fb2b..bab9c3875 100644 --- a/termwiz/src/render/terminfo.rs +++ b/termwiz/src/render/terminfo.rs @@ -4,8 +4,11 @@ use crate::cell::{AttributeChange, Blink, CellAttributes, Intensity, Underline}; use crate::color::{ColorAttribute, ColorSpec}; use crate::escape::csi::{Cursor, Edit, EraseInDisplay, EraseInLine, Sgr, CSI}; use crate::escape::esc::EscCode; -use crate::escape::osc::{ITermDimension, ITermFileData, ITermProprietary, OperatingSystemCommand}; +use crate::escape::osc::OperatingSystemCommand; +#[cfg(feature = "image")] +use crate::escape::osc::{ITermDimension, ITermFileData, ITermProprietary}; use crate::escape::{Esc, OneBased}; +#[cfg(feature = "image")] use crate::image::{ImageDataType, TextureCoordinate}; use crate::render::RenderTty; use crate::surface::{Change, CursorShape, CursorVisibility, LineAttribute, Position}; @@ -580,6 +583,7 @@ impl TerminfoRenderer { } } }, + #[cfg(feature = "image")] Change::Image(image) => { if self.caps.iterm2_image() { let data = if image.top_left == TextureCoordinate::new_f32(0.0, 0.0) diff --git a/termwiz/src/surface/change.rs b/termwiz/src/surface/change.rs index 02c885a8a..1e7d1478e 100644 --- a/termwiz/src/surface/change.rs +++ b/termwiz/src/surface/change.rs @@ -1,10 +1,12 @@ use crate::cell::{unicode_column_width, AttributeChange, CellAttributes}; use crate::color::ColorAttribute; +#[cfg(feature = "image")] pub use crate::image::{ImageData, TextureCoordinate}; use crate::surface::{CursorShape, CursorVisibility, Position}; use finl_unicode::grapheme_clusters::Graphemes; #[cfg(feature = "use_serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "image")] use std::sync::Arc; #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] @@ -63,6 +65,7 @@ pub enum Change { /// of the screen, the screen will scroll up. /// The cursor Y position is unchanged by rendering the Image. /// The cursor X position will be incremented by `Image::width` cells. + #[cfg(feature = "image")] Image(Image), /// Scroll the `region_size` lines starting at `first_row` upwards /// by `scroll_count` lines. The `scroll_count` lines at the top of @@ -228,6 +231,7 @@ impl ChangeSequence { } self.update_render_height(); } + #[cfg(feature = "image")] Change::Image(im) => { self.cursor_x += im.width; self.render_y_max = self.render_y_max.max(self.cursor_y + im.height as isize); @@ -280,6 +284,7 @@ impl ChangeSequence { /// The top left cell from that image, if it were to be included in a diff, /// would be recorded as `width=1`, `height=1`, `top_left=(0,0)`, `bottom_right=(1/4,1/3)`. #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] +#[cfg(feature = "image")] #[derive(Debug, Clone, Eq, PartialEq)] pub struct Image { /// measured in cells diff --git a/termwiz/src/surface/line/vecstorage.rs b/termwiz/src/surface/line/vecstorage.rs index 4149f7fee..b75f359f9 100644 --- a/termwiz/src/surface/line/vecstorage.rs +++ b/termwiz/src/surface/line/vecstorage.rs @@ -16,7 +16,9 @@ impl VecStorage { Self { cells } } + #[cfg_attr(not(feature = "image"), allow(unused_mut, unused_variables))] pub(crate) fn set_cell(&mut self, idx: usize, mut cell: Cell, clear_image_placement: bool) { + #[cfg(feature = "image")] if !clear_image_placement { if let Some(images) = self.cells[idx].attrs().images() { for image in images { diff --git a/termwiz/src/surface/mod.rs b/termwiz/src/surface/mod.rs index 6865f68f8..c2f89a87b 100644 --- a/termwiz/src/surface/mod.rs +++ b/termwiz/src/surface/mod.rs @@ -1,9 +1,9 @@ use crate::cell::{Cell, CellAttributes}; use crate::color::ColorAttribute; +#[cfg(feature = "image")] use crate::image::ImageCell; use crate::surface::line::CellRef; use finl_unicode::grapheme_clusters::Graphemes; -use ordered_float::NotNan; #[cfg(feature = "use_serde")] use serde::{Deserialize, Serialize}; use std::borrow::Cow; @@ -13,7 +13,9 @@ use wezterm_dynamic::{FromDynamic, ToDynamic}; pub mod change; pub mod line; -pub use self::change::{Change, Image, LineAttribute, TextureCoordinate}; +pub use self::change::{Change, LineAttribute}; +#[cfg(feature = "image")] +pub use self::change::{Image, TextureCoordinate}; pub use self::line::Line; /// Position holds 0-based positioning information, where @@ -297,6 +299,7 @@ impl Surface { Change::CursorColor(color) => self.cursor_color = *color, Change::CursorShape(shape) => self.cursor_shape = Some(*shape), Change::CursorVisibility(visibility) => self.cursor_visibility = *visibility, + #[cfg(feature = "image")] Change::Image(image) => self.add_image(image), Change::Title(text) => self.title = text.to_owned(), Change::ScrollRegionUp { @@ -313,7 +316,10 @@ impl Surface { } } + #[cfg(feature = "image")] fn add_image(&mut self, image: &Image) { + use ordered_float::NotNan; + let xsize = (image.bottom_right.x - image.top_left.x) / image.width as f32; let ysize = (image.bottom_right.y - image.top_left.y) / image.height as f32;