Skip to content

Commit

Permalink
Add GitTheme dev (rebased on official/master d2538a6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpwxf committed Dec 26, 2023
1 parent d2538a6 commit c13e74c
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@ truncate-owner:
after:
# String to be appended to a name if truncated.
marker: ""

# == Git ==
git:
# Whether to display git status
# Possible values: false, true
enabled: false
# How to display git status
# When "classic" is set, this is set to "default".
# 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
theme: default
```
</details>
Expand Down
16 changes: 14 additions & 2 deletions src/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::flags::layout::Layout;
use crate::flags::permission::PermissionFlag;
use crate::flags::size::SizeFlag;
use crate::flags::sorting::{DirGrouping, SortColumn};
use crate::flags::HyperlinkOption;
use crate::flags::{ColorOption, ThemeOption};
use crate::flags::{GitTheme, HyperlinkOption};
use crate::print_error;

use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -47,6 +47,7 @@ pub struct Config {
pub header: Option<bool>,
pub literal: Option<bool>,
pub truncate_owner: Option<TruncateOwner>,
pub git_theme: Option<GitTheme>,
}

#[derive(Eq, PartialEq, Debug, Deserialize)]
Expand Down Expand Up @@ -107,6 +108,7 @@ impl Config {
header: None,
literal: None,
truncate_owner: None,
git_theme: None,
}
}

Expand Down Expand Up @@ -347,6 +349,15 @@ truncate-owner:
after:
# String to be appended to a name if truncated.
marker: ""
# == Git ==
# How to display git status
# When "classic" is set, this is set to "default".
# 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
git-theme: default
"#;

#[cfg(test)]
Expand All @@ -366,7 +377,7 @@ mod tests {
use crate::flags::permission::PermissionFlag;
use crate::flags::size::SizeFlag;
use crate::flags::sorting::{DirGrouping, SortColumn};
use crate::flags::HyperlinkOption;
use crate::flags::{GitTheme, HyperlinkOption};

#[test]
fn test_read_default() {
Expand Down Expand Up @@ -418,6 +429,7 @@ mod tests {
after: None,
marker: Some("".to_string()),
}),
git_theme: Some(GitTheme::Default)
},
c
);
Expand Down
3 changes: 2 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Core {

let icon_when = flags.icons.when;
let icon_theme = flags.icons.theme.clone();
let git_theme = flags.git_theme.clone();

// TODO: Rework this so that flags passed downstream does not
// have Auto option for any (icon, color, hyperlink).
Expand Down Expand Up @@ -83,7 +84,7 @@ impl Core {
flags,
colors: Colors::new(color_theme),
icons: Icons::new(tty_available, icon_when, icon_theme, icon_separator),
git_theme: GitTheme::new(),
git_theme: GitTheme::new(git_theme),
sorters,
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ mod tests {
use crate::app::Cli;
use crate::color;
use crate::color::Colors;
use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme};
use crate::flags::{
GitTheme as GitFlagTheme, HyperlinkOption, IconOption, IconTheme as FlagTheme,
};
use crate::icon::Icons;
use crate::meta::{FileType, Name};
use crate::Config;
Expand Down Expand Up @@ -695,7 +697,7 @@ mod tests {
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&GitTheme::new(),
&GitTheme::new(GitFlagTheme::Default),
);

assert_eq!("one.d\n├── .hidden\n└── two\n", output);
Expand Down Expand Up @@ -727,7 +729,7 @@ mod tests {
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&GitTheme::new(),
&GitTheme::new(GitFlagTheme::Default),
);

let length_before_b = |i| -> usize {
Expand Down Expand Up @@ -768,7 +770,7 @@ mod tests {
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&GitTheme::new(),
&GitTheme::new(GitFlagTheme::Default),
);

assert_eq!(output.lines().nth(1).unwrap().chars().next().unwrap(), '└');
Expand Down Expand Up @@ -808,7 +810,7 @@ mod tests {
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&GitTheme::new(),
&GitTheme::new(GitFlagTheme::Default),
);

assert!(output.ends_with("└── two\n"));
Expand Down Expand Up @@ -839,7 +841,7 @@ mod tests {
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&GitTheme::new(),
&GitTheme::new(GitFlagTheme::Default),
);

dir.close().unwrap();
Expand Down Expand Up @@ -873,7 +875,7 @@ mod tests {
&flags,
&Colors::new(color::ThemeOption::NoColor),
&Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()),
&GitTheme::new(),
&GitTheme::new(GitFlagTheme::Default),
);

dir.close().unwrap();
Expand Down
4 changes: 4 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod color;
pub mod date;
pub mod dereference;
pub mod display;
pub mod git_theme;
pub mod header;
pub mod hyperlink;
pub mod icons;
Expand All @@ -25,6 +26,7 @@ pub use color::{ColorOption, ThemeOption};
pub use date::DateFlag;
pub use dereference::Dereference;
pub use display::Display;
pub use git_theme::GitTheme;
pub use header::Header;
pub use hyperlink::HyperlinkOption;
pub use icons::IconOption;
Expand Down Expand Up @@ -78,6 +80,7 @@ pub struct Flags {
pub header: Header,
pub literal: Literal,
pub truncate_owner: TruncateOwner,
pub git_theme: GitTheme,
}

impl Flags {
Expand Down Expand Up @@ -109,6 +112,7 @@ impl Flags {
header: Header::configure_from(cli, config),
literal: Literal::configure_from(cli, config),
truncate_owner: TruncateOwner::configure_from(cli, config),
git_theme: GitTheme::configure_from(cli, config),
})
}
}
Expand Down
119 changes: 119 additions & 0 deletions src/flags/git_theme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//! This module defines the [GitTheme] flag. To set it up from [ArgMatches], a [Config] and its
//! [Default] value, use the [configure_from](Configurable::configure_from) method.
use super::Configurable;
use serde::de::{self, Deserializer, Visitor};

use crate::app::Cli;
use crate::config_file::Config;

/// Configure how to display git block
///
/// git block is enabled in block flags
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub enum GitTheme {
/// Default ASCII theme
#[default]
Default,
/// Custom theme using a YAML config file
Custom(String),
}

impl Configurable<Self> for GitTheme {
/// Get a potential `GitTheme` value from [ArgMatches].
///
/// Git theme is not yet configurable from cli
fn from_cli(_cli: &Cli) -> Option<Self> {
None
}

/// Get a potential `GitTheme` value from a [Config].
///
/// If the `Config::git_theme` has value,
/// this returns it as the value of the `GitTheme`, in a [Some].
/// Otherwise this returns [None].
fn from_config(config: &Config) -> Option<Self> {
config.git_theme.clone()
}
}

impl<'de> de::Deserialize<'de> for GitTheme {
fn deserialize<D>(deserializer: D) -> Result<GitTheme, D::Error>
where
D: Deserializer<'de>,
{
struct ThemeOptionVisitor;

impl<'de> Visitor<'de> for ThemeOptionVisitor {
type Value = GitTheme;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("`default` or <theme-file-path>")
}

fn visit_str<E>(self, value: &str) -> Result<GitTheme, E>
where
E: de::Error,
{
match value {
"default" => Ok(GitTheme::Default),
str => Ok(GitTheme::Custom(str.to_string())),
}
}
}

deserializer.deserialize_identifier(ThemeOptionVisitor)
}
}

#[cfg(not(feature = "no-git"))]
#[cfg(test)]
mod test {
use clap::Parser;

use super::GitTheme;

use crate::app::Cli;
use crate::config_file::Config;
use crate::flags::Configurable;

#[test]
fn test_from_arg_matches_none() {
let argv = ["lsd"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(None, GitTheme::from_cli(&cli));
}

#[test]
fn test_from_arg_matches_true() {
let argv = ["lsd", "--git"];
let cli = Cli::try_parse_from(argv).unwrap();
assert_eq!(None, GitTheme::from_cli(&cli));
}

#[test]
fn test_from_config_none() {
assert_eq!(None, GitTheme::from_config(&Config::with_none()));
}

#[test]
fn test_from_config_true() {
let mut c = Config::with_none();
c.git_theme = Some(GitTheme::Default);
assert_eq!(Some(GitTheme::Default), GitTheme::from_config(&c));
}

#[test]
fn test_from_config_false() {
let mut c = Config::with_none();
c.git_theme = Some(GitTheme::Default);
assert_eq!(Some(GitTheme::Default), GitTheme::from_config(&c));
}

#[test]
fn test_from_default_config() {
let mut c = Config::with_none();
c.git_theme = None;
assert_eq!(None, GitTheme::from_config(&c));
}
}
14 changes: 12 additions & 2 deletions src/git_theme.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
use crate::flags::GitTheme as GitFlagTheme;
use crate::git::GitStatus;
use crate::theme::git::GitThemeSymbols;
use crate::theme::Theme;
use std::path::Path;

pub struct GitTheme {
symbols: GitThemeSymbols,
}

impl GitTheme {
pub fn new() -> GitTheme {
let git_symbols = GitThemeSymbols::default();
pub fn new(theme: GitFlagTheme) -> GitTheme {
let git_symbols = match theme {
GitFlagTheme::Default => GitThemeSymbols::default(),
GitFlagTheme::Custom(ref file) => Theme::from_path::<GitThemeSymbols>(
Path::new("themes").join(file).to_str().unwrap_or(file),
)
.unwrap_or_default(),
};

Self {
symbols: git_symbols,
}
Expand Down

0 comments on commit c13e74c

Please sign in to comment.