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

This commit is contained in:
Yuya Nishihara 2023-01-12 15:25:17 +09:00
parent 99b84778ed
commit 4a28962aaa
4 changed files with 15 additions and 17 deletions

View file

@ -1089,11 +1089,11 @@ pub fn print_failed_git_export(
formatter.write_str("\n")?;
}
drop(formatter);
ui.write_hint(
writeln!(
ui.hint(),
r#"Hint: Git doesn't allow a branch name that looks like a parent directory of
another (e.g. `foo` and `foo/bar`). Try to rename the branches that failed to
export or their "parent" branches.
"#,
export or their "parent" branches."#,
)?;
}
Ok(())
@ -1724,7 +1724,7 @@ pub fn handle_command_result(ui: &mut Ui, result: Result<(), CommandError>) -> i
Err(CommandError::UserError { message, hint }) => {
ui.write_error(&format!("Error: {message}\n")).unwrap();
if let Some(hint) = hint {
ui.write_hint(format!("Hint: {hint}\n")).unwrap();
writeln!(ui.hint(), "Hint: {hint}").unwrap();
}
1
}

View file

@ -1222,11 +1222,12 @@ Set `ui.allow-init-native` to allow initializing a repo with the native backend.
writeln!(ui, "Initialized repo in \"{}\"", relative_wc_path.display())?;
if args.git && wc_path.join(".git").exists() {
ui.write_warn("Empty repo created.\n")?;
ui.write_hint(format!(
writeln!(
ui.hint(),
"Hint: To create a repo backed by the existing Git repo, run `jj init --git-repo={}` \
instead.\n",
instead.",
relative_wc_path.display()
))?;
)?;
}
Ok(())
}

View file

@ -485,10 +485,11 @@ fn editor_name_from_settings(
Ok(editor_binary) => Ok(editor_binary),
Err(_) => {
let default_editor = "meld".to_string();
ui.write_hint(format!(
writeln!(
ui.hint(),
"Using default editor '{default_editor}'; you can change this by setting \
ui.{diff_or_merge}-editor\n"
))?;
ui.{diff_or_merge}-editor"
)?;
Ok(default_editor)
}
}

View file

@ -20,7 +20,7 @@ use std::{fmt, io, mem};
use crossterm::tty::IsTty;
use crate::config::FullCommandArgs;
use crate::formatter::{Formatter, FormatterFactory};
use crate::formatter::{Formatter, FormatterFactory, LabeledWriter};
pub struct Ui {
color: bool,
@ -217,12 +217,8 @@ impl Ui {
}
}
pub fn write_hint(&mut self, text: impl AsRef<str>) -> io::Result<()> {
let mut formatter = self.stderr_formatter();
formatter.add_label("hint")?;
formatter.write_str(text.as_ref())?;
formatter.remove_label()?;
Ok(())
pub fn hint(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> {
LabeledWriter::new(self.stderr_formatter(), "hint")
}
pub fn write_warn(&mut self, text: impl AsRef<str>) -> io::Result<()> {