From cc5f026656c304dae1d61fd0e4811322c268d247 Mon Sep 17 00:00:00 2001 From: Dom Slee Date: Mon, 25 Sep 2023 22:23:11 +1000 Subject: [PATCH] Add theme --- src/color.rs | 17 +++++++++++++++++ src/meta/filetype.rs | 2 ++ src/meta/windows_attributes.rs | 23 ++++++++++++++--------- src/theme/color.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/color.rs b/src/color.rs index 8cb93c1ba..9a3a14ba2 100644 --- a/src/color.rs +++ b/src/color.rs @@ -38,6 +38,12 @@ pub enum Elem { Acl, Context, + /// Attributes + Archive, + AttributeRead, + Hidden, + System, + /// Last Time Modified DayOld, HourOld, @@ -112,6 +118,11 @@ impl Elem { Elem::Acl => theme.permission.acl, Elem::Context => theme.permission.context, + Elem::Archive => theme.attributes.archive, + Elem::AttributeRead => theme.attributes.read, + Elem::Hidden => theme.attributes.hidden, + Elem::System => theme.attributes.system, + Elem::DayOld => theme.date.day_old, Elem::HourOld => theme.date.hour_old, Elem::Older => theme.date.older, @@ -399,6 +410,12 @@ mod elem { acl: Color::DarkCyan, context: Color::Cyan, }, + attributes: color::Attributes { + read: Color::Green, + archive: Color::Yellow, + hidden: Color::Red, + system: Color::Magenta, + }, file_type: color::FileType { file: color::File { exec_uid: Color::AnsiValue(40), // Green3 diff --git a/src/meta/filetype.rs b/src/meta/filetype.rs index 23b60622a..173331d82 100644 --- a/src/meta/filetype.rs +++ b/src/meta/filetype.rs @@ -109,7 +109,9 @@ impl FileType { mod test { use super::FileType; use crate::color::{Colors, ThemeOption}; + #[cfg(unix)] use crate::flags::PermissionFlag; + #[cfg(unix)] use crate::meta::permissions_or_attributes::PermissionsOrAttributes; #[cfg(unix)] use crate::meta::Permissions; diff --git a/src/meta/windows_attributes.rs b/src/meta/windows_attributes.rs index bdd7278f5..fa4a0319f 100644 --- a/src/meta/windows_attributes.rs +++ b/src/meta/windows_attributes.rs @@ -17,15 +17,20 @@ pub struct WindowsAttributes { #[cfg(windows)] pub fn get_attributes(metadata: &std::fs::Metadata) -> WindowsAttributes { + use windows::Win32::Storage::FileSystem::{ + FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_READONLY, + FILE_ATTRIBUTE_SYSTEM, FILE_FLAGS_AND_ATTRIBUTES, + }; + let bits = metadata.file_attributes(); - let has_bit = |bit| bits & bit == bit; + let has_bit = |bit: FILE_FLAGS_AND_ATTRIBUTES| bits & bit.0 == bit.0; // https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants WindowsAttributes { - archive: has_bit(0x20), - readonly: has_bit(0x1), - hidden: has_bit(0x2), - system: has_bit(0x4), + archive: has_bit(FILE_ATTRIBUTE_ARCHIVE), + readonly: has_bit(FILE_ATTRIBUTE_READONLY), + hidden: has_bit(FILE_ATTRIBUTE_HIDDEN), + system: has_bit(FILE_ATTRIBUTE_SYSTEM), } } @@ -34,19 +39,19 @@ impl WindowsAttributes { pub fn render(&self, colors: &Colors, _flags: &Flags) -> ColoredString { let res = [ match self.archive { - true => colors.colorize("a", &Elem::Acl), + true => colors.colorize("a", &Elem::Archive), false => colors.colorize('-', &Elem::NoAccess), }, match self.readonly { - true => colors.colorize("r", &Elem::Read), + true => colors.colorize("r", &Elem::AttributeRead), false => colors.colorize('-', &Elem::NoAccess), }, match self.hidden { - true => colors.colorize("h", &Elem::NonFile), + true => colors.colorize("h", &Elem::Hidden), false => colors.colorize('-', &Elem::NoAccess), }, match self.system { - true => colors.colorize("s", &Elem::NonFile), + true => colors.colorize("s", &Elem::System), false => colors.colorize('-', &Elem::NoAccess), }, ] diff --git a/src/theme/color.rs b/src/theme/color.rs index 37afca276..dd9d39d9f 100644 --- a/src/theme/color.rs +++ b/src/theme/color.rs @@ -89,6 +89,7 @@ pub struct ColorTheme { #[serde(deserialize_with = "deserialize_color")] pub group: Color, pub permission: Permission, + pub attributes: Attributes, pub date: Date, pub size: Size, pub inode: INode, @@ -124,6 +125,21 @@ pub struct Permission { pub context: Color, } +#[derive(Debug, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "kebab-case")] +#[serde(deny_unknown_fields)] +#[serde(default)] +pub struct Attributes { + #[serde(deserialize_with = "deserialize_color")] + pub archive: Color, + #[serde(deserialize_with = "deserialize_color")] + pub read: Color, + #[serde(deserialize_with = "deserialize_color")] + pub hidden: Color, + #[serde(deserialize_with = "deserialize_color")] + pub system: Color, +} + #[derive(Debug, Deserialize, PartialEq, Eq)] #[serde(rename_all = "kebab-case")] #[serde(deny_unknown_fields)] @@ -274,6 +290,16 @@ impl Default for Permission { } } } +impl Default for Attributes { + fn default() -> Self { + Attributes { + archive: Color::DarkGreen, + read: Color::DarkYellow, + hidden: Color::AnsiValue(13), // Pink, + system: Color::AnsiValue(13), // Pink, + } + } +} impl Default for FileType { fn default() -> Self { FileType { @@ -381,6 +407,7 @@ impl ColorTheme { user: Color::AnsiValue(230), // Cornsilk1 group: Color::AnsiValue(187), // LightYellow3 permission: Permission::default(), + attributes: Attributes::default(), file_type: FileType::default(), date: Date::default(), size: Size::default(),