mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-19 19:08:08 +00:00
formatter: use write!() or writeln!() thoroughly, remove .write_str()
One less Formatter API.
This commit is contained in:
parent
011b20fdac
commit
00285be7a7
9 changed files with 107 additions and 112 deletions
|
@ -192,7 +192,7 @@ pub(crate) fn print_conflicted_paths(
|
|||
write!(formatter, "{formatted_path} ",)?;
|
||||
formatter.with_label("conflict_description", |formatter| {
|
||||
let print_pair = |formatter: &mut dyn Formatter, (text, label): &(String, &str)| {
|
||||
formatter.with_label(label, |fmt| fmt.write_str(text))
|
||||
write!(formatter.labeled(label), "{text}")
|
||||
};
|
||||
print_pair(
|
||||
formatter,
|
||||
|
@ -201,10 +201,10 @@ pub(crate) fn print_conflicted_paths(
|
|||
if sides > 2 { "difficult" } else { "normal" },
|
||||
),
|
||||
)?;
|
||||
formatter.write_str(" conflict")?;
|
||||
write!(formatter, " conflict")?;
|
||||
|
||||
if !seen_objects.is_empty() {
|
||||
formatter.write_str(" including ")?;
|
||||
write!(formatter, " including ")?;
|
||||
let seen_objects = seen_objects.into_iter().collect_vec();
|
||||
match &seen_objects[..] {
|
||||
[] => unreachable!(),
|
||||
|
@ -212,10 +212,10 @@ pub(crate) fn print_conflicted_paths(
|
|||
[first, middle @ .., last] => {
|
||||
print_pair(formatter, first)?;
|
||||
for pair in middle {
|
||||
formatter.write_str(", ")?;
|
||||
write!(formatter, ", ")?;
|
||||
print_pair(formatter, pair)?;
|
||||
}
|
||||
formatter.write_str(" and ")?;
|
||||
write!(formatter, " and ")?;
|
||||
print_pair(formatter, last)?;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -57,9 +57,9 @@ pub(crate) fn cmd_status(
|
|||
let parent_tree = merge_commit_trees(repo.as_ref(), &wc_commit.parents())?;
|
||||
let tree = wc_commit.tree()?;
|
||||
if tree.id() == parent_tree.id() {
|
||||
formatter.write_str("The working copy is clean\n")?;
|
||||
writeln!(formatter, "The working copy is clean")?;
|
||||
} else {
|
||||
formatter.write_str("Working copy changes:\n")?;
|
||||
writeln!(formatter, "Working copy changes:")?;
|
||||
diff_util::show_diff_summary(
|
||||
formatter,
|
||||
&workspace_command,
|
||||
|
@ -77,16 +77,16 @@ pub(crate) fn cmd_status(
|
|||
}
|
||||
|
||||
let template = workspace_command.commit_summary_template();
|
||||
formatter.write_str("Working copy : ")?;
|
||||
write!(formatter, "Working copy : ")?;
|
||||
formatter.with_label("working_copy", |fmt| template.format(wc_commit, fmt))?;
|
||||
formatter.write_str("\n")?;
|
||||
writeln!(formatter)?;
|
||||
for parent in wc_commit.parents() {
|
||||
formatter.write_str("Parent commit: ")?;
|
||||
write!(formatter, "Parent commit: ")?;
|
||||
template.format(&parent, formatter)?;
|
||||
formatter.write_str("\n")?;
|
||||
writeln!(formatter)?;
|
||||
}
|
||||
} else {
|
||||
formatter.write_str("No working copy\n")?;
|
||||
writeln!(formatter, "No working copy")?;
|
||||
}
|
||||
|
||||
let conflicted_local_branches = repo
|
||||
|
|
|
@ -827,7 +827,7 @@ impl CommitOrChangeId {
|
|||
|
||||
impl Template<()> for CommitOrChangeId {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(&self.hex())
|
||||
write!(formatter, "{}", self.hex())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -871,8 +871,9 @@ pub struct ShortestIdPrefix {
|
|||
|
||||
impl Template<()> for ShortestIdPrefix {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.with_label("prefix", |fmt| fmt.write_str(&self.prefix))?;
|
||||
formatter.with_label("rest", |fmt| fmt.write_str(&self.rest))
|
||||
write!(formatter.labeled("prefix"), "{}", self.prefix)?;
|
||||
write!(formatter.labeled("rest"), "{}", self.rest)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -281,7 +281,7 @@ fn show_color_words_diff_hunks(
|
|||
}
|
||||
if start_skipping_context {
|
||||
context.drain(..2);
|
||||
formatter.write_str(SKIPPED_CONTEXT_LINE)?;
|
||||
write!(formatter, "{SKIPPED_CONTEXT_LINE}")?;
|
||||
skipped_context = true;
|
||||
context_before = true;
|
||||
}
|
||||
|
@ -305,7 +305,7 @@ fn show_color_words_diff_hunks(
|
|||
show_color_words_diff_line(formatter, line)?;
|
||||
}
|
||||
if context_before {
|
||||
formatter.write_str(SKIPPED_CONTEXT_LINE)?;
|
||||
write!(formatter, "{SKIPPED_CONTEXT_LINE}")?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -313,7 +313,7 @@ fn show_color_words_diff_hunks(
|
|||
let no_hunk = left.is_empty() && right.is_empty();
|
||||
let any_last_newline = left.ends_with(b"\n") || right.ends_with(b"\n");
|
||||
if !skipped_context && !no_hunk && !any_last_newline {
|
||||
formatter.write_str("\n")?;
|
||||
writeln!(formatter)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -329,9 +329,9 @@ fn show_color_words_diff_line(
|
|||
"{:>4}",
|
||||
diff_line.left_line_number
|
||||
)?;
|
||||
formatter.write_str(" ")?;
|
||||
write!(formatter, " ")?;
|
||||
} else {
|
||||
formatter.write_str(" ")?;
|
||||
write!(formatter, " ")?;
|
||||
}
|
||||
if diff_line.has_right_content {
|
||||
write!(
|
||||
|
@ -339,9 +339,9 @@ fn show_color_words_diff_line(
|
|||
"{:>4}",
|
||||
diff_line.right_line_number
|
||||
)?;
|
||||
formatter.write_str(": ")?;
|
||||
write!(formatter, ": ")?;
|
||||
} else {
|
||||
formatter.write_str(" : ")?;
|
||||
write!(formatter, " : ")?;
|
||||
}
|
||||
for hunk in &diff_line.hunks {
|
||||
match hunk {
|
||||
|
@ -744,25 +744,25 @@ fn show_unified_diff_hunks(
|
|||
match line_type {
|
||||
DiffLineType::Context => {
|
||||
formatter.with_label("context", |formatter| {
|
||||
formatter.write_str(" ")?;
|
||||
write!(formatter, " ")?;
|
||||
formatter.write_all(content)
|
||||
})?;
|
||||
}
|
||||
DiffLineType::Removed => {
|
||||
formatter.with_label("removed", |formatter| {
|
||||
formatter.write_str("-")?;
|
||||
write!(formatter, "-")?;
|
||||
formatter.write_all(content)
|
||||
})?;
|
||||
}
|
||||
DiffLineType::Added => {
|
||||
formatter.with_label("added", |formatter| {
|
||||
formatter.write_str("+")?;
|
||||
write!(formatter, "+")?;
|
||||
formatter.write_all(content)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
if !content.ends_with(b"\n") {
|
||||
formatter.write_str("\n\\ No newline at end of file\n")?;
|
||||
write!(formatter, "\n\\ No newline at end of file\n")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,10 +25,6 @@ use itertools::Itertools;
|
|||
|
||||
// Lets the caller label strings and translates the labels to colors
|
||||
pub trait Formatter: Write {
|
||||
fn write_str(&mut self, text: &str) -> io::Result<()> {
|
||||
self.write_all(text.as_bytes())
|
||||
}
|
||||
|
||||
/// Returns the backing `Write`. This is useful for writing data that is
|
||||
/// already formatted, such as in the graphical log.
|
||||
fn raw(&mut self) -> &mut dyn Write;
|
||||
|
@ -609,7 +605,7 @@ mod tests {
|
|||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = PlainTextFormatter::new(&mut output);
|
||||
formatter.push_label("warning").unwrap();
|
||||
formatter.write_str("hello").unwrap();
|
||||
write!(formatter, "hello").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"hello");
|
||||
}
|
||||
|
@ -619,7 +615,7 @@ mod tests {
|
|||
// Test that ANSI codes in the input text are NOT escaped.
|
||||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = PlainTextFormatter::new(&mut output);
|
||||
formatter.write_str("\x1b[1mactually bold\x1b[0m").unwrap();
|
||||
write!(formatter, "\x1b[1mactually bold\x1b[0m").unwrap();
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"[1mactually bold[0m");
|
||||
}
|
||||
|
||||
|
@ -628,9 +624,7 @@ mod tests {
|
|||
// Test that ANSI codes in the input text are escaped.
|
||||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = SanitizingFormatter::new(&mut output);
|
||||
formatter
|
||||
.write_str("\x1b[1mnot actually bold\x1b[0m")
|
||||
.unwrap();
|
||||
write!(formatter, "\x1b[1mnot actually bold\x1b[0m").unwrap();
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"␛[1mnot actually bold␛[0m");
|
||||
}
|
||||
|
||||
|
@ -667,9 +661,9 @@ mod tests {
|
|||
ColorFormatter::for_config(&mut output, &config_builder.build().unwrap()).unwrap();
|
||||
for color in colors {
|
||||
formatter.push_label(&color.replace(' ', "-")).unwrap();
|
||||
formatter.write_str(&format!(" {color} ")).unwrap();
|
||||
write!(formatter, " {color} ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("\n").unwrap();
|
||||
writeln!(formatter).unwrap();
|
||||
}
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @r###"
|
||||
|
@ -712,9 +706,9 @@ mod tests {
|
|||
ColorFormatter::for_config(&mut output, &config_builder.build().unwrap()).unwrap();
|
||||
for [label, _] in labels_and_colors {
|
||||
formatter.push_label(&label.replace(' ', "-")).unwrap();
|
||||
formatter.write_str(&format!(" {label} ")).unwrap();
|
||||
write!(formatter, " {label} ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("\n").unwrap();
|
||||
writeln!(formatter).unwrap();
|
||||
}
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @r###"
|
||||
|
@ -735,11 +729,11 @@ mod tests {
|
|||
);
|
||||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.write_str(" before ").unwrap();
|
||||
write!(formatter, " before ").unwrap();
|
||||
formatter.push_label("inside").unwrap();
|
||||
formatter.write_str(" inside ").unwrap();
|
||||
write!(formatter, " inside ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" after ").unwrap();
|
||||
write!(formatter, " after ").unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @" before [38;5;2m inside [39m after ");
|
||||
}
|
||||
|
@ -760,31 +754,31 @@ mod tests {
|
|||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("red_fg").unwrap();
|
||||
formatter.write_str(" fg only ").unwrap();
|
||||
write!(formatter, " fg only ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("\n").unwrap();
|
||||
writeln!(formatter).unwrap();
|
||||
formatter.push_label("blue_bg").unwrap();
|
||||
formatter.write_str(" bg only ").unwrap();
|
||||
write!(formatter, " bg only ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("\n").unwrap();
|
||||
writeln!(formatter).unwrap();
|
||||
formatter.push_label("bold_font").unwrap();
|
||||
formatter.write_str(" bold only ").unwrap();
|
||||
write!(formatter, " bold only ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("\n").unwrap();
|
||||
writeln!(formatter).unwrap();
|
||||
formatter.push_label("underlined_text").unwrap();
|
||||
formatter.write_str(" underlined only ").unwrap();
|
||||
write!(formatter, " underlined only ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("\n").unwrap();
|
||||
writeln!(formatter).unwrap();
|
||||
formatter.push_label("multiple").unwrap();
|
||||
formatter.write_str(" single rule ").unwrap();
|
||||
write!(formatter, " single rule ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("\n").unwrap();
|
||||
writeln!(formatter).unwrap();
|
||||
formatter.push_label("red_fg").unwrap();
|
||||
formatter.push_label("blue_bg").unwrap();
|
||||
formatter.write_str(" two rules ").unwrap();
|
||||
write!(formatter, " two rules ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("\n").unwrap();
|
||||
writeln!(formatter).unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @r###"
|
||||
[38;5;1m fg only [39m
|
||||
|
@ -808,11 +802,11 @@ mod tests {
|
|||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("not_bold").unwrap();
|
||||
formatter.write_str(" not bold ").unwrap();
|
||||
write!(formatter, " not bold ").unwrap();
|
||||
formatter.push_label("bold_font").unwrap();
|
||||
formatter.write_str(" bold ").unwrap();
|
||||
write!(formatter, " bold ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" not bold again ").unwrap();
|
||||
write!(formatter, " not bold again ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"[4m[38;5;1m[48;5;4m not bold [1m bold [0m[4m[38;5;1m[48;5;4m not bold again [24m[39m[49m");
|
||||
|
@ -829,14 +823,14 @@ mod tests {
|
|||
);
|
||||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.write_str("before").unwrap();
|
||||
write!(formatter, "before").unwrap();
|
||||
formatter.push_label("red").unwrap();
|
||||
formatter.write_str("first").unwrap();
|
||||
write!(formatter, "first").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.push_label("green").unwrap();
|
||||
formatter.write_str("second").unwrap();
|
||||
write!(formatter, "second").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str("after").unwrap();
|
||||
write!(formatter, "after").unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"before[38;5;1mfirst[39m[38;5;2msecond[39mafter");
|
||||
}
|
||||
|
@ -852,9 +846,7 @@ mod tests {
|
|||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("red").unwrap();
|
||||
formatter
|
||||
.write_str("\x1b[1mnot actually bold\x1b[0m")
|
||||
.unwrap();
|
||||
write!(formatter, "\x1b[1mnot actually bold\x1b[0m").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"[38;5;1m␛[1mnot actually bold␛[0m[39m");
|
||||
|
@ -874,15 +866,15 @@ mod tests {
|
|||
);
|
||||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.write_str(" before outer ").unwrap();
|
||||
write!(formatter, " before outer ").unwrap();
|
||||
formatter.push_label("outer").unwrap();
|
||||
formatter.write_str(" before inner ").unwrap();
|
||||
write!(formatter, " before inner ").unwrap();
|
||||
formatter.push_label("inner").unwrap();
|
||||
formatter.write_str(" inside inner ").unwrap();
|
||||
write!(formatter, " inside inner ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" after inner ").unwrap();
|
||||
write!(formatter, " after inner ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" after outer ").unwrap();
|
||||
write!(formatter, " after outer ").unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
|
||||
@" before outer [38;5;4m before inner [38;5;2m inside inner [38;5;4m after inner [39m after outer ");
|
||||
|
@ -899,11 +891,11 @@ mod tests {
|
|||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("outer").unwrap();
|
||||
formatter.write_str(" not colored ").unwrap();
|
||||
write!(formatter, " not colored ").unwrap();
|
||||
formatter.push_label("inner").unwrap();
|
||||
formatter.write_str(" colored ").unwrap();
|
||||
write!(formatter, " colored ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" not colored ").unwrap();
|
||||
write!(formatter, " not colored ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
|
||||
|
@ -958,15 +950,15 @@ mod tests {
|
|||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("outer").unwrap();
|
||||
formatter.write_str("Blue on yellow, ").unwrap();
|
||||
write!(formatter, "Blue on yellow, ").unwrap();
|
||||
formatter.push_label("default_fg").unwrap();
|
||||
formatter.write_str(" default fg, ").unwrap();
|
||||
write!(formatter, " default fg, ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" and back.\nBlue on yellow, ").unwrap();
|
||||
write!(formatter, " and back.\nBlue on yellow, ").unwrap();
|
||||
formatter.push_label("default_bg").unwrap();
|
||||
formatter.write_str(" default bg, ").unwrap();
|
||||
write!(formatter, " default bg, ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" and back.").unwrap();
|
||||
write!(formatter, " and back.").unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
|
||||
@r###"
|
||||
|
@ -988,7 +980,7 @@ mod tests {
|
|||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("outer1").unwrap();
|
||||
formatter.push_label("inner2").unwrap();
|
||||
formatter.write_str(" hello ").unwrap();
|
||||
write!(formatter, " hello ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
drop(formatter);
|
||||
|
@ -1008,7 +1000,7 @@ mod tests {
|
|||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("outer").unwrap();
|
||||
formatter.push_label("inner").unwrap();
|
||||
formatter.write_str(" hello ").unwrap();
|
||||
write!(formatter, " hello ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
drop(formatter);
|
||||
|
@ -1029,15 +1021,15 @@ mod tests {
|
|||
let mut output: Vec<u8> = vec![];
|
||||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("a").unwrap();
|
||||
formatter.write_str(" a1 ").unwrap();
|
||||
write!(formatter, " a1 ").unwrap();
|
||||
formatter.push_label("b").unwrap();
|
||||
formatter.write_str(" b1 ").unwrap();
|
||||
write!(formatter, " b1 ").unwrap();
|
||||
formatter.push_label("c").unwrap();
|
||||
formatter.write_str(" c ").unwrap();
|
||||
write!(formatter, " c ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" b2 ").unwrap();
|
||||
write!(formatter, " b2 ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
formatter.write_str(" a2 ").unwrap();
|
||||
write!(formatter, " a2 ").unwrap();
|
||||
formatter.pop_label().unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
|
||||
|
@ -1057,7 +1049,7 @@ mod tests {
|
|||
let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap();
|
||||
formatter.push_label("outer").unwrap();
|
||||
formatter.push_label("inner").unwrap();
|
||||
formatter.write_str(" inside ").unwrap();
|
||||
write!(formatter, " inside ").unwrap();
|
||||
drop(formatter);
|
||||
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"[38;5;2m inside [39m");
|
||||
}
|
||||
|
@ -1065,12 +1057,12 @@ mod tests {
|
|||
#[test]
|
||||
fn test_format_recorder() {
|
||||
let mut recorder = FormatRecorder::new();
|
||||
recorder.write_str(" outer1 ").unwrap();
|
||||
write!(recorder, " outer1 ").unwrap();
|
||||
recorder.push_label("inner").unwrap();
|
||||
recorder.write_str(" inner1 ").unwrap();
|
||||
recorder.write_str(" inner2 ").unwrap();
|
||||
write!(recorder, " inner1 ").unwrap();
|
||||
write!(recorder, " inner2 ").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
recorder.write_str(" outer2 ").unwrap();
|
||||
write!(recorder, " outer2 ").unwrap();
|
||||
|
||||
insta::assert_snapshot!(
|
||||
str::from_utf8(recorder.data()).unwrap(),
|
||||
|
|
|
@ -313,7 +313,7 @@ pub fn print_failed_git_export(
|
|||
writeln!(ui.warning(), "Failed to export some branches:")?;
|
||||
let mut formatter = ui.stderr_formatter();
|
||||
for FailedRefExport { name, reason } in failed_branches {
|
||||
formatter.write_str(" ")?;
|
||||
write!(formatter, " ")?;
|
||||
write!(formatter.labeled("branch"), "{name}")?;
|
||||
for err in iter::successors(Some(reason as &dyn error::Error), |err| err.source()) {
|
||||
write!(formatter, ": {err}")?;
|
||||
|
|
|
@ -288,7 +288,7 @@ fn builtin_operation_methods() -> OperationTemplateBuildMethodFnMap<Operation> {
|
|||
|
||||
impl Template<()> for OperationId {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(&self.hex())
|
||||
write!(formatter, "{}", self.hex())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -72,20 +72,20 @@ impl Template<()> for Signature {
|
|||
|
||||
impl Template<()> for String {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(self)
|
||||
write!(formatter, "{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl Template<()> for &str {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(self)
|
||||
write!(formatter, "{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl Template<()> for Timestamp {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
match time_util::format_absolute_timestamp(self) {
|
||||
Ok(formatted) => formatter.write_str(&formatted),
|
||||
Ok(formatted) => write!(formatter, "{formatted}"),
|
||||
Err(err) => format_error_inline(formatter, &err),
|
||||
}
|
||||
}
|
||||
|
@ -129,7 +129,8 @@ impl Template<()> for Vec<String> {
|
|||
|
||||
impl Template<()> for bool {
|
||||
fn format(&self, _: &(), formatter: &mut dyn Formatter) -> io::Result<()> {
|
||||
formatter.write_str(if *self { "true" } else { "false" })
|
||||
let repr = if *self { "true" } else { "false" };
|
||||
write!(formatter, "{repr}")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -261,6 +261,8 @@ pub fn write_wrapped(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Write as _;
|
||||
|
||||
use super::*;
|
||||
use crate::formatter::{ColorFormatter, PlainTextFormatter};
|
||||
|
||||
|
@ -495,7 +497,7 @@ mod tests {
|
|||
// Split single label chunk
|
||||
let mut recorder = FormatRecorder::new();
|
||||
recorder.push_label("red").unwrap();
|
||||
recorder.write_str("foo bar baz\nqux quux\n").unwrap();
|
||||
write!(recorder, "foo bar baz\nqux quux\n").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
insta::assert_snapshot!(
|
||||
format_colored(|formatter| write_wrapped(formatter, &recorder, 7)),
|
||||
|
@ -511,7 +513,7 @@ mod tests {
|
|||
let mut recorder = FormatRecorder::new();
|
||||
for (i, word) in ["foo ", "bar ", "baz\n", "qux ", "quux"].iter().enumerate() {
|
||||
recorder.push_label(["red", "cyan"][i & 1]).unwrap();
|
||||
recorder.write_str(word).unwrap();
|
||||
write!(recorder, "{word}").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
}
|
||||
insta::assert_snapshot!(
|
||||
|
@ -528,8 +530,7 @@ mod tests {
|
|||
let mut recorder = FormatRecorder::new();
|
||||
for (i, word) in ["", "foo", "", "bar baz", ""].iter().enumerate() {
|
||||
recorder.push_label(["red", "cyan"][i & 1]).unwrap();
|
||||
recorder.write_str(word).unwrap();
|
||||
recorder.write_str("\n").unwrap();
|
||||
writeln!(recorder, "{word}").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
}
|
||||
insta::assert_snapshot!(
|
||||
|
@ -546,11 +547,11 @@ mod tests {
|
|||
// Split at label boundary
|
||||
let mut recorder = FormatRecorder::new();
|
||||
recorder.push_label("red").unwrap();
|
||||
recorder.write_str("foo bar").unwrap();
|
||||
write!(recorder, "foo bar").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
recorder.write_str(" ").unwrap();
|
||||
write!(recorder, " ").unwrap();
|
||||
recorder.push_label("cyan").unwrap();
|
||||
recorder.write_str("baz\n").unwrap();
|
||||
writeln!(recorder, "baz").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
insta::assert_snapshot!(
|
||||
format_colored(|formatter| write_wrapped(formatter, &recorder, 10)),
|
||||
|
@ -563,10 +564,10 @@ mod tests {
|
|||
// Do not split at label boundary "ba|z" (since it's a single word)
|
||||
let mut recorder = FormatRecorder::new();
|
||||
recorder.push_label("red").unwrap();
|
||||
recorder.write_str("foo bar ba").unwrap();
|
||||
write!(recorder, "foo bar ba").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
recorder.push_label("cyan").unwrap();
|
||||
recorder.write_str("z\n").unwrap();
|
||||
writeln!(recorder, "z").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
insta::assert_snapshot!(
|
||||
format_colored(|formatter| write_wrapped(formatter, &recorder, 10)),
|
||||
|
@ -581,9 +582,9 @@ mod tests {
|
|||
fn test_write_wrapped_leading_labeled_whitespace() {
|
||||
let mut recorder = FormatRecorder::new();
|
||||
recorder.push_label("red").unwrap();
|
||||
recorder.write_str(" ").unwrap();
|
||||
write!(recorder, " ").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
recorder.write_str("foo").unwrap();
|
||||
write!(recorder, "foo").unwrap();
|
||||
insta::assert_snapshot!(
|
||||
format_colored(|formatter| write_wrapped(formatter, &recorder, 10)),
|
||||
@"[38;5;1m [39mfoo"
|
||||
|
@ -595,9 +596,9 @@ mod tests {
|
|||
// data: "foo" " "
|
||||
// line: ---
|
||||
let mut recorder = FormatRecorder::new();
|
||||
recorder.write_str("foo").unwrap();
|
||||
write!(recorder, "foo").unwrap();
|
||||
recorder.push_label("red").unwrap();
|
||||
recorder.write_str(" ").unwrap();
|
||||
write!(recorder, " ").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
assert_eq!(
|
||||
format_plain_text(|formatter| write_wrapped(formatter, &recorder, 10)),
|
||||
|
@ -607,9 +608,9 @@ mod tests {
|
|||
// data: "foo" "\n"
|
||||
// line: --- -
|
||||
let mut recorder = FormatRecorder::new();
|
||||
recorder.write_str("foo").unwrap();
|
||||
write!(recorder, "foo").unwrap();
|
||||
recorder.push_label("red").unwrap();
|
||||
recorder.write_str("\n").unwrap();
|
||||
writeln!(recorder).unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
assert_eq!(
|
||||
format_plain_text(|formatter| write_wrapped(formatter, &recorder, 10)),
|
||||
|
@ -619,9 +620,9 @@ mod tests {
|
|||
// data: "foo\n" " "
|
||||
// line: --- -
|
||||
let mut recorder = FormatRecorder::new();
|
||||
recorder.write_str("foo\n").unwrap();
|
||||
writeln!(recorder, "foo").unwrap();
|
||||
recorder.push_label("red").unwrap();
|
||||
recorder.write_str(" ").unwrap();
|
||||
write!(recorder, " ").unwrap();
|
||||
recorder.pop_label().unwrap();
|
||||
assert_eq!(
|
||||
format_plain_text(|formatter| write_wrapped(formatter, &recorder, 10)),
|
||||
|
|
Loading…
Reference in a new issue