Skip to content

Commit

Permalink
model: use strum to implement FromStr for ColorMode
Browse files Browse the repository at this point in the history
Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information
davvid committed Jul 16, 2023
1 parent a508b26 commit 1c5194a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
6 changes: 6 additions & 0 deletions doc/src/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.8.1 (beta0)

**Development**:

- Use `strum` to implement `FromStr` for `enum ColorMode`.

## v0.8.0

**Features**:
Expand Down
7 changes: 4 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use super::path;
#[command(name = "garden")]
#[command(author, version, about, long_about = None)]
pub struct MainOptions {
/// Set the color mode
/// Use ANSI colors [auto, true, false, on, off, always, never, 1, 0]
#[arg(
long,
require_equals = true,
num_args = 0..=1,
default_value_t = model::ColorMode::Auto,
default_missing_value = "on",
value_enum,
default_missing_value = "true",
value_name = "WHEN",
value_parser = model::ColorMode::parse_from_str,
)]
color: model::ColorMode,

Expand Down
69 changes: 37 additions & 32 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use super::eval;
use super::path;
use super::syntax;

use clap::ValueEnum;
use indexmap::{IndexMap, IndexSet};
use indextree::{Arena, NodeId};
use std::cell::RefCell;
use std::collections::HashMap;
use std::str::FromStr;
use strum::VariantNames;
use strum_macros;
use which::which;

/// TreeName keys into config.trees
Expand Down Expand Up @@ -1048,18 +1050,49 @@ impl TreeQuery {
}
}

#[derive(ValueEnum, Clone, Debug, Default, PartialEq, Eq)]
#[derive(
Clone,
Debug,
Default,
PartialEq,
Eq,
strum_macros::EnumString,
strum_macros::Display,
strum_macros::EnumVariantNames,
)]
#[strum(ascii_case_insensitive, serialize_all = "kebab-case")]
pub enum ColorMode {
/// Enable color when a tty is detected
#[default]
Auto,
/// Disable color
Off,
/// Enable color
#[strum(
serialize = "1",
serialize = "on",
serialize = "true",
serialize = "always",
serialize = "y",
serialize = "yes"
)]
On,
/// Disable color
#[strum(
serialize = "0",
serialize = "off",
serialize = "false",
serialize = "never",
serialize = "n",
serialize = "no"
)]
Off,
}

impl ColorMode {
/// Parse a color mode from a string using strum's from_str().
pub fn parse_from_str(string: &str) -> Result<ColorMode, String> {
ColorMode::from_str(string).map_err(|_| format!("choices are {:?}", Self::VARIANTS))
}

pub fn is_enabled(&self) -> bool {
match self {
ColorMode::Auto => atty::is(atty::Stream::Stdout),
Expand All @@ -1068,10 +1101,6 @@ impl ColorMode {
}
}

pub fn names() -> &'static str {
"auto, true, false, 1, 0, [y]es, [n]o, on, off, always, never"
}

pub fn update(&mut self) {
if *self == ColorMode::Auto {
// Speedup future calls to is_enabled() by performing the "auto"
Expand All @@ -1089,30 +1118,6 @@ impl ColorMode {
}
}

impl std::str::FromStr for ColorMode {
type Err = (); // For the FromStr trait

fn from_str(src: &str) -> Result<ColorMode, ()> {
match src.to_lowercase().as_ref() {
"auto" => Ok(ColorMode::Auto),
"-1" => Ok(ColorMode::Auto),
"0" => Ok(ColorMode::Off),
"1" => Ok(ColorMode::On),
"false" => Ok(ColorMode::Off),
"true" => Ok(ColorMode::On),
"never" => Ok(ColorMode::Off),
"always" => Ok(ColorMode::Off),
"off" => Ok(ColorMode::Off),
"on" => Ok(ColorMode::On),
"n" => Ok(ColorMode::Off),
"y" => Ok(ColorMode::On),
"no" => Ok(ColorMode::Off),
"yes" => Ok(ColorMode::On),
_ => Err(()),
}
}
}

// Color is an alias for yansi::Paint.
pub type Color<T> = yansi::Paint<T>;

Expand Down

0 comments on commit 1c5194a

Please sign in to comment.