Skip to content

Commit

Permalink
refactor: use macros to implement getters for LsColors and ThemeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
bydlw98 committed Apr 27, 2024
1 parent bcf1605 commit 67344f8
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 99 deletions.
114 changes: 67 additions & 47 deletions src/ls_colors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
use std::collections::hash_map::HashMap;

macro_rules! ls_colors_get_style_impl {
($field:ident, $method:ident, $comment:literal) => {
#[doc = concat!("Returns the style used for ", $comment)]
#[inline]
pub fn $method(&self) -> Option<&str> {
self.$field.as_deref()
}
};
}

#[derive(Debug)]
pub struct LsColors {
file: Option<String>,
Expand All @@ -23,7 +33,7 @@ pub struct LsColors {
#[cfg(unix)]
dir_sticky_and_other_writable: Option<String>,
#[cfg(unix)]
dir_other_writeable: Option<String>,
dir_other_writable: Option<String>,
#[cfg(unix)]
dir_sticky: Option<String>,
extension: HashMap<String, String>,
Expand Down Expand Up @@ -98,7 +108,7 @@ impl LsColors {
}
#[cfg(unix)]
"ow" => {
self.dir_other_writeable = Some(v.to_string());
self.dir_other_writable = Some(v.to_string());
}
#[cfg(unix)]
"st" => {
Expand All @@ -115,58 +125,68 @@ impl LsColors {
}
}

pub fn file_style(&self) -> Option<&str> {
self.file.as_deref()
}
pub fn dir_style(&self) -> Option<&str> {
self.dir.as_deref()
}
pub fn symlink_style(&self) -> Option<&str> {
self.symlink.as_deref()
}
pub fn exec_style(&self) -> Option<&str> {
self.exec.as_deref()
}
ls_colors_get_style_impl!(file, file_style, "regular files.");

ls_colors_get_style_impl!(dir, dir_style, "directories.");

ls_colors_get_style_impl!(symlink, symlink_style, "symbolic links.");

ls_colors_get_style_impl!(exec, exec_style, "executables.");

#[cfg(unix)]
pub fn block_device_style(&self) -> Option<&str> {
self.block_device.as_deref()
}
ls_colors_get_style_impl!(block_device, block_device_style, "block devices.");

#[cfg(unix)]
pub fn char_device_style(&self) -> Option<&str> {
self.char_device.as_deref()
}
ls_colors_get_style_impl!(char_device, char_device_style, "char devices.");

#[cfg(unix)]
pub fn fifo_style(&self) -> Option<&str> {
self.fifo.as_deref()
}
ls_colors_get_style_impl!(fifo, fifo_style, "fifos.");

#[cfg(unix)]
pub fn socket_style(&self) -> Option<&str> {
self.socket.as_deref()
}
ls_colors_get_style_impl!(socket, socket_style, "socket file type.");

#[cfg(unix)]
pub fn setuid_style(&self) -> Option<&str> {
self.setuid.as_deref()
}
ls_colors_get_style_impl!(
setuid,
setuid_style,
"regular files with setuid file permission."
);

#[cfg(unix)]
pub fn setgid_style(&self) -> Option<&str> {
self.setgid.as_deref()
}
ls_colors_get_style_impl!(
setgid,
setgid_style,
"regular files with setgid file permission."
);

#[cfg(unix)]
pub fn multiple_hard_links_style(&self) -> Option<&str> {
self.multiple_hard_links.as_deref()
}
ls_colors_get_style_impl!(
multiple_hard_links,
multiple_hard_links_style,
"files with multiple hard links."
);

#[cfg(unix)]
pub fn dir_sticky_and_other_writeable_style(&self) -> Option<&str> {
self.dir_sticky_and_other_writable.as_deref()
}
ls_colors_get_style_impl!(
dir_sticky_and_other_writable,
dir_sticky_and_other_writable_style,
"directories with sticky and other writable permissions."
);

#[cfg(unix)]
pub fn dir_other_writeable_style(&self) -> Option<&str> {
self.dir_other_writeable.as_deref()
}
ls_colors_get_style_impl!(
dir_other_writable,
dir_other_writable_style,
"directories with other writable permission."
);

#[cfg(unix)]
pub fn dir_sticky_style(&self) -> Option<&str> {
self.dir_sticky.as_deref()
}
ls_colors_get_style_impl!(
dir_sticky,
dir_sticky_style,
"directories with sticky permission."
);

pub fn extension_style(&self, extension: String) -> Option<&str> {
if self.extension.is_empty() {
self.file_style()
Expand Down Expand Up @@ -203,7 +223,7 @@ impl Default for LsColors {
#[cfg(unix)]
dir_sticky_and_other_writable: None,
#[cfg(unix)]
dir_other_writeable: None,
dir_other_writable: None,
#[cfg(unix)]
dir_sticky: None,
extension: HashMap::with_capacity(128),
Expand Down Expand Up @@ -307,12 +327,12 @@ mod test {

#[cfg(unix)]
assert_eq!(
ls_colors.dir_sticky_and_other_writeable_style(),
ls_colors.dir_sticky_and_other_writable_style(),
Some("30;42")
);

#[cfg(unix)]
assert_eq!(ls_colors.dir_other_writeable_style(), Some("34;42"));
assert_eq!(ls_colors.dir_other_writable_style(), Some("34;42"));

#[cfg(unix)]
assert_eq!(ls_colors.dir_sticky_style(), Some("37;44"));
Expand Down
4 changes: 2 additions & 2 deletions src/output/format_filename/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ fn internal_format_dir(file_name: &str, _metadata: &Metadata, config: &Config) -
) {
(false, false) => create_filename_cell(file_name, ls_colors.dir_style(), icon),
(true, false) => create_filename_cell(file_name, ls_colors.dir_sticky_style(), icon),
(false, true) => create_filename_cell(file_name, ls_colors.dir_other_writeable_style(), icon),
(false, true) => create_filename_cell(file_name, ls_colors.dir_other_writable_style(), icon),
_ => create_filename_cell(
file_name,
ls_colors.dir_sticky_and_other_writeable_style(), icon
ls_colors.dir_sticky_and_other_writable_style(), icon
),
};
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/output/format_filename/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ fn test_format_filename_sticky_dir() {
#[test]
fn test_format_filename_other_writeable_dir() {
let ls_colors = LsColors::with_colors();
let ansi_style_str = ls_colors.dir_other_writeable_style();
let ansi_style_str = ls_colors.dir_other_writable_style();
let mode: Option<u32> = Some(0o755 | c::S_IWOTH);

internal_test_format_filename_dir(mode, IndicatorStyle::Never, false, ansi_style_str);
Expand All @@ -853,7 +853,7 @@ fn test_format_filename_other_writeable_dir() {
#[test]
fn test_format_filename_sticky_and_other_writeable_dir() {
let ls_colors = LsColors::with_colors();
let ansi_style_str = ls_colors.dir_sticky_and_other_writeable_style();
let ansi_style_str = ls_colors.dir_sticky_and_other_writable_style();
let mode: Option<u32> = Some(0o755 | c::S_ISVTX | c::S_IWOTH);

internal_test_format_filename_dir(mode, IndicatorStyle::Never, false, ansi_style_str);
Expand Down
90 changes: 42 additions & 48 deletions src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
macro_rules! theme_config_get_style_impl {
($field:ident, $method:ident, $comment:literal) => {
#[doc = concat!("Returns the style used for ", stringify!($comment))]
#[inline]
pub fn $method(&self) -> Option<&str> {
self.$field.as_deref()
}
};
}

#[derive(Debug, Default)]
pub struct ThemeConfig {
inode: Option<String>,
Expand All @@ -24,59 +34,43 @@ pub struct ThemeConfig {
}

impl ThemeConfig {
pub fn inode_style(&self) -> Option<&str> {
self.inode.as_deref()
}
pub fn nlink_style(&self) -> Option<&str> {
self.nlink.as_deref()
}
pub fn owner_style(&self) -> Option<&str> {
self.owner.as_deref()
}
pub fn group_style(&self) -> Option<&str> {
self.group.as_deref()
}
pub fn size_style(&self) -> Option<&str> {
self.size.as_deref()
}
pub fn timestamp_style(&self) -> Option<&str> {
self.timestamp.as_deref()
}
pub fn read_style(&self) -> Option<&str> {
self.read.as_deref()
}
pub fn write_style(&self) -> Option<&str> {
self.write.as_deref()
}
pub fn execute_style(&self) -> Option<&str> {
self.execute.as_deref()
}
pub fn no_permission_style(&self) -> Option<&str> {
self.no_permission.as_deref()
}
theme_config_get_style_impl!(inode, inode_style, "inode.");

theme_config_get_style_impl!(nlink, nlink_style, "nlink.");

theme_config_get_style_impl!(owner, owner_style, "owner.");

theme_config_get_style_impl!(group, group_style, "group.");

theme_config_get_style_impl!(size, size_style, "size.");

theme_config_get_style_impl!(timestamp, timestamp_style, "timestamp.");

theme_config_get_style_impl!(read, read_style, "read permission.");

theme_config_get_style_impl!(write, write_style, "write permission.");

theme_config_get_style_impl!(execute, execute_style, "execute permission.");

theme_config_get_style_impl!(no_permission, no_permission_style, "no permission.");

#[cfg(unix)]
pub fn setuid_style(&self) -> Option<&str> {
self.setuid.as_deref()
}
theme_config_get_style_impl!(setuid, setuid_style, "setuid permission.");

#[cfg(unix)]
pub fn setgid_style(&self) -> Option<&str> {
self.setgid.as_deref()
}
theme_config_get_style_impl!(setgid, setgid_style, "setgid permission.");

#[cfg(unix)]
pub fn sticky_style(&self) -> Option<&str> {
self.sticky.as_deref()
}
theme_config_get_style_impl!(sticky, sticky_style, "sticky permission.");

#[cfg(windows)]
pub fn archive_style(&self) -> Option<&str> {
self.archive.as_deref()
}
theme_config_get_style_impl!(archive, archive_style, "archive attribute.");

#[cfg(windows)]
pub fn system_style(&self) -> Option<&str> {
self.system.as_deref()
}
pub fn hidden_style(&self) -> Option<&str> {
self.hidden.as_deref()
}
theme_config_get_style_impl!(system, system_style, "system attribute.");

theme_config_get_style_impl!(hidden, hidden_style, "hidden attribute.");

pub fn with_default_colors() -> Self {
Self {
inode: Some(String::from("32;1")),
Expand Down

0 comments on commit 67344f8

Please sign in to comment.