1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 13:52:55 +03:00

termwiz: add ImageData::hash

Moves the localized hashing logic from term -> termwiz
where it can be re-used.

refs: #986
This commit is contained in:
Wez Furlong 2021-08-02 09:10:36 -07:00
parent 7f9d96328c
commit b862c8d111
5 changed files with 24 additions and 14 deletions

2
Cargo.lock generated
View File

@ -4336,6 +4336,7 @@ dependencies = [
"regex", "regex",
"semver 0.11.0", "semver 0.11.0",
"serde", "serde",
"sha2",
"signal-hook 0.1.17", "signal-hook 0.1.17",
"terminfo", "terminfo",
"termios 0.3.3", "termios 0.3.3",
@ -5142,7 +5143,6 @@ dependencies = [
"pretty_assertions", "pretty_assertions",
"pretty_env_logger", "pretty_env_logger",
"serde", "serde",
"sha2",
"terminfo", "terminfo",
"termwiz", "termwiz",
"unicode-segmentation", "unicode-segmentation",

View File

@ -26,7 +26,6 @@ num-traits = "0.2"
ordered-float = "2.7" ordered-float = "2.7"
palette = "0.5" palette = "0.5"
serde = {version="1.0", features = ["rc"]} serde = {version="1.0", features = ["rc"]}
sha2 = "0.9"
terminfo = "0.7" terminfo = "0.7"
unicode-segmentation = "1.7" unicode-segmentation = "1.7"
unicode-width = "0.1" unicode-width = "0.1"

View File

@ -162,14 +162,7 @@ impl TerminalState {
/// cache recent images and avoid assigning a new id for repeated data! /// cache recent images and avoid assigning a new id for repeated data!
pub(crate) fn raw_image_to_image_data(&mut self, data: ImageDataType) -> Arc<ImageData> { pub(crate) fn raw_image_to_image_data(&mut self, data: ImageDataType) -> Arc<ImageData> {
use sha2::Digest; let key = data.compute_hash();
let mut hasher = sha2::Sha256::new();
match &data {
ImageDataType::EncodedFile(data) => hasher.update(data),
ImageDataType::Rgba8 { data, .. } => hasher.update(data),
};
let key = hasher.finalize().into();
if let Some(item) = self.image_cache.get(&key) { if let Some(item) = self.image_cache.get(&key) {
Arc::clone(item) Arc::clone(item)
} else { } else {

View File

@ -28,6 +28,7 @@ ordered-float = "2.7"
regex = "1" regex = "1"
semver = "0.11" semver = "0.11"
serde = {version="1.0", features = ["rc", "derive"], optional=true} serde = {version="1.0", features = ["rc", "derive"], optional=true}
sha2 = "0.9"
terminfo = "0.7" terminfo = "0.7"
thiserror = "1.0" thiserror = "1.0"
unicode-segmentation = "1.7" unicode-segmentation = "1.7"

View File

@ -153,8 +153,6 @@ impl ImageCell {
} }
} }
static IMAGE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
pub enum ImageDataType { pub enum ImageDataType {
@ -190,10 +188,25 @@ impl std::fmt::Debug for ImageDataType {
} }
} }
impl ImageDataType {
pub fn compute_hash(&self) -> [u8; 32] {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
match self {
ImageDataType::EncodedFile(data) => hasher.update(data),
ImageDataType::Rgba8 { data, .. } => hasher.update(data),
};
hasher.finalize().into()
}
}
static IMAGE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct ImageData { pub struct ImageData {
id: usize, id: usize,
hash: [u8; 32],
data: ImageDataType, data: ImageDataType,
} }
@ -205,7 +218,8 @@ impl ImageData {
pub fn with_data(data: ImageDataType) -> Self { pub fn with_data(data: ImageDataType) -> Self {
let id = IMAGE_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed); let id = IMAGE_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed);
Self { id, data } let hash = data.compute_hash();
Self { id, hash, data }
} }
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
@ -220,8 +234,11 @@ impl ImageData {
&self.data &self.data
} }
#[inline]
pub fn id(&self) -> usize { pub fn id(&self) -> usize {
self.id self.id
} }
pub fn hash(&self) -> [u8; 32] {
self.hash
}
} }