Skip to content

Commit

Permalink
Display color and boldness values in help message
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzrehde committed Nov 9, 2023
1 parent fa38272 commit 28943aa
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 28 deletions.
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/fritzrehde/watchbind"
description = "A CLI menu for periodically watching a program's output and executing commands on its lines through keybindings"

[dependencies]
clap = { version = "4.4.7", default-features = false, features = ["std", "help", "cargo", "derive", "error-context"] }
clap = { version = "4.4.7", default-features = false, features = ["std", "help", "cargo", "derive", "error-context", "string", "color"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
toml = { version = "0.7.8", default-features = false, features = ["parse"] }
ratatui = "0.22.0"
Expand Down
130 changes: 106 additions & 24 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod keybindings;
mod style;

pub use fields::{Fields, TableFormatter};
use itertools::Itertools;
pub use keybindings::{
KeyEvent, Keybindings, Operation, OperationParsed, Operations, OperationsParsed,
};
Expand All @@ -12,7 +13,7 @@ use self::fields::{FieldSelections, FieldSeparator};
use self::keybindings::{KeybindingsParsed, StringKeybindings};
use self::style::{Boldness, Color, Style};
use anyhow::{bail, Context, Result};
use clap::Parser;
use clap::{ArgAction, Parser, ValueEnum};
use indoc::indoc;
use serde::Deserialize;
use std::{fs::read_to_string, path::PathBuf, time::Duration};
Expand All @@ -27,7 +28,6 @@ pub struct Config {
pub keybindings_parsed: KeybindingsParsed,
pub header_lines: usize,
pub fields: Fields,
// pub initial_env_variables: Vec<String>,
pub initial_env_variables: OperationsParsed,
}

Expand Down Expand Up @@ -242,7 +242,7 @@ impl Default for TomlConfig {
}

#[derive(Parser)]
#[clap(version, about, rename_all = "kebab-case")]
#[command(version, about, rename_all = "kebab-case", after_help = Self::all_possible_values())]
pub struct ClapConfig {
/// Enable logging, and write logs to file.
#[arg(short, long, value_name = "FILE")]
Expand All @@ -260,51 +260,108 @@ pub struct ClapConfig {
#[arg(short, long, value_name = "FILE")]
config_file: Option<String>,

/// Seconds to wait between updates, 0 only executes once
#[arg(short, long, value_name = "SECS")]
/// Seconds (f64) to wait between updates, 0 only executes once
#[arg(short, long, value_name = "SECONDS")]
interval: Option<f64>,

/// Foreground color of cursor line
#[arg(long, value_name = "COLOR", default_value_t)]
#[arg(
long,
value_name = "COLOR",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
cursor_fg: Color,

/// Background color of cursor line
#[arg(long, value_name = "COLOR", default_value_t)]
#[arg(
long,
value_name = "COLOR",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
cursor_bg: Color,

/// Boldness of cursor line
#[arg(long, value_name = "BOLDNESS", default_value_t)]
#[arg(
long,
value_name = "BOLDNESS",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
cursor_boldness: Boldness,

/// Foreground color of header lines
#[arg(long, value_name = "COLOR", default_value_t)]
#[arg(
long,
value_name = "COLOR",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
header_fg: Color,

/// Background color of header lines
#[arg(long, value_name = "COLOR", default_value_t)]
#[arg(
long,
value_name = "COLOR",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
header_bg: Color,

/// Boldness of header lines
#[arg(long, value_name = "BOLDNESS", default_value_t)]
#[arg(
long,
value_name = "BOLDNESS",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
header_boldness: Boldness,

/// Foreground color of the lines that the cursor is not on and that are not
/// header lines.
#[arg(long, value_name = "COLOR", default_value_t)]
/// Foreground color of non-cursor, non-header lines.
#[arg(
long,
value_name = "COLOR",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
non_cursor_non_header_fg: Color,

/// Background color of the lines that the cursor is not on and that are not
/// header lines.
#[arg(long, value_name = "COLOR", default_value_t)]
/// Background color of non-cursor, non-header lines.
#[arg(
long,
value_name = "COLOR",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
non_cursor_non_header_bg: Color,

/// Boldness of the lines that the cursor is not on and that are not
/// header lines.
#[arg(long, value_name = "BOLDNESS", default_value_t)]
/// Boldness of non-cursor, non-header lines.
#[arg(
long,
value_name = "BOLDNESS",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
non_cursor_non_header_boldness: Boldness,

/// Background color of selected line marker
#[arg(long, value_name = "COLOR", default_value_t)]
/// Background color of selected line indicator
#[arg(
long,
value_name = "COLOR",
default_value_t,
hide_default_value = true,
hide_possible_values = true
)]
selected_bg: Color,

/// The first N lines of the input are treated as a sticky header
Expand All @@ -315,13 +372,38 @@ pub struct ClapConfig {
#[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).
/// Comma-separated field selections/ranges, e.g. `X`, `X-Y`, `X-` (field indexes start at 1).
#[arg(short = 'f', long = "fields", value_name = "LIST")]
field_selections: Option<FieldSelections>,

// TODO: replace with StringKeybindings once clap supports parsing into HashMap
// TODO: known clap bug: replace with ClapKeybindings once supported
/// Keybindings as comma-separated `KEY:OP[+OP]*` pairs, e.g., `q:select+exit,r:reload`.
/// Keybindings as comma-separated `KEY:OP[+OP]*` pairs, e.g. `q:select+exit,r:reload`.
#[arg(short = 'b', long = "bind", value_name = "LIST", value_delimiter = ',', value_parser = keybindings::parse_str)]
keybindings: Option<Vec<(String, Vec<String>)>>,
}

/// Get string list of all possible values of `T`.
fn get_possible_values<T: ValueEnum>() -> String {
T::value_variants()
.iter()
.filter_map(T::to_possible_value)
.map(|v| v.get_name().to_owned())
.join(", ")
}

impl ClapConfig {
/// Get printable help menu for possible values of `Color` and `Boldness`.
fn all_possible_values() -> String {
let possible_color_values = get_possible_values::<Color>();
let possible_boldness_values = get_possible_values::<Boldness>();
format!(
indoc! {r#"
Possible values:
COLOR: [{}]
BOLDNESS: [{}]
"#},
possible_color_values, possible_boldness_values
)
}
}
3 changes: 1 addition & 2 deletions src/ui/state/lines/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl<'a> ToUnformattedString for Text<'a> {
.map(|span| span.content.as_ref())
.collect::<String>()
})
.intersperse("\n".to_owned())
.collect()
.join("\n")
}
}
2 changes: 1 addition & 1 deletion src/ui/state/lines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Lines {

let selected_lines = match selected_lines_iter.peek() {
// There are some selected lines.
Some(_) => selected_lines_iter.intersperse("\n").collect(),
Some(_) => selected_lines_iter.join("\n"),
// There are no selected lines.
None => cursor_line.clone(),
};
Expand Down

0 comments on commit 28943aa

Please sign in to comment.