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 30, 2024
1 parent 594be59 commit e4f0ce8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj duplicate` and `jj abandon` can now take more than a single `-r` argument,
for consistency with other commands.

* 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 @@ -2122,6 +2122,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 @@ -2291,6 +2301,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(format!(r#"ui.quiet=true"#));
}
if args.no_pager.unwrap_or_default() {
args.config_toml.push(r#"ui.paginate="never""#.to_owned());
}
Expand Down
26 changes: 26 additions & 0 deletions cli/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,32 @@ impl FormatterFactory {
}
}

pub struct SilentFormatter;

impl Write for SilentFormatter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

impl Formatter for SilentFormatter {
fn raw(&mut self) -> &mut dyn Write {
self
}

fn push_label(&mut self, _label: &str) -> io::Result<()> {
Ok(())
}

fn pop_label(&mut self) -> io::Result<()> {
Ok(())
}
}

pub struct PlainTextFormatter<W> {
output: W,
}
Expand Down
12 changes: 10 additions & 2 deletions cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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, SilentFormatter};

const BUILTIN_PAGER_NAME: &str = ":builtin";

Expand Down Expand Up @@ -163,6 +163,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 +246,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 +379,12 @@ 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(SilentFormatter)
}
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
16 changes: 16 additions & 0 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,21 @@ 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 +550,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 e4f0ce8

Please sign in to comment.