Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yaml based theme support #452

Merged
merged 19 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
718d611
config: :hammer: using target enum and vec string for config options
zwpaper Nov 21, 2020
6ef0db1
log: :doc: unique error output and update comment to fit config
zwpaper Nov 28, 2020
83b3d75
color: :sparkles: add parse theme file
zwpaper Nov 25, 2020
00a6b3d
theme/test: :mag: :hammer: update tests to fit theme update
zwpaper Nov 29, 2020
af15854
theme/test: :mag: :sparkles: add tests for theme
zwpaper Nov 29, 2020
a3a45e5
theme: :hammer: using default to return dark theme and more tests
zwpaper Dec 5, 2020
9ed80be
theme: :art: :memo: update readme, change log, error log
zwpaper Dec 6, 2020
49f58d1
:sparkles: done use crossterm to impl theme, functionally works
zwpaper Jul 6, 2021
24d38d5
:mag: :hammer: update tests to fit crossterm color
zwpaper Jul 7, 2021
30d6bf2
:memo: :hammer: update readme to corssterm color
zwpaper Jul 7, 2021
519c878
:fire: get rid of ansi term in all code
zwpaper Jul 7, 2021
47a7714
build: :arrow_up: update dependency to deserialize color
zwpaper Aug 24, 2021
d260cb1
theme: :hammer: use `themes` dir for themes configurations
zwpaper Aug 31, 2021
03f406d
config: :hammer: user do not have to config yaml extension for theme
zwpaper Aug 31, 2021
69a4f28
:memo: add filename coloring explanation between LS_COLOR and theme
zwpaper Sep 9, 2021
f1cde2b
theme: :hammer: not deserialize no color and no lscolors from config
zwpaper Sep 9, 2021
e992756
:hammer: use lscolors for filename only, skip from theme configure
zwpaper Sep 12, 2021
9377613
theme: :hammer: use dark color for default theme
zwpaper Sep 13, 2021
bb6e7f9
theme: :hammer: try yml if yaml theme file not found
zwpaper Sep 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
theme: 🎨 📝 update readme, change log, error log
Signed-off-by: zwPapEr <zw.paper@gmail.com>
  • Loading branch information
