mirror of
https://github.com/wez/wezterm.git
synced 2024-12-23 21:32:13 +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:
parent
7f9d96328c
commit
b862c8d111
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -4336,6 +4336,7 @@ dependencies = [
|
||||
"regex",
|
||||
"semver 0.11.0",
|
||||
"serde",
|
||||
"sha2",
|
||||
"signal-hook 0.1.17",
|
||||
"terminfo",
|
||||
"termios 0.3.3",
|
||||
@ -5142,7 +5143,6 @@ dependencies = [
|
||||
"pretty_assertions",
|
||||
"pretty_env_logger",
|
||||
"serde",
|
||||
"sha2",
|
||||
"terminfo",
|
||||
"termwiz",
|
||||
"unicode-segmentation",
|
||||
|
@ -26,7 +26,6 @@ num-traits = "0.2"
|
||||
ordered-float = "2.7"
|
||||
palette = "0.5"
|
||||
serde = {version="1.0", features = ["rc"]}
|
||||
sha2 = "0.9"
|
||||
terminfo = "0.7"
|
||||
unicode-segmentation = "1.7"
|
||||
unicode-width = "0.1"
|
||||
|
@ -162,14 +162,7 @@ impl TerminalState {
|
||||
|
||||
/// 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> {
|
||||
use sha2::Digest;
|
||||
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();
|
||||
|
||||
let key = data.compute_hash();
|
||||
if let Some(item) = self.image_cache.get(&key) {
|
||||
Arc::clone(item)
|
||||
} else {
|
||||
|
@ -28,6 +28,7 @@ ordered-float = "2.7"
|
||||
regex = "1"
|
||||
semver = "0.11"
|
||||
serde = {version="1.0", features = ["rc", "derive"], optional=true}
|
||||
sha2 = "0.9"
|
||||
terminfo = "0.7"
|
||||
thiserror = "1.0"
|
||||
unicode-segmentation = "1.7"
|
||||
|
@ -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))]
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
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))]
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct ImageData {
|
||||
id: usize,
|
||||
hash: [u8; 32],
|
||||
data: ImageDataType,
|
||||
}
|
||||
|
||||
@ -205,7 +218,8 @@ impl ImageData {
|
||||
|
||||
pub fn with_data(data: ImageDataType) -> Self {
|
||||
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 {
|
||||
@ -220,8 +234,11 @@ impl ImageData {
|
||||
&self.data
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn id(&self) -> usize {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub fn hash(&self) -> [u8; 32] {
|
||||
self.hash
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user