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

Optional command color #1670

Merged
merged 19 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
10 changes: 8 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ansi_term::Colour;
avi-cenna marked this conversation as resolved.
Show resolved Hide resolved
use {
super::*,
ansi_term::{ANSIGenericString, Color::*, Prefix, Style, Suffix},
Expand Down Expand Up @@ -76,8 +77,13 @@ impl Color {
self.restyle(Style::new().fg(Cyan).bold())
}

pub(crate) fn command(self) -> Self {
self.restyle(Style::new().bold())
pub(crate) fn command(self, foreground_color: Option<Colour>) -> Self {
let new_style = Style {
foreground: foreground_color,
is_bold: true,
..Style::default()
};
self.restyle(new_style)
avi-cenna marked this conversation as resolved.
Show resolved Hide resolved
}

pub(crate) fn parameter(self) -> Self {
Expand Down
36 changes: 36 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ansi_term::Colour;
avi-cenna marked this conversation as resolved.
Show resolved Hide resolved
use {
super::*,
clap::{App, AppSettings, Arg, ArgGroup, ArgMatches, ArgSettings},
Expand All @@ -15,6 +16,7 @@ pub(crate) const CHOOSE_HELP: &str = "Select one or more recipes to run using a
pub(crate) struct Config {
pub(crate) check: bool,
pub(crate) color: Color,
pub(crate) command_color: Option<Colour>,
avi-cenna marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) dotenv_filename: Option<String>,
pub(crate) dotenv_path: Option<PathBuf>,
pub(crate) dry_run: bool,
Expand Down Expand Up @@ -85,6 +87,7 @@ mod arg {
pub(crate) const CHOOSER: &str = "CHOOSER";
pub(crate) const CLEAR_SHELL_ARGS: &str = "CLEAR-SHELL-ARGS";
pub(crate) const COLOR: &str = "COLOR";
pub(crate) const COMMAND_COLOR: &str = "COMMAND-COLOR";
pub(crate) const DOTENV_FILENAME: &str = "DOTENV-FILENAME";
pub(crate) const DOTENV_PATH: &str = "DOTENV-PATH";
pub(crate) const DRY_RUN: &str = "DRY-RUN";
Expand All @@ -110,6 +113,12 @@ mod arg {
pub(crate) const COLOR_NEVER: &str = "never";
pub(crate) const COLOR_VALUES: &[&str] = &[COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER];

pub(crate) const COMMAND_COLOR_CYAN: &str = "cyan";
pub(crate) const COMMAND_COLOR_PURPLE: &str = "purple";
pub(crate) const COMMAND_COLOR_NONE: &str = "none";
avi-cenna marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) const COMMAND_COLOR_VALUES: &[&str] =
&[COMMAND_COLOR_NONE, COMMAND_COLOR_CYAN, COMMAND_COLOR_PURPLE];
avi-cenna marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) const DUMP_FORMAT_JSON: &str = "json";
pub(crate) const DUMP_FORMAT_JUST: &str = "just";
pub(crate) const DUMP_FORMAT_VALUES: &[&str] = &[DUMP_FORMAT_JUST, DUMP_FORMAT_JSON];
Expand Down Expand Up @@ -142,6 +151,14 @@ impl Config {
.default_value(arg::COLOR_AUTO)
.help("Print colorful output"),
)
.arg(
Arg::with_name(arg::COMMAND_COLOR)
.long("command-color")
.takes_value(true)
.possible_values(arg::COMMAND_COLOR_VALUES)
.default_value(arg::COMMAND_COLOR_NONE)
.help("Color to use when echoing recipe lines"),
avi-cenna marked this conversation as resolved.
Show resolved Hide resolved
)
.arg(
Arg::with_name(arg::DRY_RUN)
.short("n")
Expand Down Expand Up @@ -396,6 +413,23 @@ impl Config {
}
}

fn command_color_from_matches(matches: &ArgMatches) -> ConfigResult<Option<Colour>> {
let value = matches
.value_of(arg::COMMAND_COLOR)
.ok_or_else(|| ConfigError::Internal {
message: "`--command-color` had no value".to_string(),
})?;

match value {
arg::COMMAND_COLOR_NONE => Ok(None),
arg::COMMAND_COLOR_CYAN => Ok(Some(Colour::Cyan)),
arg::COMMAND_COLOR_PURPLE => Ok(Some(Colour::Purple)),
_ => Err(ConfigError::Internal {
message: format!("Invalid argument `{value}` to --command-color."),
}),
}
avi-cenna marked this conversation as resolved.
Show resolved Hide resolved
}

fn dump_format_from_matches(matches: &ArgMatches) -> ConfigResult<DumpFormat> {
let value = matches
.value_of(arg::DUMP_FORMAT)
Expand All @@ -422,6 +456,7 @@ impl Config {
};

let color = Self::color_from_matches(matches)?;
let command_color = Self::command_color_from_matches(matches)?;

let set_count = matches.occurrences_of(arg::SET);
let mut overrides = BTreeMap::new();
Expand Down Expand Up @@ -588,6 +623,7 @@ impl Config {
.unwrap_or(" ")
.to_owned(),
color,
command_color,
invocation_directory,
search_config,
shell_args,
Expand Down
2 changes: 1 addition & 1 deletion src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<'src, D> Recipe<'src, D> {
|| !((quiet_command ^ self.quiet) || config.verbosity.quiet())
{
let color = if config.highlight {
config.color.command()
config.color.command(config.command_color)
} else {
config.color
};
Expand Down
2 changes: 1 addition & 1 deletion tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test! {
error: The argument '--command <COMMAND>' requires a value but none was supplied

USAGE:
just{EXE_SUFFIX} --color <COLOR> --dump-format <FORMAT> --shell <SHELL> \
just{EXE_SUFFIX} --color <COLOR> --command-color <COMMAND-COLOR> --dump-format <FORMAT> --shell <SHELL> \
<--changelog|--choose|--command <COMMAND>|--completions <SHELL>|--dump|--edit|\
--evaluate|--fmt|--init|--list|--show <RECIPE>|--summary|--variables>

Expand Down
Loading