From b0a8e6db8c6f725f9eeee50d780f44b12099730f Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 3 Mar 2026 20:10:11 +0800 Subject: [PATCH] Simplify the code --- .github/workflows/draft.yml | 14 ++-- .github/workflows/publish.yml | 4 +- yazi-actor/src/lives/file.rs | 10 ++- yazi-binding/src/file.rs | 6 ++ yazi-binding/src/macros.rs | 6 -- yazi-codegen/src/lib.rs | 2 - yazi-config/preset/theme-dark.toml | 2 +- yazi-config/preset/theme-light.toml | 1 + yazi-config/src/theme/icon.rs | 94 ------------------------ yazi-plugin/preset/components/entity.lua | 2 +- 10 files changed, 27 insertions(+), 114 deletions(-) diff --git a/.github/workflows/draft.yml b/.github/workflows/draft.yml index 4df42b5b..f518914e 100644 --- a/.github/workflows/draft.yml +++ b/.github/workflows/draft.yml @@ -59,7 +59,7 @@ jobs: run: ./scripts/build.sh ${{ matrix.target }} - name: Upload artifact - uses: actions/upload-artifact@v6 + uses: actions/upload-artifact@v7 with: name: ${{ matrix.target }} path: | @@ -105,7 +105,7 @@ jobs: Compress-Archive -Path ${env:TARGET_NAME} -DestinationPath "${env:TARGET_NAME}.zip" - name: Upload artifact - uses: actions/upload-artifact@v6 + uses: actions/upload-artifact@v7 with: name: ${{ matrix.target }} path: yazi-${{ matrix.target }}.zip @@ -133,7 +133,7 @@ jobs: run: ./scripts/build.sh ${{ matrix.target }} - name: Upload artifact - uses: actions/upload-artifact@v6 + uses: actions/upload-artifact@v7 with: name: ${{ matrix.target }} path: | @@ -167,7 +167,7 @@ jobs: run: mv yazi_*.snap yazi-${{ matrix.arch }}.snap - name: Upload artifact - uses: actions/upload-artifact@v6 + uses: actions/upload-artifact@v7 with: name: snap-${{ matrix.arch }} path: yazi-${{ matrix.arch }}.snap @@ -176,7 +176,7 @@ jobs: runs-on: ubuntu-latest needs: [build-snap] steps: - - uses: actions/download-artifact@v7 + - uses: actions/download-artifact@v8 with: pattern: snap-* merge-multiple: true @@ -205,7 +205,7 @@ jobs: runs-on: ubuntu-latest needs: [build-unix, build-windows, build-musl, build-snap] steps: - - uses: actions/download-artifact@v7 + - uses: actions/download-artifact@v8 with: merge-multiple: true @@ -235,7 +235,7 @@ jobs: - uses: actions/checkout@v6 - - uses: actions/download-artifact@v7 + - uses: actions/download-artifact@v8 with: merge-multiple: true diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 624175c0..523ef441 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,7 +11,7 @@ jobs: winget: runs-on: ubuntu-latest steps: - - uses: actions/download-artifact@v7 + - uses: actions/download-artifact@v8 with: merge-multiple: true @@ -25,7 +25,7 @@ jobs: snapcraft: runs-on: ubuntu-latest steps: - - uses: actions/download-artifact@v7 + - uses: actions/download-artifact@v8 with: merge-multiple: true diff --git a/yazi-actor/src/lives/file.rs b/yazi-actor/src/lives/file.rs index ac439541..578c384c 100644 --- a/yazi-actor/src/lives/file.rs +++ b/yazi-actor/src/lives/file.rs @@ -65,6 +65,9 @@ impl File { } }) } + + #[inline] + fn is_hovered(&self) -> bool { self.idx == self.folder.cursor } } impl UserData for File { @@ -73,7 +76,7 @@ impl UserData for File { cached_field!(fields, bare, |_, me| Ok(yazi_binding::File::new(&**me))); fields.add_field_method_get("idx", |_, me| Ok(me.idx + 1)); - fields.add_field_method_get("is_hovered", |_, me| Ok(me.idx == me.folder.cursor)); + fields.add_field_method_get("is_hovered", |_, me| Ok(me.is_hovered())); fields.add_field_method_get("in_current", |_, me| Ok(ptr::eq(&*me.folder, &me.tab.current))); fields.add_field_method_get("in_preview", |_, me| { Ok(me.idx == me.folder.cursor && me.tab.hovered().is_some_and(|f| f.url == me.folder.url)) @@ -83,6 +86,11 @@ impl UserData for File { fn add_methods>(methods: &mut M) { yazi_binding::impl_file_methods!(methods); + methods.add_method("icon", |_, me, ()| { + use yazi_binding::Icon; + // TODO: use a cache + Ok(yazi_config::THEME.icon.matches(me, me.is_hovered()).map(Icon::from)) + }); methods.add_method("size", |_, me, ()| { Ok(if me.is_dir() { me.folder.files.sizes.get(&me.urn()).copied() } else { Some(me.len) }) }); diff --git a/yazi-binding/src/file.rs b/yazi-binding/src/file.rs index 19eed92b..62d1c5c4 100644 --- a/yazi-binding/src/file.rs +++ b/yazi-binding/src/file.rs @@ -90,5 +90,11 @@ impl UserData for File { fn add_methods>(methods: &mut M) { impl_file_methods!(methods); + + methods.add_method("icon", |_, me, ()| { + use crate::Icon; + // TODO: use a cache + Ok(yazi_config::THEME.icon.matches(me, false).map(Icon::from)) + }); } } diff --git a/yazi-binding/src/macros.rs b/yazi-binding/src/macros.rs index 53351f87..4cb71fac 100644 --- a/yazi-binding/src/macros.rs +++ b/yazi-binding/src/macros.rs @@ -267,11 +267,5 @@ macro_rules! impl_file_methods { use yazi_fs::FsHash64; Ok(me.hash_u64()) }); - - $methods.add_method("icon", |_, me, hovered: Option| { - use $crate::Icon; - // TODO: use a cache - Ok(yazi_config::THEME.icon.matches(me, hovered.unwrap_or(false)).map(Icon::from)) - }); }; } diff --git a/yazi-codegen/src/lib.rs b/yazi-codegen/src/lib.rs index 47d9b99d..6fb82433 100644 --- a/yazi-codegen/src/lib.rs +++ b/yazi-codegen/src/lib.rs @@ -53,7 +53,6 @@ pub fn deserialize_over1(input: TokenStream) -> TokenStream { quote! { impl #ident { - #[inline] pub(crate) fn deserialize_over_with<'de>(mut self, table: toml::Spanned>) -> Result { use serde::{Deserialize, de::IntoDeserializer}; @@ -96,7 +95,6 @@ pub fn deserialize_over2(input: TokenStream) -> TokenStream { quote! { impl #ident { - #[inline] pub(crate) fn deserialize_over_with<'de>(mut self, table: toml::Spanned>) -> Result { use serde::{Deserialize, de::IntoDeserializer}; diff --git a/yazi-config/preset/theme-dark.toml b/yazi-config/preset/theme-dark.toml index dfc33def..372e5f9c 100644 --- a/yazi-config/preset/theme-dark.toml +++ b/yazi-config/preset/theme-dark.toml @@ -997,7 +997,7 @@ conds = [ { if = "dummy", text = "", fg = "#f44336" }, # Fallback - { if = "dir & hovered", text = "" }, + { if = "dir & hovered", text = "", fg = "#03a9f4" }, { if = "dir", text = "", fg = "#03a9f4" }, { if = "exec", text = "", fg = "#8bc34a" }, { if = "!dir", text = "", fg = "#ffffff" }, diff --git a/yazi-config/preset/theme-light.toml b/yazi-config/preset/theme-light.toml index 0489b216..e68d8309 100644 --- a/yazi-config/preset/theme-light.toml +++ b/yazi-config/preset/theme-light.toml @@ -997,6 +997,7 @@ conds = [ { if = "dummy", text = "", fg = "#f44336" }, # Fallback + { if = "dir & hovered", text = "", fg = "#03a9f4" }, { if = "dir", text = "", fg = "#03a9f4" }, { if = "exec", text = "", fg = "#8bc34a" }, { if = "!dir", text = "", fg = "#000000" }, diff --git a/yazi-config/src/theme/icon.rs b/yazi-config/src/theme/icon.rs index ca6a8955..cdbe9471 100644 --- a/yazi-config/src/theme/icon.rs +++ b/yazi-config/src/theme/icon.rs @@ -206,97 +206,3 @@ impl<'de> Deserialize<'de> for CondIcons { )) } } - -#[cfg(test)] -mod tests { - use std::str::FromStr; - - use yazi_fs::{File, cha::{Cha, ChaType}}; - use yazi_shared::url::Url; - - use super::*; - - fn create_test_file(name: &str, cha_type: ChaType) -> File { - let url = Url::regular(name).to_owned(); - File::from_dummy(url, Some(cha_type)) - } - - #[test] - fn test_matches_hovered() { - // icon with hovered only - let icon = Icon { - conds: CondIcons(vec![(Condition::from_str("hovered").unwrap(), I { - text: "hovered_icon".to_string(), - style: Style::default(), - })]), - ..Default::default() - }; - - let file = create_test_file("test.txt", ChaType::File); - - // should match when hovered - let result = icon.matches(&file, true); - assert!(result.is_some()); - assert_eq!(result.unwrap().text, "hovered_icon"); - - // should not match when not hovered - let result = icon.matches(&file, false); - assert!(result.is_none()); - } - #[test] - fn test_matches_dir_and_hovered_condition() { - // icon with dir and hovered - let icon = Icon { - conds: CondIcons(vec![(Condition::from_str("dir & hovered").unwrap(), I { - text: "dir_hovered".to_string(), - style: Style::default(), - })]), - ..Default::default() - }; - - let dir_file = create_test_file("test_dir", ChaType::Dir); - let file = create_test_file("test.txt", ChaType::File); - - // directory + hovered - let result = icon.matches(&dir_file, true); - assert!(result.is_some()); - assert_eq!(result.unwrap().text, "dir_hovered"); - - // directory + not hovered - let result = icon.matches(&dir_file, false); - assert!(result.is_none()); - - // file + hovered isnt present - let result = icon.matches(&file, true); - assert!(result.is_none()); - } - #[test] - fn test_matches_hovered_before_dir() { - // icon with hovered before dir - let icon = Icon { - conds: CondIcons(vec![ - (Condition::from_str("dir & hovered").unwrap(), I { - text: "dir_hovered".to_string(), - style: Style::default(), - }), - (Condition::from_str("dir").unwrap(), I { - text: "dir_normal".to_string(), - style: Style::default(), - }), - ]), - ..Default::default() - }; - - let dir_file = create_test_file("test_dir", ChaType::Dir); - - // hovered - let result = icon.matches(&dir_file, true); - assert!(result.is_some()); - assert_eq!(result.unwrap().text, "dir_hovered"); - - // non-hovered - let result = icon.matches(&dir_file, false); - assert!(result.is_some()); - assert_eq!(result.unwrap().text, "dir_normal"); - } -} diff --git a/yazi-plugin/preset/components/entity.lua b/yazi-plugin/preset/components/entity.lua index 887fd7ba..aad13b12 100644 --- a/yazi-plugin/preset/components/entity.lua +++ b/yazi-plugin/preset/components/entity.lua @@ -26,7 +26,7 @@ function Entity:padding() end function Entity:icon() - local icon = self._file:icon(self._file.is_hovered) + local icon = self._file:icon() if not icon then return "" elseif self._file.is_hovered then