mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: add FiletypeIs to icons
This commit is contained in:
parent
aa8448677e
commit
89d73954fd
2 changed files with 70 additions and 1 deletions
|
|
@ -1,14 +1,40 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use anyhow::bail;
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
|
use yazi_shared::fs::File;
|
||||||
|
|
||||||
use super::Style;
|
use super::Style;
|
||||||
use crate::{preset::Preset, theme::{Color, StyleShadow}, Pattern};
|
use crate::{preset::Preset, theme::{Color, StyleShadow}, Pattern};
|
||||||
|
|
||||||
pub struct Icon {
|
pub struct Icon {
|
||||||
|
pub is: FiletypeIs,
|
||||||
pub name: Pattern,
|
pub name: Pattern,
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub style: Style,
|
pub style: Style,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Icon {
|
||||||
|
pub fn matches(&self, file: &File) -> bool {
|
||||||
|
let b = match self.is {
|
||||||
|
FiletypeIs::None => true,
|
||||||
|
FiletypeIs::Block => file.cha.is_block_device(),
|
||||||
|
FiletypeIs::Char => file.cha.is_char_device(),
|
||||||
|
FiletypeIs::Exec => file.cha.is_exec(),
|
||||||
|
FiletypeIs::Fifo => file.cha.is_fifo(),
|
||||||
|
FiletypeIs::Link => file.cha.is_link(),
|
||||||
|
FiletypeIs::Orphan => file.cha.is_orphan(),
|
||||||
|
FiletypeIs::Sock => file.cha.is_socket(),
|
||||||
|
FiletypeIs::Sticky => file.cha.is_sticky(),
|
||||||
|
};
|
||||||
|
if !b {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.name.match_path(&file.url, file.is_dir())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Icon {
|
impl Icon {
|
||||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Icon>, D::Error>
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Icon>, D::Error>
|
||||||
where
|
where
|
||||||
|
|
@ -24,6 +50,8 @@ impl Icon {
|
||||||
}
|
}
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct IconRule {
|
struct IconRule {
|
||||||
|
#[serde(default)]
|
||||||
|
is: FiletypeIs,
|
||||||
name: Pattern,
|
name: Pattern,
|
||||||
text: String,
|
text: String,
|
||||||
|
|
||||||
|
|
@ -45,6 +73,7 @@ impl Icon {
|
||||||
.rules
|
.rules
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|r| Icon {
|
.map(|r| Icon {
|
||||||
|
is: r.is,
|
||||||
name: r.name,
|
name: r.name,
|
||||||
text: r.text,
|
text: r.text,
|
||||||
style: StyleShadow { fg: r.fg, ..Default::default() }.into(),
|
style: StyleShadow { fg: r.fg, ..Default::default() }.into(),
|
||||||
|
|
@ -53,3 +82,43 @@ impl Icon {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- FiletypeIs
|
||||||
|
#[derive(Default, Deserialize)]
|
||||||
|
#[serde(try_from = "String")]
|
||||||
|
pub enum FiletypeIs {
|
||||||
|
#[default]
|
||||||
|
None,
|
||||||
|
Block,
|
||||||
|
Char,
|
||||||
|
Exec,
|
||||||
|
Fifo,
|
||||||
|
Link,
|
||||||
|
Orphan,
|
||||||
|
Sock,
|
||||||
|
Sticky,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for FiletypeIs {
|
||||||
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Ok(match s {
|
||||||
|
"block" => Self::Block,
|
||||||
|
"char" => Self::Char,
|
||||||
|
"exec" => Self::Exec,
|
||||||
|
"fifo" => Self::Fifo,
|
||||||
|
"link" => Self::Link,
|
||||||
|
"orphan" => Self::Orphan,
|
||||||
|
"sock" => Self::Sock,
|
||||||
|
"sticky" => Self::Sticky,
|
||||||
|
_ => bail!("invalid filetype: {s}"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<String> for FiletypeIs {
|
||||||
|
type Error = anyhow::Error;
|
||||||
|
|
||||||
|
fn try_from(s: String) -> Result<Self, Self::Error> { Self::from_str(&s) }
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ impl File {
|
||||||
THEME
|
THEME
|
||||||
.icons
|
.icons
|
||||||
.iter()
|
.iter()
|
||||||
.find(|&x| x.name.match_path(&me.url, me.is_dir()))
|
.find(|&x| x.matches(me))
|
||||||
.map(|x| Icon::cast(lua, x))
|
.map(|x| Icon::cast(lua, x))
|
||||||
.transpose()
|
.transpose()
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue