From 6e09ec3206393fef051feeba65611e049d39148d Mon Sep 17 00:00:00 2001 From: Lzzzt <101313294+Lzzzzzt@users.noreply.github.com> Date: Sun, 7 Jul 2024 07:21:48 +0800 Subject: [PATCH] rebase (#547) Signed-off-by: Lzzzt --- Cargo.lock | 7 ++++++ Cargo.toml | 1 + config/joshuto.toml | 2 +- docs/configuration/joshuto.toml.md | 4 ++-- src/config/raw/app/display/preview.rs | 33 +++++++++++++++++++++++++-- 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6871c8f0e..d91908c79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -288,6 +288,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" +[[package]] +name = "bytesize" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e368af43e418a04d52505cf3dbc23dda4e3407ae2fa99fd0e4f308ce546acc" + [[package]] name = "cassowary" version = "0.3.0" @@ -1067,6 +1073,7 @@ dependencies = [ "alphanumeric-sort", "ansi-to-tui", "bitflags 2.6.0", + "bytesize", "chrono", "clap", "clap_complete", diff --git a/Cargo.toml b/Cargo.toml index fc95d5e74..cd7f0794d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ unicode-width = "^0" walkdir = "^2" whoami = "^1" xdg = "^2" +bytesize = "1.3.0" [dependencies.nix] version = "^0" diff --git a/config/joshuto.toml b/config/joshuto.toml index 64e6462d5..26ecb3df4 100644 --- a/config/joshuto.toml +++ b/config/joshuto.toml @@ -39,7 +39,7 @@ directories_first = true reverse = false [preview] -max_preview_size = 2097152 # 2MB +max_preview_size = "2MB" # 2MB preview_script = "~/.config/joshuto/preview_file.sh" # make sure it's marked as executable [search] diff --git a/docs/configuration/joshuto.toml.md b/docs/configuration/joshuto.toml.md index e7d70e9be..367184cb7 100644 --- a/docs/configuration/joshuto.toml.md +++ b/docs/configuration/joshuto.toml.md @@ -33,8 +33,8 @@ watch_files = true # - `:mkdir ./b` keeps the cursor where it was focus_on_create = true -# The maximum file size to show a preview for -max_preview_size = 2097152 # 2MB +# The maximum file size to show a preview for, it can be "2 MB", "2 Mb" or "2097152" +max_preview_size = "2 MB" # Update the zoxide database with every navigation type instead of only with the z command zoxide_update = false diff --git a/src/config/raw/app/display/preview.rs b/src/config/raw/app/display/preview.rs index e4b3b1f5f..2e2e9e7aa 100644 --- a/src/config/raw/app/display/preview.rs +++ b/src/config/raw/app/display/preview.rs @@ -1,6 +1,8 @@ use allmytoes::ThumbSize; +use bytesize::ByteSize; use ratatui_image::picker::ProtocolType; -use serde::Deserialize; +use serde::{de::Unexpected, Deserialize, Deserializer}; +use toml::Value; pub const fn default_max_preview_size() -> u64 { 2 * 1024 * 1024 // 2 MB @@ -43,7 +45,10 @@ impl XDGThumbSizes { #[derive(Clone, Debug, Deserialize)] pub struct PreviewOptionRaw { - #[serde(default = "default_max_preview_size")] + #[serde( + default = "default_max_preview_size", + deserialize_with = "deserialize_max_preview_size" + )] pub max_preview_size: u64, #[serde(default)] pub preview_protocol: PreviewProtocol, @@ -73,3 +78,27 @@ impl std::default::Default for PreviewOptionRaw { } } } + +fn deserialize_max_preview_size<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let value: Value = Deserialize::deserialize(deserializer)?; + + let string = match value { + Value::String(s) => s, + Value::Integer(i) => (i as u64).to_string(), + v => { + return Err(serde::de::Error::invalid_type( + Unexpected::Other(v.type_str()), + &"String or Integer", + )) + } + }; + + let size = string + .parse::() + .map_err(serde::de::Error::custom)?; + + Ok(size.as_u64()) +}