zwpaper committed Aug 31, 2021
commit 9ed80bedff1f579cbac9f1740d2c9cb3b87599ff
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate
### Added
- update minimal rust version to 1.42.0 from [zwpaper](https://github.com/zwpaper) [#534](https://github.com/Peltoche/lsd/issues/534)
- Add support for theme from [zwpaper](https://github.com/zwpaper) [#452](https://github.com/Peltoche/lsd/pull/452)
- Update minimal rust version to 1.42.0 from [zwpaper](https://github.com/zwpaper) [#534](https://github.com/Peltoche/lsd/issues/534)
- [`NO_COLOR`](https://no-color.org/) environment variable support from [AnInternetTroll](https://github.com/aninternettroll)
### Changed
- Change size to use btyes in classic mode from [meain](https://github.com/meain)
Expand Down
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ color:
# When "classic" is set, this is set to "never".
# Possible values: never, auto, always
when: auto
# How to colorize the output.
# When "classic" is set, this is set to "no-color".
# Possible values: default, no-color, no-lscolors, <theme-file-name>
meain marked this conversation as resolved.
Show resolved Hide resolved
# when specifying <theme-file-name>, lsd will look up theme file in
# XDG Base Directory if relative
# The file path if absolute
theme: default

# == Date ==
# This specifies the date format for the date column. The freeform format
Expand Down Expand Up @@ -304,6 +311,100 @@ total-size: false
symlink-arrow: ⇒
```

## Theme

`lsd` can be configured with a theme file to set the colors.

Theme can be configured in the [configuration file](#configuration)(color.theme),
The valid theme configurations are:
- `default`: the default color scheme shipped in `lsd`
- `no-color`: classic mode, there will be just black and white.
zwpaper marked this conversation as resolved.
Show resolved Hide resolved
- `no-lscolors`: do not check the `LSCOLORS` env
zwpaper marked this conversation as resolved.
Show resolved Hide resolved
- theme-file-name(yaml): use the theme file to specify colors

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
meain marked this conversation as resolved.
Show resolved Hide resolved
- absolute name: use the file path and name to find theme file
meain marked this conversation as resolved.
Show resolved Hide resolved

Check [Theme file content](#theme-file-content) for details.

### Theme file content

Theme file use the [ansi_term](https://docs.rs/ansi_term)
configure the colors, check [ansi_term](https://docs.rs/ansi_term/0.12.1/ansi_term/enum.Colour.html)
for the supported colors.

Color table: https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg

This is the default theme scheme shipped with `lsd`.

```yaml
user:
Fixed: 230
zwpaper marked this conversation as resolved.
Show resolved Hide resolved
group:
Fixed: 187
permissions:
read: Green
write: Yellow
exec: Red
exec-sticky: Purple
no-access:
Fixed: 245
file-type:
zwpaper marked this conversation as resolved.
Show resolved Hide resolved
file:
exec-uid:
Fixed: 40
uid-no-exec:
Fixed: 184
exec-no-uid:
Fixed: 40
no-exec-no-uid:
Fixed: 184
dir:
uid:
Fixed: 33
no-uid:
Fixed: 33
pipe:
Fixed: 44
symlink:
default:
Fixed: 44
broken:
Fixed: 124
block-device:
Fixed: 44
char-device:
Fixed: 172
socket:
Fixed: 44
special:
Fixed: 44
modified:
hour-old:
Fixed: 40
day-old:
Fixed: 42
older:
Fixed: 36
size:
none:
Fixed: 245
small:
Fixed: 229
medium:
Fixed: 216
large:
Fixed: 172
inode:
valid:
Fixed: 13
invalid:
Fixed: 245
```


## External Configurations

### Required
Expand Down
23 changes: 12 additions & 11 deletions src/color/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Theme {
let real = if let Some(path) = config_file::Config::expand_home(file) {
path
} else {
print_error!("Bad theme file path: {}.", &file);
print_error!("Not a valid theme file path: {}.", &file);
return None;
};
let path = if Path::new(&real).is_absolute() {
Expand All @@ -125,24 +125,25 @@ impl Theme {
config_file::Config::config_file_path().unwrap().join(real)
};
match fs::read(&path) {
Ok(f) => Self::with_yaml(&String::from_utf8_lossy(&f)),
Ok(f) => match Self::with_yaml(&String::from_utf8_lossy(&f)) {
Ok(t) => Some(t),
Err(e) => {
print_error!("Theme file {} format error: {}.", &file, e);
None
}
},
Err(e) => {
print_error!("bad theme file: {}, {}\n", path.to_string_lossy(), e);
print_error!("Not a valid theme: {}, {}.", path.to_string_lossy(), e);
None
}
}
}

/// This constructs a Theme struct with a passed [Yaml] str.
fn with_yaml(yaml: &str) -> Option<Self> {
match serde_yaml::from_str::<Self>(yaml) {
Ok(c) => Some(c),
Err(e) => {
print_error!("theme file format error, {}\n\n", e);
None
}
}
fn with_yaml(yaml: &str) -> Result<Self, serde_yaml::Error> {
serde_yaml::from_str::<Self>(yaml)
}

pub fn default_dark() -> Self {
Theme {
user: Colour::Fixed(230), // Cornsilk1
Expand Down
6 changes: 6 additions & 0 deletions src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ color:
# When "classic" is set, this is set to "never".
# Possible values: never, auto, always
when: auto
# How to colorize the output.
# When "classic" is set, this is set to "no-color".
# Possible values: default, no-color, no-lscolors, <theme-file-name>
# when specifying <theme-file-name>, lsd will look up theme file in
# XDG Base Directory if relative
# The file path if absolute
theme: default

# == Date ==
Expand Down