formatter: rename {add,remove}_label to {push,pop}_label

The labels behave like a stack, and these names hopefully help clarify
that.
This commit is contained in:
Martin von Zweigbergk 2023-01-12 00:00:12 -08:00 committed by Martin von Zweigbergk
parent 25422b2abd
commit d33721d6fb
4 changed files with 75 additions and 75 deletions

View file

@ -1669,7 +1669,7 @@ fn cmd_log(ui: &mut Ui, command: &CommandHelper, args: &LogArgs) -> Result<(), C
ui.request_pager();
let mut formatter = ui.stdout_formatter();
let mut formatter = formatter.as_mut();
formatter.add_label("log")?;
formatter.push_label("log")?;
if !args.no_graph {
let mut graph = AsciiGraphDrawer::new(&mut formatter);
@ -1802,7 +1802,7 @@ fn cmd_obslog(ui: &mut Ui, command: &CommandHelper, args: &ObslogArgs) -> Result
ui.request_pager();
let mut formatter = ui.stdout_formatter();
let mut formatter = formatter.as_mut();
formatter.add_label("log")?;
formatter.push_label("log")?;
let commits = topo_order_reverse(
vec![start_commit],

View file

@ -312,7 +312,7 @@ pub fn show_color_words_diff(
tree_diff: TreeDiffIterator,
) -> Result<(), CommandError> {
let repo = workspace_command.repo();
formatter.add_label("diff")?;
formatter.push_label("diff")?;
for (path, diff) in tree_diff {
let ui_path = workspace_command.format_file_path(&path);
match diff {
@ -383,7 +383,7 @@ pub fn show_color_words_diff(
}
}
}
formatter.remove_label()?;
formatter.pop_label()?;
Ok(())
}
@ -586,7 +586,7 @@ pub fn show_git_diff(
tree_diff: TreeDiffIterator,
) -> Result<(), CommandError> {
let repo = workspace_command.repo();
formatter.add_label("diff")?;
formatter.push_label("diff")?;
for (path, diff) in tree_diff {
let path_string = path.to_internal_file_string();
match diff {
@ -640,7 +640,7 @@ pub fn show_git_diff(
}
}
}
formatter.remove_label()?;
formatter.pop_label()?;
Ok(())
}

View file

@ -32,9 +32,9 @@ pub trait Formatter: Write {
self.write_all(text.as_bytes())
}
fn add_label(&mut self, label: &str) -> io::Result<()>;
fn push_label(&mut self, label: &str) -> io::Result<()>;
fn remove_label(&mut self) -> io::Result<()>;
fn pop_label(&mut self) -> io::Result<()>;
}
impl dyn Formatter + '_ {
@ -50,10 +50,10 @@ impl dyn Formatter + '_ {
label: &str,
write_inner: impl FnOnce(&mut dyn Formatter) -> io::Result<()>,
) -> io::Result<()> {
self.add_label(label)?;
// Call `remove_label()` whether or not `write_inner()` fails, but don't let
self.push_label(label)?;
// Call `pop_label()` whether or not `write_inner()` fails, but don't let
// its error replace the one from `write_inner()`.
write_inner(self).and(self.remove_label())
write_inner(self).and(self.pop_label())
}
}
@ -141,11 +141,11 @@ impl<W: Write> Write for PlainTextFormatter<W> {
}
impl<W: Write> Formatter for PlainTextFormatter<W> {
fn add_label(&mut self, _label: &str) -> io::Result<()> {
fn push_label(&mut self, _label: &str) -> io::Result<()> {
Ok(())
}
fn remove_label(&mut self) -> io::Result<()> {
fn pop_label(&mut self) -> io::Result<()> {
Ok(())
}
}
@ -352,12 +352,12 @@ impl<W: Write> Write for ColorFormatter<W> {
}
impl<W: Write> Formatter for ColorFormatter<W> {
fn add_label(&mut self, label: &str) -> io::Result<()> {
fn push_label(&mut self, label: &str) -> io::Result<()> {
self.labels.push(label.to_owned());
self.write_new_style()
}
fn remove_label(&mut self) -> io::Result<()> {
fn pop_label(&mut self) -> io::Result<()> {
self.labels.pop();
self.write_new_style()
}
@ -379,9 +379,9 @@ mod tests {
// Test that PlainTextFormatter ignores labels.
let mut output: Vec<u8> = vec![];
let mut formatter = PlainTextFormatter::new(&mut output);
formatter.add_label("warning").unwrap();
formatter.push_label("warning").unwrap();
formatter.write_str("hello").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"hello");
}
@ -417,9 +417,9 @@ mod tests {
let mut formatter =
ColorFormatter::for_config(&mut output, &config_builder.build().unwrap());
for color in colors {
formatter.add_label(&color.replace(' ', "-")).unwrap();
formatter.push_label(&color.replace(' ', "-")).unwrap();
formatter.write_str(&format!(" {color} ")).unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str("\n").unwrap();
}
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @r###"
@ -454,9 +454,9 @@ mod tests {
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.write_str(" before ").unwrap();
formatter.add_label("inside").unwrap();
formatter.push_label("inside").unwrap();
formatter.write_str(" inside ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str(" after ").unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @" before  inside  after ");
}
@ -476,31 +476,31 @@ mod tests {
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("red_fg").unwrap();
formatter.push_label("red_fg").unwrap();
formatter.write_str(" fg only ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str("\n").unwrap();
formatter.add_label("blue_bg").unwrap();
formatter.push_label("blue_bg").unwrap();
formatter.write_str(" bg only ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str("\n").unwrap();
formatter.add_label("bold_font").unwrap();
formatter.push_label("bold_font").unwrap();
formatter.write_str(" bold only ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str("\n").unwrap();
formatter.add_label("underlined_text").unwrap();
formatter.push_label("underlined_text").unwrap();
formatter.write_str(" underlined only ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str("\n").unwrap();
formatter.add_label("multiple").unwrap();
formatter.push_label("multiple").unwrap();
formatter.write_str(" single rule ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str("\n").unwrap();
formatter.add_label("red_fg").unwrap();
formatter.add_label("blue_bg").unwrap();
formatter.push_label("red_fg").unwrap();
formatter.push_label("blue_bg").unwrap();
formatter.write_str(" two rules ").unwrap();
formatter.remove_label().unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str("\n").unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @r###"
 fg only 
@ -523,13 +523,13 @@ mod tests {
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("not_bold").unwrap();
formatter.push_label("not_bold").unwrap();
formatter.write_str(" not bold ").unwrap();
formatter.add_label("bold_font").unwrap();
formatter.push_label("bold_font").unwrap();
formatter.write_str(" bold ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str(" not bold again ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @" not bold  bold  not bold again ");
}
@ -545,12 +545,12 @@ mod tests {
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.write_str("before").unwrap();
formatter.add_label("red").unwrap();
formatter.push_label("red").unwrap();
formatter.write_str("first").unwrap();
formatter.remove_label().unwrap();
formatter.add_label("green").unwrap();
formatter.pop_label().unwrap();
formatter.push_label("green").unwrap();
formatter.write_str("second").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str("after").unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"beforefirstsecondafter");
}
@ -565,11 +565,11 @@ mod tests {
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("red").unwrap();
formatter.push_label("red").unwrap();
formatter
.write_str("\x1b[1mnot actually bold\x1b[0m")
.unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
// TODO: Replace the ANSI escape (\x1b) by something else (🌈?)
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @"not actually bold");
}
@ -589,13 +589,13 @@ mod tests {
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.write_str(" before outer ").unwrap();
formatter.add_label("outer").unwrap();
formatter.push_label("outer").unwrap();
formatter.write_str(" before inner ").unwrap();
formatter.add_label("inner").unwrap();
formatter.push_label("inner").unwrap();
formatter.write_str(" inside inner ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str(" after inner ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str(" after outer ").unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
@" before outer  before inner  inside inner  after inner  after outer ");
@ -611,13 +611,13 @@ mod tests {
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("outer").unwrap();
formatter.push_label("outer").unwrap();
formatter.write_str(" not colored ").unwrap();
formatter.add_label("inner").unwrap();
formatter.push_label("inner").unwrap();
formatter.write_str(" colored ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str(" not colored ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
@" not colored  colored  not colored ");
}
@ -633,13 +633,13 @@ mod tests {
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("outer").unwrap();
formatter.push_label("outer").unwrap();
formatter.write_str(" red before ").unwrap();
formatter.add_label("inner").unwrap();
formatter.push_label("inner").unwrap();
formatter.write_str(" still red inside ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str(" also red afterwards ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
@" red before still red inside also red afterwards ");
}
@ -655,11 +655,11 @@ mod tests {
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("outer1").unwrap();
formatter.add_label("inner2").unwrap();
formatter.push_label("outer1").unwrap();
formatter.push_label("inner2").unwrap();
formatter.write_str(" hello ").unwrap();
formatter.remove_label().unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.pop_label().unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
@" hello ");
}
@ -674,11 +674,11 @@ mod tests {
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("outer").unwrap();
formatter.add_label("inner").unwrap();
formatter.push_label("outer").unwrap();
formatter.push_label("inner").unwrap();
formatter.write_str(" hello ").unwrap();
formatter.remove_label().unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.pop_label().unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(), @" hello ");
}
@ -695,17 +695,17 @@ mod tests {
);
let mut output: Vec<u8> = vec![];
let mut formatter = ColorFormatter::for_config(&mut output, &config);
formatter.add_label("a").unwrap();
formatter.push_label("a").unwrap();
formatter.write_str(" a1 ").unwrap();
formatter.add_label("b").unwrap();
formatter.push_label("b").unwrap();
formatter.write_str(" b1 ").unwrap();
formatter.add_label("c").unwrap();
formatter.push_label("c").unwrap();
formatter.write_str(" c ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str(" b2 ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
formatter.write_str(" a2 ").unwrap();
formatter.remove_label().unwrap();
formatter.pop_label().unwrap();
insta::assert_snapshot!(String::from_utf8(output).unwrap(),
@" a1  b1  c  b2  a2 ");
}

View file

@ -72,11 +72,11 @@ impl<'a, C> LabelTemplate<'a, C> {
impl<'a, C> Template<C> for LabelTemplate<'a, C> {
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
for label in &self.labels {
formatter.add_label(label)?;
formatter.push_label(label)?;
}
self.content.format(context, formatter)?;
for _label in &self.labels {
formatter.remove_label()?;
formatter.pop_label()?;
}
Ok(())
}
@ -106,11 +106,11 @@ impl<'a, C> Template<C> for DynamicLabelTemplate<'a, C> {
fn format(&self, context: &C, formatter: &mut dyn Formatter) -> io::Result<()> {
let labels = self.label_property.as_ref()(context);
for label in &labels {
formatter.add_label(label)?;
formatter.push_label(label)?;
}
self.content.format(context, formatter)?;
for _label in &labels {
formatter.remove_label()?;
formatter.pop_label()?;
}
Ok(())
}