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

Commit

Permalink
feat(rome_cli): add a --force-colors argument (#3625)
Browse files Browse the repository at this point in the history
* feat(rome_cli): add a `--force-colors` argument

* merge `--no-colors` and `--force-colors` into a single `--colors` argument
  • Loading branch information
leops authored Nov 14, 2022
1 parent 7082c83 commit 58fbb93
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/rome_cli/src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +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>"--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
45 changes: 39 additions & 6 deletions crates/rome_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
//! 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::EnvConsole;
use rome_console::{ColorMode, EnvConsole};
use rome_flags::FeatureFlags;
use rome_fs::OsFileSystem;
use rome_service::{App, DynRef, Workspace, WorkspaceRef};
Expand Down Expand Up @@ -45,16 +47,47 @@ pub struct CliSession<'app> {
}

impl<'app> CliSession<'app> {
pub fn new(workspace: &'app dyn Workspace, mut args: Arguments) -> Self {
let no_colors = args.contains("--no-colors");
Self {
pub fn new(workspace: &'app dyn Workspace, mut args: Arguments) -> Result<Self, Termination> {
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 {
app: App::new(
DynRef::Owned(Box::new(OsFileSystem)),
DynRef::Owned(Box::new(EnvConsole::new(no_colors))),
DynRef::Owned(Box::new(EnvConsole::new(colors))),
WorkspaceRef::Borrowed(workspace),
),
args,
}
})
}

/// Main function to run Rome CLI
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ fn main() -> Result<(), Termination> {
workspace::server()
};

CliSession::new(&*workspace, args).run()
let session = CliSession::new(&*workspace, args)?;
session.run()
}
34 changes: 34 additions & 0 deletions crates/rome_cli/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod help {
mod main {
use super::*;
use rome_diagnostics::MAXIMUM_DISPLAYABLE_DIAGNOSTICS;
use rome_service::workspace;

#[test]
fn unknown_command() {
Expand Down Expand Up @@ -181,6 +182,39 @@ mod main {
_ => panic!("run_cli returned {result:?} for a malformed, expected an error"),
}
}

#[test]
fn no_colors() {
let workspace = workspace::server();
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:?}");
}

#[test]
fn force_colors() {
let workspace = workspace::server();
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 invalid_colors() {
let workspace = workspace::server();
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::ParseError { argument, .. }) => {
assert_eq!(argument, "--colors");
}
_ => panic!("run_cli returned {result:?} for a malformed, expected an error"),
}
}
}

mod init {
Expand Down
42 changes: 31 additions & 11 deletions crates/rome_console/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,37 @@ pub struct EnvConsole {
r#in: Stdin,
}

pub enum ColorMode {
/// Always print color using either ANSI or the Windows Console API
Enabled,
/// Never print colors
Disabled,
/// Print colors if stdout / stderr are determined to be TTY / Console
/// streams, and the `TERM=dumb` and `NO_COLOR` environment variables are
/// not set
Auto,
}

impl EnvConsole {
pub fn new(no_colors: bool) -> Self {
let out_mode = if no_colors || !atty::is(atty::Stream::Stdout) {
ColorChoice::Never
} else {
ColorChoice::Auto
};
let err_mode = if no_colors || !atty::is(atty::Stream::Stderr) {
ColorChoice::Never
} else {
ColorChoice::Auto
pub fn new(colors: ColorMode) -> Self {
let (out_mode, err_mode) = match colors {
ColorMode::Enabled => (ColorChoice::Always, ColorChoice::Always),
ColorMode::Disabled => (ColorChoice::Never, ColorChoice::Never),
ColorMode::Auto => {
let stdout = if atty::is(atty::Stream::Stdout) {
ColorChoice::Auto
} else {
ColorChoice::Never
};

let stderr = if atty::is(atty::Stream::Stderr) {
ColorChoice::Auto
} else {
ColorChoice::Never
};

(stdout, stderr)
}
};

Self {
Expand All @@ -86,7 +106,7 @@ impl EnvConsole {

impl Default for EnvConsole {
fn default() -> Self {
Self::new(false)
Self::new(ColorMode::Auto)
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/rome_js_semantic/src/tests/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn assert(code: &str, test_name: &str) {
let r = rome_js_parser::parse(code, FileId::zero(), SourceType::tsx());

if r.has_errors() {
let mut console = EnvConsole::new(false);
let mut console = EnvConsole::default();
for diag in r.into_diagnostics() {
let error = diag
.with_file_path(FileId::zero())
Expand Down Expand Up @@ -756,7 +756,7 @@ fn error_assertion_not_attached_to_a_declaration(
.with_file_path((test_name.to_string(), FileId::zero()))
.with_file_source_code(code);

let mut console = EnvConsole::new(false);
let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error)}
});
Expand All @@ -777,7 +777,7 @@ fn error_declaration_pointing_to_unknown_scope(
.with_file_path((test_name.to_string(), FileId::zero()))
.with_file_source_code(code);

let mut console = EnvConsole::new(false);
let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error)}
});
Expand All @@ -802,7 +802,7 @@ fn error_assertion_name_clash(
.with_file_path((test_name.to_string(), FileId::zero()))
.with_file_source_code(code);

let mut console = EnvConsole::new(false);
let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error)}
});
Expand All @@ -825,7 +825,7 @@ fn error_scope_end_assertion_points_to_non_existing_scope_start_assertion(
.with_file_path((file_name.to_string(), FileId::zero()))
.with_file_source_code(code);

let mut console = EnvConsole::new(false);
let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&error)}
});
Expand All @@ -852,7 +852,7 @@ fn error_scope_end_assertion_points_to_the_wrong_scope_start(
.with_file_path((file_name.to_string(), FileId::zero()))
.with_file_source_code(code);

let mut console = EnvConsole::new(false);
let mut console = EnvConsole::default();
console.log(markup! {
{PrintDiagnostic(&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
4 changes: 2 additions & 2 deletions xtask/lintdoc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ fn assert_lint(
if test.expect_diagnostic {
// Print all diagnostics to help the user
if all_diagnostics.len() > 1 {
let mut console = rome_console::EnvConsole::new(false);
let mut console = rome_console::EnvConsole::default();
for diag in all_diagnostics.iter() {
console.print(
rome_console::LogLevel::Error,
Expand All @@ -492,7 +492,7 @@ fn assert_lint(
);
} else {
// Print all diagnostics to help the user
let mut console = rome_console::EnvConsole::new(false);
let mut console = rome_console::EnvConsole::default();
for diag in all_diagnostics.iter() {
console.print(
rome_console::LogLevel::Error,
Expand Down

0 comments on commit 58fbb93

Please sign in to comment.