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

Use null Ui in completion to prevent writing warnings into completions #5539

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3714,7 +3714,7 @@ impl CliRunner {
ui.reset(&config)?;

if env::var_os("COMPLETE").is_some() {
return handle_shell_completion(ui, &self.app, &config, &cwd);
return handle_shell_completion(&Ui::null(), &self.app, &config, &cwd);
}

let string_args = expand_args(ui, &self.app, env::args_os(), &config)?;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ fn get_jj_command() -> Result<(JjBuilder, UserSettings), CommandError> {
// required.
let app = crate::commands::default_app();
let mut raw_config = config_from_environment(default_config_layers());
let ui = Ui::with_config(raw_config.as_ref()).expect("default config should be valid");
let ui = Ui::null();
let cwd = std::env::current_dir()
.and_then(dunce::canonicalize)
.map_err(user_error)?;
Expand Down
20 changes: 20 additions & 0 deletions cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ enum UiOutput {
err_wr: PipeWriter,
pager_thread: JoinHandle<streampager::Result<()>>,
},
Null,
}

impl UiOutput {
Expand Down Expand Up @@ -140,6 +141,7 @@ impl UiOutput {
}
}
}
UiOutput::Null => {}
}
}
}
Expand All @@ -148,12 +150,14 @@ pub enum UiStdout<'a> {
Terminal(StdoutLock<'static>),
Paged(&'a ChildStdin),
Builtin(&'a PipeWriter),
Null(io::Sink),
}

pub enum UiStderr<'a> {
Terminal(StderrLock<'static>),
Paged(&'a ChildStdin),
Builtin(&'a PipeWriter),
Null(io::Sink),
}

macro_rules! for_outputs {
Expand All @@ -162,6 +166,7 @@ macro_rules! for_outputs {
$ty::Terminal($pat) => $expr,
$ty::Paged($pat) => $expr,
$ty::Builtin($pat) => $expr,
$ty::Null($pat) => $expr,
}
};
}
Expand Down Expand Up @@ -254,6 +259,17 @@ pub enum PaginationChoice {
}

impl Ui {
pub fn null() -> Ui {
Ui {
quiet: true,
pager_cmd: CommandNameAndArgs::String(String::new()),
paginate: PaginationChoice::Never,
progress_indicator: false,
formatter_factory: FormatterFactory::plain_text(),
output: UiOutput::Null,
}
}

pub fn with_config(config: &StackedConfig) -> Result<Ui, CommandError> {
let formatter_factory = prepare_formatter_factory(config, &io::stdout())?;
Ok(Ui {
Expand Down Expand Up @@ -336,6 +352,7 @@ impl Ui {
UiOutput::Terminal { stdout, .. } => UiStdout::Terminal(stdout.lock()),
UiOutput::Paged { child_stdin, .. } => UiStdout::Paged(child_stdin),
UiOutput::BuiltinPaged { out_wr, .. } => UiStdout::Builtin(out_wr),
UiOutput::Null => UiStdout::Null(io::sink()),
}
}

Expand All @@ -353,6 +370,7 @@ impl Ui {
UiOutput::Terminal { stderr, .. } => UiStderr::Terminal(stderr.lock()),
UiOutput::Paged { child_stdin, .. } => UiStderr::Paged(child_stdin),
UiOutput::BuiltinPaged { err_wr, .. } => UiStderr::Builtin(err_wr),
UiOutput::Null => UiStderr::Null(io::sink()),
}
}

Expand All @@ -367,6 +385,7 @@ impl Ui {
UiOutput::Terminal { .. } => Ok(Stdio::inherit()),
UiOutput::Paged { child_stdin, .. } => Ok(duplicate_child_stdin(child_stdin)?.into()),
UiOutput::BuiltinPaged { err_wr, .. } => Ok(err_wr.try_clone()?.into()),
UiOutput::Null => Ok(Stdio::null()),
}
}

Expand All @@ -377,6 +396,7 @@ impl Ui {
UiOutput::Terminal { stderr, .. } => self.progress_indicator && stderr.is_terminal(),
UiOutput::Paged { .. } => false,
UiOutput::BuiltinPaged { .. } => false,
UiOutput::Null => false,
}
}

Expand Down