Skip to content

Commit

Permalink
cli: add a global --quiet flag, which silences status messages
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvonz committed Mar 31, 2024
1 parent e8c9daf commit 4c2da99
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* `jj branch list` now allows combining `-r` and `-a` options.

* There is a new global `--quiet` flag to silence commands' non-primary output.

### Fixed bugs

## [0.15.1] - 2024-03-06
Expand Down
13 changes: 13 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2123,6 +2123,16 @@ pub struct EarlyArgs {
/// When to colorize output (always, never, auto)
#[arg(long, value_name = "WHEN", global = true)]
pub color: Option<ColorChoice>,
/// Silence non-primary command output
///
/// For example, `jj files` will still list files, but it won't tell you if
/// the working copy was snapshotted or if descendants were rebased.
///
/// Warnings and errors will still be printed.
#[arg(long, global = true, action = ArgAction::SetTrue)]
// Parsing with ignore_errors will crash if this is bool, so use
// Option<bool>.
pub quiet: Option<bool>,
/// Disable the pager
#[arg(long, value_name = "WHEN", global = true, action = ArgAction::SetTrue)]
// Parsing with ignore_errors will crash if this is bool, so use
Expand Down Expand Up @@ -2292,6 +2302,9 @@ fn handle_early_args(
if let Some(choice) = args.color {
args.config_toml.push(format!(r#"ui.color="{choice}""#));
}
if args.quiet.unwrap_or_default() {
args.config_toml.push(r#"ui.quiet=true"#.to_string());
}
if args.no_pager.unwrap_or_default() {
args.config_toml.push(r#"ui.paginate="never""#.to_owned());
}
Expand Down
13 changes: 11 additions & 2 deletions cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use tracing::instrument;

use crate::command_error::{config_error_with_message, CommandError};
use crate::config::CommandNameAndArgs;
use crate::formatter::{Formatter, FormatterFactory, HeadingLabeledWriter, LabeledWriter};
use crate::formatter::{
Formatter, FormatterFactory, HeadingLabeledWriter, LabeledWriter, PlainTextFormatter,
};

const BUILTIN_PAGER_NAME: &str = ":builtin";

Expand Down Expand Up @@ -163,6 +165,7 @@ impl Write for UiStderr<'_> {

pub struct Ui {
color: bool,
quiet: bool,
pager_cmd: CommandNameAndArgs,
paginate: PaginationChoice,
progress_indicator: bool,
Expand Down Expand Up @@ -245,13 +248,15 @@ fn pager_setting(config: &config::Config) -> Result<CommandNameAndArgs, CommandE
impl Ui {
pub fn with_config(config: &config::Config) -> Result<Ui, CommandError> {
let color = use_color(color_setting(config));
let quiet = config.get_bool("ui.quiet").unwrap_or_default();
// Sanitize ANSI escape codes if we're printing to a terminal. Doesn't affect
// ANSI escape codes that originate from the formatter itself.
let sanitize = io::stdout().is_terminal();
let formatter_factory = FormatterFactory::prepare(config, color, sanitize)?;
let progress_indicator = progress_indicator_setting(config);
Ok(Ui {
color,
quiet,
formatter_factory,
pager_cmd: pager_setting(config)?,
paginate: pagination_setting(config)?,
Expand Down Expand Up @@ -376,7 +381,11 @@ impl Ui {
/// Writes a message that's a status update not part of the command's main
/// output.
pub fn status(&self) -> Box<dyn Formatter + '_> {
self.stderr_formatter()
if self.quiet {
Box::new(PlainTextFormatter::new(std::io::sink()))
} else {
self.stderr_formatter()
}
}

/// Writer to print hint with the default "Hint: " heading.
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ To get started, see the tutorial at https://github.com/martinvonz/jj/blob/main/d
Possible values: `true`, `false`
* `--color <WHEN>` — When to colorize output (always, never, auto)
* `--quiet` — Silence non-primary command output
Possible values: `true`, `false`
* `--no-pager` — Disable the pager
Possible values: `true`, `false`
Expand Down
17 changes: 17 additions & 0 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ fn test_color_ui_messages() {
"###);
}

#[test]
fn test_quiet() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

// Can skip message about new working copy with `--quiet`
std::fs::write(repo_path.join("file1"), "contents").unwrap();
let (_stdout, stderr) =
test_env.jj_cmd_ok(&repo_path, &["--quiet", "describe", "-m=new description"]);
insta::assert_snapshot!(stderr, @r###"
Working copy now at: qpvuntsm 09bef04e new description
Parent commit : zzzzzzzz 00000000 (empty) (no description set)
"###);
}

#[test]
fn test_early_args() {
// Test that help output parses early args
Expand Down Expand Up @@ -535,6 +551,7 @@ fn test_help() {
--at-operation <AT_OPERATION> Operation to load the repo at [default: @] [aliases: at-op]
--debug Enable debug logging
--color <WHEN> When to colorize output (always, never, auto)
--quiet Silence non-primary command output
--no-pager Disable the pager
--config-toml <TOML> Additional configuration options (can be repeated)
"###);
Expand Down

0 comments on commit 4c2da99

Please sign in to comment.