Skip to content

Commit

Permalink
Added new custom option for color config, marked themes folder as d…
Browse files Browse the repository at this point in the history
…eprecated. (#851)
  • Loading branch information
peppidesu authored Jun 23, 2023
1 parent d97e7c4 commit 0e3f97b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Icon theme with overrides from config [sudame](https://github.com/sudame)
- Incorrect colorizing with `--size=bytes` [bells307](https://github.com/bells307)

### Changed
- Color theme is now expected to be in `$XDG/lsd/colors.yaml` by default from [peppidesu](https://github.com/peppidesu)
Legacy behaviour (`themes` folder) is marked as deprecated but is still supported.
[#749](https://github.com/lsd-rs/lsd/issues/749)

## [0.23.1] - 2022-09-13

### Fixed
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,8 @@ color:
when: auto
# How to colorize the output.
# When "classic" is set, this is set to "no-color".
# Possible values: default, <theme-file-name>
# when specifying <theme-file-name>, lsd will look up theme file
# XDG Base Directory if relative, e.g. ~/.config/lsd/themes/<theme-file-name>.yaml,
# The file path if absolute
# Possible values: default, custom
# When "custom" is set, lsd will look in the config directory for `colors.yaml`.
theme: default

# == Date ==
Expand Down Expand Up @@ -243,9 +241,13 @@ Color theme can be configured in the [configuration file](#configuration)(color.
The valid theme configurations are:

- `default`: the default color scheme shipped in `lsd`
- theme-file-name(yaml): use the theme file to specify colors(without the `yaml` extension)
- `custom`: use a custom color scheme defined in `colors.yaml`
- *(deprecated) theme_file_name(yaml): use the theme file to specify colors(without the `yaml` extension)*

when configured with the `theme-file-name` which is a `yaml` file,
When set to `custom`, `lsd` will look for `colors.yaml` in the
XDG Base Directory, e.g. ~/.config/lsd/colors.yaml

When configured with the `theme-file-name` which is a `yaml` file,
`lsd` will look up the theme file in the following way:

- relative name: check the XDG Base Directory, e.g. ~/.config/lsd/themes/<theme-file-name>.yaml
Expand Down
28 changes: 22 additions & 6 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::Path;

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

#[allow(dead_code)]
Expand Down Expand Up @@ -143,7 +144,14 @@ impl Colors {
let theme = match t {
ThemeOption::NoColor => None,
ThemeOption::Default | ThemeOption::NoLscolors => Some(Theme::default().color),
ThemeOption::Custom(ref file) => {
ThemeOption::Custom => Some(
Theme::from_path::<ColorTheme>(Path::new("colors").to_str().unwrap())
.unwrap_or_default(),
),
ThemeOption::CustomLegacy(ref file) => {
print_output!(
"Warning: the 'themes' directory is deprecated, use 'colors.yaml' instead."
);
// TODO: drop the `themes` dir prefix, adding it here only for backwards compatibility
Some(
Theme::from_path::<ColorTheme>(
Expand All @@ -154,7 +162,7 @@ impl Colors {
}
};
let lscolors = match t {
ThemeOption::Default | ThemeOption::Custom(_) => {
ThemeOption::Default | ThemeOption::Custom | ThemeOption::CustomLegacy(_) => {
Some(LsColors::from_env().unwrap_or_default())
}
_ => None,
Expand Down Expand Up @@ -316,17 +324,25 @@ mod tests {
}

#[test]
fn test_color_new_default_theme() {
fn test_color_new_custom_theme() {
assert_eq!(
Colors::new(ThemeOption::Custom).theme,
Some(ColorTheme::default_dark()),
);
}

#[test]
fn test_color_new_custom_no_file_theme() {
assert_eq!(
Colors::new(ThemeOption::Default).theme,
Colors::new(ThemeOption::Custom).theme,
Some(ColorTheme::default_dark()),
);
}

#[test]
fn test_color_new_bad_custom_theme() {
fn test_color_new_bad_legacy_custom_theme() {
assert_eq!(
Colors::new(ThemeOption::Custom("not-existed".to_string())).theme,
Colors::new(ThemeOption::CustomLegacy("not-existed".to_string())).theme,
Some(ColorTheme::default_dark()),
);
}
Expand Down
10 changes: 6 additions & 4 deletions src/flags/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub enum ThemeOption {
Default,
#[allow(dead_code)]
NoLscolors,
Custom(String),
CustomLegacy(String),
Custom,
}

impl ThemeOption {
Expand Down Expand Up @@ -77,7 +78,8 @@ impl<'de> de::Deserialize<'de> for ThemeOption {
{
match value {
"default" => Ok(ThemeOption::Default),
str => Ok(ThemeOption::Custom(str.to_string())),
"custom" => Ok(ThemeOption::Custom),
str => Ok(ThemeOption::CustomLegacy(str.to_string())),
}
}
}
Expand Down Expand Up @@ -301,10 +303,10 @@ mod test_theme_option {
let mut c = Config::with_none();
c.color = Some(config_file::Color {
when: None,
theme: Some(ThemeOption::Custom("not-existed".to_string())),
theme: Some(ThemeOption::CustomLegacy("not-existed".to_string())),
});
assert_eq!(
ThemeOption::Custom("not-existed".to_string()),
ThemeOption::CustomLegacy("not-existed".to_string()),
ThemeOption::from_config(&c)
);
}
Expand Down

0 comments on commit 0e3f97b

Please sign in to comment.