Skip to content

Commit

Permalink
cli: highlight "Error: " headings
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Mar 25, 2024
1 parent a5fe712 commit e03a274
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
12 changes: 6 additions & 6 deletions cli/src/command_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,11 @@ fn try_handle_command_result(
let hints = &cmd_err.hints;
match cmd_err.kind {
CommandErrorKind::User => {
print_error(ui, "Error", err, hints)?;
print_error(ui, "Error: ", err, hints)?;
Ok(ExitCode::from(1))
}
CommandErrorKind::Config => {
print_error(ui, "Config error", err, hints)?;
print_error(ui, "Config error: ", err, hints)?;
writeln!(
ui.hint_no_heading(),
"For help, see https://github.com/martinvonz/jj/blob/main/docs/config.md."
Expand All @@ -515,7 +515,7 @@ fn try_handle_command_result(
if let Some(err) = err.downcast_ref::<clap::Error>() {
handle_clap_error(ui, err, hints)
} else {
print_error(ui, "Error", err, hints)?;
print_error(ui, "Error: ", err, hints)?;
Ok(ExitCode::from(2))
}
}
Expand All @@ -524,14 +524,14 @@ fn try_handle_command_result(
Ok(ExitCode::from(BROKEN_PIPE_EXIT_CODE))
}
CommandErrorKind::Internal => {
print_error(ui, "Internal error", err, hints)?;
print_error(ui, "Internal error: ", err, hints)?;
Ok(ExitCode::from(255))
}
}
}

fn print_error(ui: &Ui, prefix: &str, err: &dyn error::Error, hints: &[String]) -> io::Result<()> {
writeln!(ui.error(), "{prefix}: {err}")?;
fn print_error(ui: &Ui, heading: &str, err: &dyn error::Error, hints: &[String]) -> io::Result<()> {
writeln!(ui.error_with_heading(heading), "{err}")?;
print_error_sources(ui, err.source())?;
for hint in hints {
writeln!(ui.hint_with_heading("Hint: "), "{hint}")?;
Expand Down
1 change: 1 addition & 0 deletions cli/src/config/colors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"error" = "red"
"warning" = "yellow"
"hint" = "cyan"
"error heading" = { bold = true }
"warning heading" = { bold = true }
"hint heading" = { bold = true }

Expand Down
6 changes: 4 additions & 2 deletions cli/src/templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,10 @@ where

fn format_error_inline(formatter: &mut dyn Formatter, err: &dyn error::Error) -> io::Result<()> {
formatter.with_label("error", |formatter| {
write!(formatter, "<Error")?;
for err in iter::successors(Some(err), |err| err.source()) {
write!(formatter, "<")?;
write!(formatter.labeled("heading"), "Error: ")?;
write!(formatter, "{err}")?;
for err in iter::successors(err.source(), |err| err.source()) {
write!(formatter, ": {err}")?;
}
write!(formatter, ">")?;
Expand Down
13 changes: 11 additions & 2 deletions cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,19 @@ impl Ui {
HeadingLabeledWriter::new(self.stderr_formatter(), "warning", heading)
}

pub fn error(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> {
/// Writer to print error without the "Error: " heading.
pub fn error_no_heading(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> {
LabeledWriter::new(self.stderr_formatter(), "error")
}

/// Writer to print error with the given heading.
pub fn error_with_heading<H: fmt::Display>(
&self,
heading: H,
) -> HeadingLabeledWriter<Box<dyn Formatter + '_>, &'static str, H> {
HeadingLabeledWriter::new(self.stderr_formatter(), "error", heading)
}

/// Waits for the pager exits.
#[instrument(skip_all)]
pub fn finalize_pager(&mut self) {
Expand All @@ -416,7 +425,7 @@ impl Ui {
// It's possible (though unlikely) that this write fails, but
// this function gets called so late that there's not much we
// can do about it.
writeln!(self.error(), "Failed to wait on pager: {e}").ok();
writeln!(self.error_no_heading(), "Failed to wait on pager: {e}").ok();
}
}
UiOutput::BuiltinPaged { pager } => {
Expand Down
19 changes: 18 additions & 1 deletion cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,31 @@ fn test_color_ui_messages() {
insta::assert_snapshot!(stderr, @r###"
Hint: Use `jj -h` for a list of available commands.
Run `jj config set --user ui.default-command log` to disable this message.
[38;5;1mError: There is no jj repo in "."[39m
[1m[38;5;1mError: [0m[38;5;1mThere is no jj repo in "."[39m
"###);

// warning
let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["log", "@"]);
insta::assert_snapshot!(stderr, @r###"
warning: The argument "@" is being interpreted as a path. To specify a revset, pass -r "@" instead.
"###);

// error inlined in template output
test_env.jj_cmd_ok(&repo_path, &["new"]);
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-r@|@--",
"--config-toml=templates.log_node='commit_id'",
"-Tdescription",
],
);
insta::assert_snapshot!(stdout, @r###"
8bb159bc30a9859930e567eb9238a7c43ee6744d
<Error: No commit available> (elided revisions)
0000000000000000000000000000000000000000
"###);
}

#[test]
Expand Down

0 comments on commit e03a274

Please sign in to comment.