cli: migrate ui.write_error() to writeln!()

This commit is contained in:
Yuya Nishihara 2023-01-12 15:41:38 +09:00
parent ea0c3930bb
commit a3438978a6
3 changed files with 15 additions and 20 deletions

View file

@ -1724,19 +1724,18 @@ pub fn handle_command_result(ui: &mut Ui, result: Result<(), CommandError>) -> i
match result {
Ok(()) => 0,
Err(CommandError::UserError { message, hint }) => {
ui.write_error(&format!("Error: {message}\n")).unwrap();
writeln!(ui.error(), "Error: {message}").unwrap();
if let Some(hint) = hint {
writeln!(ui.hint(), "Hint: {hint}").unwrap();
}
1
}
Err(CommandError::ConfigError(message)) => {
ui.write_error(&format!("Config error: {message}\n"))
.unwrap();
writeln!(ui.error(), "Config error: {message}").unwrap();
1
}
Err(CommandError::CliError(message)) => {
ui.write_error(&format!("Error: {message}\n")).unwrap();
writeln!(ui.error(), "Error: {message}").unwrap();
2
}
Err(CommandError::ClapCliError(inner)) => {
@ -1768,8 +1767,7 @@ pub fn handle_command_result(ui: &mut Ui, result: Result<(), CommandError>) -> i
}
Err(CommandError::BrokenPipe) => 3,
Err(CommandError::InternalError(message)) => {
ui.write_error(&format!("Internal error: {message}\n"))
.unwrap();
writeln!(ui.error(), "Internal error: {message}").unwrap();
255
}
}

View file

@ -1128,18 +1128,20 @@ fn add_to_git_exclude(ui: &mut Ui, git_repo: &git2::Repository) -> Result<(), Co
}
}
Err(err) => {
ui.write_error(&format!(
"Failed to add `.jj/` to {}: {}\n",
writeln!(
ui.error(),
"Failed to add `.jj/` to {}: {}",
exclude_file_path.to_string_lossy(),
err
))?;
)?;
}
}
} else {
ui.write_error(&format!(
"Failed to add `.jj/` to {} because it doesn't exist\n",
writeln!(
ui.error(),
"Failed to add `.jj/` to {} because it doesn't exist",
exclude_file_path.to_string_lossy()
))?;
)?;
}
Ok(())
}

View file

@ -226,12 +226,8 @@ impl Ui {
LabeledWriter::new(self.stderr_formatter(), "warning")
}
pub fn write_error(&mut self, text: &str) -> io::Result<()> {
let mut formatter = self.stderr_formatter();
formatter.add_label("error")?;
formatter.write_str(text)?;
formatter.remove_label()?;
Ok(())
pub fn error(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> {
LabeledWriter::new(self.stderr_formatter(), "error")
}
pub fn flush(&mut self) -> io::Result<()> {
@ -252,8 +248,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.
self.write_error(&format!("Failed to wait on pager: {e}\n"))
.ok();
writeln!(self.error(), "Failed to wait on pager: {e}").ok();
}
}
}