From 97452d0ccc421fd69ec60c31ad19a59d95cd691b Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 16 Mar 2023 22:41:52 -0700 Subject: [PATCH] term: improve readability of error message for oversized image refs: https://github.com/wez/wezterm/issues/3264 --- Cargo.lock | 16 ++++++++++++++++ term/Cargo.toml | 1 + term/src/terminalstate/image.rs | 8 +++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c559fe706..ee10e8204 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2126,6 +2126,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" +[[package]] +name = "humansize" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7" +dependencies = [ + "libm", +] + [[package]] name = "humantime" version = "2.1.0" @@ -2537,6 +2546,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "libm" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "348108ab3fba42ec82ff6e9564fc4ca0247bdccdc68dd8af9764bbc79c3c8ffb" + [[package]] name = "libsqlite3-sys" version = "0.24.2" @@ -6064,6 +6079,7 @@ dependencies = [ "env_logger", "finl_unicode", "hex", + "humansize", "image", "k9", "lazy_static", diff --git a/term/Cargo.toml b/term/Cargo.toml index 57a16f354..513820f6f 100644 --- a/term/Cargo.toml +++ b/term/Cargo.toml @@ -17,6 +17,7 @@ use_serde = ["termwiz/use_serde"] anyhow = "1.0" bitflags = "1.3" csscolorparser = "0.6" +humansize = "2.1" miniz_oxide = "0.4" finl_unicode = "1.2" hex = "0.4" diff --git a/term/src/terminalstate/image.rs b/term/src/terminalstate/image.rs index 7fbf1d83d..774d57c69 100644 --- a/term/src/terminalstate/image.rs +++ b/term/src/terminalstate/image.rs @@ -1,5 +1,6 @@ use crate::{Position, StableRowIndex, TerminalState}; use anyhow::Context; +use humansize::{SizeFormatter, DECIMAL}; use ordered_float::NotNan; use std::sync::Arc; use termwiz::cell::Cell; @@ -244,11 +245,12 @@ pub(crate) fn check_image_dimensions(width: u32, height: u32) -> anyhow::Result< let size = width.saturating_mul(height).saturating_mul(4); if size > MAX_IMAGE_SIZE { anyhow::bail!( - "Ignoring image data {}x{} because {} bytes > max allowed {}", + "Ignoring image data for image with dimensions {}x{} \ + because required RAM {} > max allowed {}", width, height, - size, - MAX_IMAGE_SIZE + SizeFormatter::new(size, DECIMAL), + SizeFormatter::new(MAX_IMAGE_SIZE, DECIMAL), ); } Ok(())