Skip to content

Commit

Permalink
Improved parsing of colors and boldness
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzrehde committed Nov 8, 2023
1 parent c97399d commit ba3f693
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 117 deletions.
130 changes: 63 additions & 67 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use style::Styles;

use self::fields::{FieldSelections, FieldSeparator};
use self::keybindings::{KeybindingsParsed, StringKeybindings};
use self::style::{Boldness, Color};
use anyhow::{bail, Context, Result};
use clap::Parser;
use indoc::indoc;
Expand Down Expand Up @@ -58,15 +59,15 @@ impl TryFrom<TomlConfig> for Config {
toml.interval.or(default.interval).expect("default"),
),
styles: Styles::parse(
toml.fg.or(default.fg),
toml.bg.or(default.bg),
toml.bold.or(default.bold),
toml.non_cursor_fg.or(default.non_cursor_fg),
toml.non_cursor_bg.or(default.non_cursor_bg),
toml.non_cursor_boldness.or(default.non_cursor_boldness),
toml.cursor_fg.or(default.cursor_fg),
toml.cursor_bg.or(default.cursor_bg),
toml.cursor_bold.or(default.cursor_bold),
toml.cursor_boldness.or(default.cursor_boldness),
toml.header_fg.or(default.header_fg),
toml.header_bg.or(default.header_bg),
toml.header_bold.or(default.header_bold),
toml.header_boldness.or(default.header_boldness),
toml.selected_bg.or(default.selected_bg),
)?,
keybindings_parsed: StringKeybindings::merge(toml.keybindings, default.keybindings)
Expand All @@ -79,50 +80,34 @@ impl TryFrom<TomlConfig> for Config {
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct TomlConfig {
log_file: Option<PathBuf>,

#[serde(rename = "initial-env")]
initial_env_variables: Option<Vec<String>>,

#[serde(rename = "watched-command")]
watched_command: Option<String>,

interval: Option<f64>,
fg: Option<String>,
bg: Option<String>,
bold: Option<bool>,

#[serde(rename = "cursor-fg")]
cursor_fg: Option<String>,

#[serde(rename = "cursor-bg")]
cursor_bg: Option<String>,

#[serde(rename = "cursor-bold")]
cursor_bold: Option<bool>,

#[serde(rename = "header-fg")]
header_fg: Option<String>,

#[serde(rename = "header-bg")]
header_bg: Option<String>,

#[serde(rename = "header-bold")]
header_bold: Option<bool>,
non_cursor_fg: Color,
non_cursor_bg: Color,
non_cursor_boldness: Boldness,

#[serde(rename = "selected-bg")]
selected_bg: Option<String>,
cursor_fg: Color,
cursor_bg: Color,
cursor_boldness: Boldness,

#[serde(rename = "header-lines")]
header_lines: Option<usize>,
header_fg: Color,
header_bg: Color,
header_boldness: Boldness,

#[serde(rename = "field-separator")]
field_separator: Option<FieldSeparator>,
selected_bg: Color,

#[serde(rename = "fields")]
field_selections: Option<FieldSelections>,
field_separator: Option<FieldSeparator>,

keybindings: Option<StringKeybindings>,
}
Expand All @@ -144,15 +129,15 @@ impl TomlConfig {
initial_env_variables: self.initial_env_variables.or(other.initial_env_variables),
watched_command: self.watched_command.or(other.watched_command),
interval: self.interval.or(other.interval),
fg: self.fg.or(other.fg),
bg: self.bg.or(other.bg),
bold: self.bold.or(other.bold),
non_cursor_fg: self.non_cursor_fg.or(other.non_cursor_fg),
non_cursor_bg: self.non_cursor_bg.or(other.non_cursor_bg),
non_cursor_boldness: self.non_cursor_boldness.or(other.non_cursor_boldness),
cursor_fg: self.cursor_fg.or(other.cursor_fg),
cursor_bg: self.cursor_bg.or(other.cursor_bg),
cursor_bold: self.cursor_bold.or(other.cursor_bold),
cursor_boldness: self.cursor_boldness.or(other.cursor_boldness),
header_fg: self.header_fg.or(other.header_fg),
header_bg: self.header_bg.or(other.header_bg),
header_bold: self.header_bold.or(other.header_bold),
header_boldness: self.header_boldness.or(other.header_boldness),
selected_bg: self.selected_bg.or(other.selected_bg),
header_lines: self.header_lines.or(other.header_lines),
field_separator: self.field_separator.or(other.field_separator),
Expand All @@ -169,15 +154,15 @@ impl From<ClapConfig> for TomlConfig {
initial_env_variables: clap.initial_env_variables,
watched_command: clap.watched_command.map(|s| s.join(" ")),
interval: clap.interval,
fg: clap.fg,
bg: clap.bg,
bold: clap.bold,
non_cursor_fg: clap.non_cursor_fg,
non_cursor_bg: clap.non_cursor_bg,
non_cursor_boldness: clap.non_cursor_boldness,
cursor_fg: clap.cursor_fg,
cursor_bg: clap.cursor_bg,
cursor_bold: clap.cursor_bold,
cursor_boldness: clap.cursor_boldness,
header_fg: clap.header_fg,
header_bg: clap.header_bg,
header_bold: clap.header_bold,
header_boldness: clap.header_boldness,
selected_bg: clap.selected_bg,
header_lines: clap.header_lines,
field_separator: clap.field_separator,
Expand All @@ -191,8 +176,19 @@ impl Default for TomlConfig {
fn default() -> Self {
let toml = indoc! {r#"
"interval" = 3.0
"cursor-bg" = "blue"
"cursor-bold" = true
"non-cursor-fg" = "unspecified"
"non-cursor-bg" = "unspecified"
"non-cursor-boldness" = "unspecified"
"cursor-fg" = "unspecified"
"cursor-bg" = "gray"
"cursor-boldness" = "bold"
"header-fg" = "blue"
"header-bg" = "unspecified"
"header-boldness" = "non-bold"
"selected-bg" = "magenta"
[keybindings]
Expand Down Expand Up @@ -238,51 +234,51 @@ pub struct ClapConfig {
interval: Option<f64>,

/// Foreground color of all lines except cursor
#[arg(long, value_name = "COLOR")]
fg: Option<String>,
#[arg(long, value_name = "COLOR", default_value_t)]
non_cursor_fg: Color,

/// Background color of all lines except cursor
#[arg(long, value_name = "COLOR")]
bg: Option<String>,
#[arg(long, value_name = "COLOR", default_value_t)]
non_cursor_bg: Color,

/// Text on all lines except the cursor's line are bold
#[arg(long, value_name = "BOOL")]
bold: Option<bool>,
#[arg(long, value_name = "BOLDNESS", default_value_t)]
non_cursor_boldness: Boldness,

/// Foreground color of cursor
#[arg(long = "cursor-fg", value_name = "COLOR")]
cursor_fg: Option<String>,
#[arg(long, value_name = "COLOR", default_value_t)]
cursor_fg: Color,

/// Background color of cursor
#[arg(long = "cursor-bg", value_name = "COLOR")]
cursor_bg: Option<String>,
#[arg(long, value_name = "COLOR", default_value_t)]
cursor_bg: Color,

/// Text on cursor's line is bold
#[arg(long = "cursor-bold", value_name = "BOOL")]
cursor_bold: Option<bool>,
#[arg(long, value_name = "BOLDNESS", default_value_t)]
cursor_boldness: Boldness,

/// Foreground color of header lines
#[arg(long = "header-fg", value_name = "COLOR")]
header_fg: Option<String>,
#[arg(long, value_name = "COLOR", default_value_t)]
header_fg: Color,

/// Background color of header lines
#[arg(long = "header-bg", value_name = "COLOR")]
header_bg: Option<String>,
#[arg(long, value_name = "COLOR", default_value_t)]
header_bg: Color,

/// Text on header line is bold
#[arg(long = "header-bold", value_name = "BOOL")]
header_bold: Option<bool>,
#[arg(long, value_name = "BOLDNESS", default_value_t)]
header_boldness: Boldness,

/// Background color of selected line marker
#[arg(long = "selected-bg", value_name = "COLOR")]
selected_bg: Option<String>,
#[arg(long, value_name = "COLOR", default_value_t)]
selected_bg: Color,

/// The first N lines of the input are treated as a sticky header
#[arg(long = "header-lines", value_name = "N")]
#[arg(long, value_name = "N")]
header_lines: Option<usize>,

/// Field separator [possible values: any string]
#[arg(short = 's', long = "field-separator", value_name = "STRING")]
#[arg(short = 's', long, value_name = "STRING")]
field_separator: Option<FieldSeparator>,

/// Field selections/ranges (comma-separated), e.g., `X`, `X-Y`, `X-` (field indexes start at 1).
Expand Down
Loading

0 comments on commit ba3f693

Please sign in to comment.