Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
merge --no-colors and --force-colors into a single --colors arg…
Browse files Browse the repository at this point in the history
…ument
  • Loading branch information
leops committed Nov 14, 2022
1 parent 78f0ce9 commit dc5ca27
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
3 changes: 1 addition & 2 deletions crates/rome_cli/src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const MAIN: Markup = markup! {
- "<Emphasis>"help"</Emphasis>" Prints this help message
"<Emphasis>"OPTIONS:"</Emphasis>"
"<Dim>"--no-colors"</Dim>" Disable the formatting of markup (print everything as plain text)
"<Dim>"--force-colors"</Dim>" Force the formatting of markup using ANSI, even if the console output is determined to be incompatible
"<Dim>"--colors=<off|force>"</Dim>" Set the formatting mode for markup: \"off\" prints everything as plain text, \"force\" forces the formatting of markup using ANSI even if the console output is determined to be incompatible
"<Dim>"--use-server"</Dim>" Connect to a running instance of the Rome daemon server
"<Dim>"--version"</Dim>" Show the Rome version information and quit
"<Dim>"--files-max-size"</Dim>" The maximum allowed size for source code files in bytes (default: 1MB)
Expand Down
42 changes: 31 additions & 11 deletions crates/rome_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//! to parse commands and arguments, redirect the execution of the commands and
//! execute the traversal of directory and files, based on the command that were passed.
use std::str::FromStr;

pub use pico_args::Arguments;
use rome_console::{ColorMode, EnvConsole};
use rome_flags::FeatureFlags;
Expand Down Expand Up @@ -46,18 +48,36 @@ pub struct CliSession<'app> {

impl<'app> CliSession<'app> {
pub fn new(workspace: &'app dyn Workspace, mut args: Arguments) -> Result<Self, Termination> {
let no_colors = args.contains("--no-colors");
let force_colors = args.contains("--force-colors");
let colors = match (no_colors, force_colors) {
(true, false) => ColorMode::Disabled,
(false, true) => ColorMode::Enabled,
(false, false) => ColorMode::Auto,
(true, true) => {
return Err(Termination::IncompatibleArguments(
"--no-colors",
"--force-colors",
))
enum ColorsArg {
Off,
Force,
}

impl FromStr for ColorsArg {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"off" => Ok(Self::Off),
"force" => Ok(Self::Force),
_ => Err(format!(
"value {s:?} is not valid for the --colors argument"
)),
}
}
}

let colors =
args.opt_value_from_str("--colors")
.map_err(|source| Termination::ParseError {
argument: "--colors",
source,
})?;

let colors = match colors {
Some(ColorsArg::Off) => ColorMode::Disabled,
Some(ColorsArg::Force) => ColorMode::Enabled,
None => ColorMode::Auto,
};

Ok(Self {
Expand Down
16 changes: 6 additions & 10 deletions crates/rome_cli/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod main {
#[test]
fn no_colors() {
let workspace = workspace::server();
let args = Arguments::from_vec(vec![OsString::from("--no-colors")]);
let args = Arguments::from_vec(vec![OsString::from("--colors=off")]);
let result = CliSession::new(&*workspace, args).and_then(|session| session.run());

assert!(result.is_ok(), "run_cli returned {result:?}");
Expand All @@ -195,26 +195,22 @@ mod main {
#[test]
fn force_colors() {
let workspace = workspace::server();
let args = Arguments::from_vec(vec![OsString::from("--force-colors")]);
let args = Arguments::from_vec(vec![OsString::from("--colors=force")]);
let result = CliSession::new(&*workspace, args).and_then(|session| session.run());

assert!(result.is_ok(), "run_cli returned {result:?}");
}

#[test]
fn incompatible_colors() {
fn invalid_colors() {
let workspace = workspace::server();
let args = Arguments::from_vec(vec![
OsString::from("--no-colors"),
OsString::from("--force-colors"),
]);
let args = Arguments::from_vec(vec![OsString::from("--colors=other")]);

let result = CliSession::new(&*workspace, args).and_then(|session| session.run());

match result {
Err(Termination::IncompatibleArguments(lhs, rhs)) => {
assert_eq!(lhs, "--no-colors");
assert_eq!(rhs, "--force-colors");
Err(Termination::ParseError { argument, .. }) => {
assert_eq!(argument, "--colors");
}
_ => panic!("run_cli returned {result:?} for a malformed, expected an error"),
}
Expand Down
6 changes: 4 additions & 2 deletions website/src/pages/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ Stop the Rome [daemon](/internals/architecture#deamon) server

## Common Options

### `--no-colors`
### `--colors=<off|force>`

Disable the formatting of markup (print everything as plain text)
Set the formatting mode for markup: `off` prints everything as plain text,
`force` forces the formatting of markup using ANSI even if the console output
is determined to be incompatible

### `--use-server`

Expand Down

0 comments on commit dc5ca27

Please sign in to comment.