mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Simplify the code
This commit is contained in:
parent
b1c41912b4
commit
b0a8e6db8c
10 changed files with 27 additions and 114 deletions
14
.github/workflows/draft.yml
vendored
14
.github/workflows/draft.yml
vendored
|
|
@ -59,7 +59,7 @@ jobs:
|
||||||
run: ./scripts/build.sh ${{ matrix.target }}
|
run: ./scripts/build.sh ${{ matrix.target }}
|
||||||
|
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v6
|
uses: actions/upload-artifact@v7
|
||||||
with:
|
with:
|
||||||
name: ${{ matrix.target }}
|
name: ${{ matrix.target }}
|
||||||
path: |
|
path: |
|
||||||
|
|
@ -105,7 +105,7 @@ jobs:
|
||||||
Compress-Archive -Path ${env:TARGET_NAME} -DestinationPath "${env:TARGET_NAME}.zip"
|
Compress-Archive -Path ${env:TARGET_NAME} -DestinationPath "${env:TARGET_NAME}.zip"
|
||||||
|
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v6
|
uses: actions/upload-artifact@v7
|
||||||
with:
|
with:
|
||||||
name: ${{ matrix.target }}
|
name: ${{ matrix.target }}
|
||||||
path: yazi-${{ matrix.target }}.zip
|
path: yazi-${{ matrix.target }}.zip
|
||||||
|
|
@ -133,7 +133,7 @@ jobs:
|
||||||
run: ./scripts/build.sh ${{ matrix.target }}
|
run: ./scripts/build.sh ${{ matrix.target }}
|
||||||
|
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v6
|
uses: actions/upload-artifact@v7
|
||||||
with:
|
with:
|
||||||
name: ${{ matrix.target }}
|
name: ${{ matrix.target }}
|
||||||
path: |
|
path: |
|
||||||
|
|
@ -167,7 +167,7 @@ jobs:
|
||||||
run: mv yazi_*.snap yazi-${{ matrix.arch }}.snap
|
run: mv yazi_*.snap yazi-${{ matrix.arch }}.snap
|
||||||
|
|
||||||
- name: Upload artifact
|
- name: Upload artifact
|
||||||
uses: actions/upload-artifact@v6
|
uses: actions/upload-artifact@v7
|
||||||
with:
|
with:
|
||||||
name: snap-${{ matrix.arch }}
|
name: snap-${{ matrix.arch }}
|
||||||
path: yazi-${{ matrix.arch }}.snap
|
path: yazi-${{ matrix.arch }}.snap
|
||||||
|
|
@ -176,7 +176,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [build-snap]
|
needs: [build-snap]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/download-artifact@v7
|
- uses: actions/download-artifact@v8
|
||||||
with:
|
with:
|
||||||
pattern: snap-*
|
pattern: snap-*
|
||||||
merge-multiple: true
|
merge-multiple: true
|
||||||
|
|
@ -205,7 +205,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: [build-unix, build-windows, build-musl, build-snap]
|
needs: [build-unix, build-windows, build-musl, build-snap]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/download-artifact@v7
|
- uses: actions/download-artifact@v8
|
||||||
with:
|
with:
|
||||||
merge-multiple: true
|
merge-multiple: true
|
||||||
|
|
||||||
|
|
@ -235,7 +235,7 @@ jobs:
|
||||||
|
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v6
|
||||||
|
|
||||||
- uses: actions/download-artifact@v7
|
- uses: actions/download-artifact@v8
|
||||||
with:
|
with:
|
||||||
merge-multiple: true
|
merge-multiple: true
|
||||||
|
|
||||||
|
|
|
||||||
4
.github/workflows/publish.yml
vendored
4
.github/workflows/publish.yml
vendored
|
|
@ -11,7 +11,7 @@ jobs:
|
||||||
winget:
|
winget:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/download-artifact@v7
|
- uses: actions/download-artifact@v8
|
||||||
with:
|
with:
|
||||||
merge-multiple: true
|
merge-multiple: true
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@ jobs:
|
||||||
snapcraft:
|
snapcraft:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/download-artifact@v7
|
- uses: actions/download-artifact@v8
|
||||||
with:
|
with:
|
||||||
merge-multiple: true
|
merge-multiple: true
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,9 @@ impl File {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn is_hovered(&self) -> bool { self.idx == self.folder.cursor }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserData for File {
|
impl UserData for File {
|
||||||
|
|
@ -73,7 +76,7 @@ impl UserData for File {
|
||||||
cached_field!(fields, bare, |_, me| Ok(yazi_binding::File::new(&**me)));
|
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("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_current", |_, me| Ok(ptr::eq(&*me.folder, &me.tab.current)));
|
||||||
fields.add_field_method_get("in_preview", |_, me| {
|
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))
|
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<M: UserDataMethods<Self>>(methods: &mut M) {
|
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||||
yazi_binding::impl_file_methods!(methods);
|
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, ()| {
|
methods.add_method("size", |_, me, ()| {
|
||||||
Ok(if me.is_dir() { me.folder.files.sizes.get(&me.urn()).copied() } else { Some(me.len) })
|
Ok(if me.is_dir() { me.folder.files.sizes.get(&me.urn()).copied() } else { Some(me.len) })
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -90,5 +90,11 @@ impl UserData for File {
|
||||||
|
|
||||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||||
impl_file_methods!(methods);
|
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))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -267,11 +267,5 @@ macro_rules! impl_file_methods {
|
||||||
use yazi_fs::FsHash64;
|
use yazi_fs::FsHash64;
|
||||||
Ok(me.hash_u64())
|
Ok(me.hash_u64())
|
||||||
});
|
});
|
||||||
|
|
||||||
$methods.add_method("icon", |_, me, hovered: Option<bool>| {
|
|
||||||
use $crate::Icon;
|
|
||||||
// TODO: use a cache
|
|
||||||
Ok(yazi_config::THEME.icon.matches(me, hovered.unwrap_or(false)).map(Icon::from))
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,6 @@ pub fn deserialize_over1(input: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl #ident {
|
impl #ident {
|
||||||
#[inline]
|
|
||||||
pub(crate) fn deserialize_over_with<'de>(mut self, table: toml::Spanned<toml::de::DeTable<'de>>) -> Result<Self, toml::de::Error> {
|
pub(crate) fn deserialize_over_with<'de>(mut self, table: toml::Spanned<toml::de::DeTable<'de>>) -> Result<Self, toml::de::Error> {
|
||||||
use serde::{Deserialize, de::IntoDeserializer};
|
use serde::{Deserialize, de::IntoDeserializer};
|
||||||
|
|
||||||
|
|
@ -96,7 +95,6 @@ pub fn deserialize_over2(input: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl #ident {
|
impl #ident {
|
||||||
#[inline]
|
|
||||||
pub(crate) fn deserialize_over_with<'de>(mut self, table: toml::Spanned<toml::de::DeTable<'de>>) -> Result<Self, toml::de::Error> {
|
pub(crate) fn deserialize_over_with<'de>(mut self, table: toml::Spanned<toml::de::DeTable<'de>>) -> Result<Self, toml::de::Error> {
|
||||||
use serde::{Deserialize, de::IntoDeserializer};
|
use serde::{Deserialize, de::IntoDeserializer};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -997,7 +997,7 @@ conds = [
|
||||||
{ if = "dummy", text = "", fg = "#f44336" },
|
{ if = "dummy", text = "", fg = "#f44336" },
|
||||||
|
|
||||||
# Fallback
|
# Fallback
|
||||||
{ if = "dir & hovered", text = "" },
|
{ if = "dir & hovered", text = "", fg = "#03a9f4" },
|
||||||
{ if = "dir", text = "", fg = "#03a9f4" },
|
{ if = "dir", text = "", fg = "#03a9f4" },
|
||||||
{ if = "exec", text = "", fg = "#8bc34a" },
|
{ if = "exec", text = "", fg = "#8bc34a" },
|
||||||
{ if = "!dir", text = "", fg = "#ffffff" },
|
{ if = "!dir", text = "", fg = "#ffffff" },
|
||||||
|
|
|
||||||
|
|
@ -997,6 +997,7 @@ conds = [
|
||||||
{ if = "dummy", text = "", fg = "#f44336" },
|
{ if = "dummy", text = "", fg = "#f44336" },
|
||||||
|
|
||||||
# Fallback
|
# Fallback
|
||||||
|
{ if = "dir & hovered", text = "", fg = "#03a9f4" },
|
||||||
{ if = "dir", text = "", fg = "#03a9f4" },
|
{ if = "dir", text = "", fg = "#03a9f4" },
|
||||||
{ if = "exec", text = "", fg = "#8bc34a" },
|
{ if = "exec", text = "", fg = "#8bc34a" },
|
||||||
{ if = "!dir", text = "", fg = "#000000" },
|
{ if = "!dir", text = "", fg = "#000000" },
|
||||||
|
|
|
||||||
|
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ function Entity:padding()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:icon()
|
function Entity:icon()
|
||||||
local icon = self._file:icon(self._file.is_hovered)
|
local icon = self._file:icon()
|
||||||
if not icon then
|
if not icon then
|
||||||
return ""
|
return ""
|
||||||
elseif self._file.is_hovered then
|
elseif self._file.is_hovered then
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue