Skip to content

Commit

Permalink
Add theme
Browse files Browse the repository at this point in the history
  • Loading branch information
domsleee committed Sep 25, 2023
1 parent 60a2ebc commit cc5f026
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub enum Elem {
Acl,
Context,

/// Attributes
Archive,
AttributeRead,
Hidden,
System,

/// Last Time Modified
DayOld,
HourOld,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/meta/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 14 additions & 9 deletions src/meta/windows_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand All @@ -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),
},
]
Expand Down
27 changes: 27 additions & 0 deletions src/theme/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit cc5f026

Please sign in to comment.