termwiz: make image support optional with a Cargo feature

The `image` module introduces some extra dependencies that seem a bit
heavyweight. This change adds an `image` Cargo feature to make related
features optional.

    ├── sha2 v0.10.8
    │   ├── cfg-if v1.0.0
    │   ├── cpufeatures v0.2.17
    │   └── digest v0.10.7
    │       ├── block-buffer v0.10.4
    │       │   └── generic-array v0.14.7
    │       │       └── typenum v1.18.0
    │       └── crypto-common v0.1.6
    │           ├── generic-array v0.14.7 (*)
    │           └── typenum v1.18.0
    └── wezterm-blob-leases v0.1.1
        ├── getrandom v0.3.1
        │   ├── cfg-if v1.0.0
        │   └── libc v0.2.170
        ├── mac_address v1.1.8
        │   └── nix v0.29.0 (*)
        ├── sha2 v0.10.8 (*)
        ├── thiserror v1.0.69 (*)
        └── uuid v1.15.1
            ├── atomic v0.6.0
            │   └── bytemuck v1.22.0
            └── getrandom v0.3.1 (*)

The `image` feature is default on to maintain compatibility.

Test Plan:

    cargo check --no-default-features
    cargo check
This commit is contained in:
Jun Wu authored and Wez Furlong committed 2025-04-12 22:05:40 -07:00
1 parent a873585160
commit b4261af93e
8 files changed
+45 -9

No files matched your search

+6 -4
View File
@@ -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
+17 -2
View File
@@ -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<Arc<Hyperlink>>,
/// The image data, if any
#[cfg(feature = "image")]
image: Vec<Box<ImageCell>>,
/// 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<ImageCell>) -> &mut Self {
self.allocate_fat_attributes();
@@ -458,7 +470,9 @@ impl CellAttributes {
}
self
}
}
impl CellAttributes {
pub fn set_underline_color<C: Into<ColorAttribute>>(
&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<Vec<ImageCell>> {
let fat = self.fat.as_ref()?;
if fat.image.is_empty() {
+1
View File
@@ -72,6 +72,7 @@ pub enum InternalError {
#[error(transparent)]
FileDescriptor(#[from] filedescriptor::Error),
#[cfg(feature = "image")]
#[error(transparent)]
BlobLease(#[from] wezterm_blob_leases::Error),
+1
View File
@@ -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;
+5 -1
View File
@@ -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)
+5
View File
@@ -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
+2
View File
@@ -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 {
+8 -2
View File
@@ -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;