Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
r3dArch committed Oct 14, 2022
1 parent 385501e commit 97cd83e
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 78 deletions.
57 changes: 26 additions & 31 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crossterm::style::Color;
mod theme;

use crossterm::style::{Attribute, ContentStyle, StyledContent, Stylize};
use lscolors::{Indicator, LsColors};
use std::path::Path;
use theme::Theme;

pub use crate::flags::color::ThemeOption;
use crate::theme::{color::ColorTheme, Theme};

use crossterm::style::Color;
use lscolors::{Indicator, LsColors};
use std::path::Path;

#[allow(dead_code)]
#[derive(Hash, Debug, Eq, PartialEq, Clone)]
Expand Down Expand Up @@ -68,7 +71,7 @@ impl Elem {
matches!(self, Elem::Dir { uid: true } | Elem::File { uid: true, .. })
}

pub fn get_color(&self, theme: &ColorTheme) -> Color {
fn get_color(&self, theme: &theme::Theme) -> Color {
match self {
Elem::File {
exec: true,
Expand Down Expand Up @@ -128,24 +131,16 @@ impl Elem {
pub type ColoredString = StyledContent<String>;

pub struct Colors {
theme: Option<ColorTheme>,
theme: Option<Theme>,
lscolors: Option<LsColors>,
}

impl Colors {
pub fn new(t: ThemeOption) -> Self {
let theme = match t {
ThemeOption::NoColor => None,
ThemeOption::Default | ThemeOption::NoLscolors => Some(Theme::default().color),
ThemeOption::Custom(ref file) => {
// TODO: drop the `themes` dir prefix, adding it here only for backwards compatibility
Some(
Theme::from_path::<ColorTheme>(
Path::new("themes").join(file).to_str().unwrap_or(file),
)
.unwrap_or_default(),
)
}
ThemeOption::Default | ThemeOption::NoLscolors => Some(Theme::default()),
ThemeOption::Custom(ref file) => Some(Theme::from_path(file).unwrap_or_default()),
};
let lscolors = match t {
ThemeOption::Default | ThemeOption::Custom(_) => {
Expand Down Expand Up @@ -306,8 +301,8 @@ fn to_content_style(ls: &lscolors::Style) -> ContentStyle {
#[cfg(test)]
mod tests {
use super::Colors;
use crate::color::Theme;
use crate::color::ThemeOption;
use crate::theme::color::ColorTheme;
#[test]
fn test_color_new_no_color_theme() {
assert!(Colors::new(ThemeOption::NoColor).theme.is_none());
Expand All @@ -317,31 +312,31 @@ mod tests {
fn test_color_new_default_theme() {
assert_eq!(
Colors::new(ThemeOption::Default).theme,
Some(ColorTheme::default_dark()),
Some(Theme::default_dark()),
);
}

#[test]
fn test_color_new_bad_custom_theme() {
assert_eq!(
Colors::new(ThemeOption::Custom("not-existed".to_string())).theme,
Some(ColorTheme::default_dark()),
Some(Theme::default_dark()),
);
}
}

#[cfg(test)]
mod elem {
use super::Elem;
use crate::theme::{color, color::ColorTheme};
use crate::color::{theme, Theme};
use crossterm::style::Color;

#[cfg(test)]
fn test_theme() -> ColorTheme {
ColorTheme {
fn test_theme() -> Theme {
Theme {
user: Color::AnsiValue(230), // Cornsilk1
group: Color::AnsiValue(187), // LightYellow3
permission: color::Permission {
permission: theme::Permission {
read: Color::Green,
write: Color::Yellow,
exec: Color::Red,
Expand All @@ -351,19 +346,19 @@ mod elem {
acl: Color::DarkCyan,
context: Color::Cyan,
},
file_type: color::FileType {
file: color::File {
file_type: theme::FileType {
file: theme::File {
exec_uid: Color::AnsiValue(40), // Green3
uid_no_exec: Color::AnsiValue(184), // Yellow3
exec_no_uid: Color::AnsiValue(40), // Green3
no_exec_no_uid: Color::AnsiValue(184), // Yellow3
},
dir: color::Dir {
dir: theme::Dir {
uid: Color::AnsiValue(33), // DodgerBlue1
no_uid: Color::AnsiValue(33), // DodgerBlue1
},
pipe: Color::AnsiValue(44), // DarkTurquoise
symlink: color::Symlink {
symlink: theme::Symlink {
default: Color::AnsiValue(44), // DarkTurquoise
broken: Color::AnsiValue(124), // Red3
missing_target: Color::AnsiValue(124), // Red3
Expand All @@ -373,22 +368,22 @@ mod elem {
socket: Color::AnsiValue(44), // DarkTurquoise
special: Color::AnsiValue(44), // DarkTurquoise
},
date: color::Date {
date: theme::Date {
hour_old: Color::AnsiValue(40), // Green3
day_old: Color::AnsiValue(42), // SpringGreen2
older: Color::AnsiValue(36), // DarkCyan
},
size: color::Size {
size: theme::Size {
none: Color::AnsiValue(245), // Grey
small: Color::AnsiValue(229), // Wheat1
medium: Color::AnsiValue(216), // LightSalmon1
large: Color::AnsiValue(172), // Orange3
},
inode: color::INode {
inode: theme::INode {
valid: Color::AnsiValue(13), // Pink
invalid: Color::AnsiValue(245), // Grey
},
links: color::Links {
links: theme::Links {
valid: Color::AnsiValue(13), // Pink
invalid: Color::AnsiValue(245), // Grey
},
Expand Down
16 changes: 11 additions & 5 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::color::Colors;
use crate::display;
use crate::flags::{ColorOption, Display, Flags, HyperlinkOption, Layout, SortOrder, ThemeOption};
use crate::icon::Icons;
use crate::flags::{
ColorOption, Display, Flags, HyperlinkOption, IconOption, IconTheme, Layout, SortOrder,
ThemeOption,
};
use crate::icon::{self, Icons};
use crate::meta::Meta;
use crate::{print_error, print_output, sort, ExitCode};
use std::path::PathBuf;
Expand Down Expand Up @@ -44,8 +47,11 @@ impl Core {
_ => flags.color.theme.clone(),
};

let icon_when = flags.icons.when;
let icon_theme = flags.icons.theme.clone();
let icon_theme = match (tty_available, flags.icons.when, flags.icons.theme) {
(_, IconOption::Never, _) | (false, IconOption::Auto, _) => icon::Theme::NoIcon,
(_, _, IconTheme::Fancy) => icon::Theme::Fancy,
(_, _, IconTheme::Unicode) => icon::Theme::Unicode,
};

// TODO: Rework this so that flags passed downstream does not
// have Auto option for any (icon, color, hyperlink).
Expand Down Expand Up @@ -74,7 +80,7 @@ impl Core {
Self {
flags,
colors: Colors::new(color_theme),
icons: Icons::new(tty_available, icon_when, icon_theme, icon_separator),
icons: Icons::new(icon_theme, icon_separator),
sorters,
}
}
Expand Down
81 changes: 58 additions & 23 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ fn get_output(
tree: (usize, &str),
) -> Vec<String> {
let mut strings: Vec<String> = Vec::new();
let colorize_missing = |string: &str| colors.colorize(string, &Elem::NoAccess);

for (i, block) in flags.blocks.0.iter().enumerate() {
let mut block_vec = if Layout::Tree == flags.layout && tree.0 == i {
vec![colors.colorize(tree.1, &Elem::TreeEdge)]
Expand All @@ -297,28 +299,58 @@ fn get_output(
};

match block {
Block::INode => block_vec.push(meta.inode.render(colors)),
Block::Links => block_vec.push(meta.links.render(colors)),
Block::INode => block_vec.push(match &meta.inode {
Some(inode) => inode.render(colors),
None => colorize_missing("?"),
}),
Block::Links => block_vec.push(match &meta.links {
Some(links) => links.render(colors),
None => colorize_missing("?"),
}),
Block::Permission => {
block_vec.extend([
meta.file_type.render(colors),
meta.permissions.render(colors, flags),
meta.access_control.render_method(colors),
match meta.permissions {
Some(permissions) => permissions.render(colors, flags),
None => colorize_missing("?????????"),
},
match &meta.access_control {
Some(access_control) => access_control.render_method(colors),
None => colorize_missing(""),
},
]);
}
Block::User => block_vec.push(meta.owner.render_user(colors)),
Block::Group => block_vec.push(meta.owner.render_group(colors)),
Block::Context => block_vec.push(meta.access_control.render_context(colors)),
Block::User => block_vec.push(match &meta.owner {
Some(owner) => owner.render_user(colors),
None => colorize_missing("?"),
}),
Block::Group => block_vec.push(match &meta.owner {
Some(owner) => owner.render_group(colors),
None => colorize_missing("?"),
}),
Block::Context => block_vec.push(match &meta.access_control {
Some(access_control) => access_control.render_context(colors),
None => colorize_missing("?"),
}),
Block::Size => {
let pad = if Layout::Tree == flags.layout && 0 == tree.0 && 0 == i {
None
} else {
Some(padding_rules[&Block::SizeValue])
};
block_vec.push(meta.size.render(colors, flags, pad))
block_vec.push(match &meta.size {
Some(size) => size.render(colors, flags, pad),
None => colorize_missing("?"),
})
}
Block::SizeValue => block_vec.push(meta.size.render_value(colors, flags)),
Block::Date => block_vec.push(meta.date.render(colors, flags)),
Block::SizeValue => block_vec.push(match &meta.size {
Some(size) => size.render_value(colors, flags),
None => colorize_missing("?"),
}),
Block::Date => block_vec.push(match &meta.date {
Some(date) => date.render(colors, flags),
None => colorize_missing("?"),
}),
Block::Name => {
block_vec.extend([
meta.name.render(
Expand Down Expand Up @@ -377,7 +409,10 @@ fn detect_size_lengths(metas: &[Meta], flags: &Flags) -> usize {
let mut max_value_length: usize = 0;

for meta in metas {
let value_len = meta.size.value_string(flags).len();
let value_len = match &meta.size {
Some(size) => size.value_string(flags).len(),
None => 0,
};

if value_len > max_value_length {
max_value_length = value_len;
Expand Down Expand Up @@ -413,11 +448,11 @@ mod tests {
use super::*;
use crate::color;
use crate::color::Colors;
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme};
use crate::flags::HyperlinkOption;
use crate::icon::Icons;
use crate::meta::{FileType, Name};
use crate::Config;
use crate::{app, flags, sort};
use crate::{app, flags, icon, sort};
use assert_fs::prelude::*;
use std::path::Path;

Expand All @@ -443,7 +478,7 @@ mod tests {
let output = name
.render(
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
Expand Down Expand Up @@ -478,7 +513,7 @@ mod tests {
let output = name
.render(
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::Fancy, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
Expand Down Expand Up @@ -512,7 +547,7 @@ mod tests {
let output = name
.render(
&Colors::new(color::ThemeOption::NoLscolors),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
Expand Down Expand Up @@ -554,7 +589,7 @@ mod tests {
let output = name
.render(
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&DisplayOption::FileName,
HyperlinkOption::Never,
true,
Expand Down Expand Up @@ -618,7 +653,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
);

assert_eq!("one.d\n├── .hidden\n└── two\n", output);
Expand Down Expand Up @@ -649,7 +684,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
);

let length_before_b = |i| -> usize {
Expand Down Expand Up @@ -689,7 +724,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
);

assert_eq!(output.lines().nth(1).unwrap().chars().next().unwrap(), '└');
Expand Down Expand Up @@ -728,7 +763,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
);

assert!(output.ends_with("└── two\n"));
Expand Down Expand Up @@ -758,7 +793,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
);

dir.close().unwrap();
Expand Down Expand Up @@ -791,7 +826,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
);

dir.close().unwrap();
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ mod flags;
mod icon;
mod meta;
mod sort;
mod theme;

use crate::config_file::Config;
use crate::core::Core;
Expand Down
Loading

0 comments on commit 97cd83e

Please sign in to comment.