From 9923bab3baa2f80220b76eb39f26fdc8d0fe94dc Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 28 Mar 2022 17:59:08 +0900 Subject: [PATCH] diff: pass formatter to show_diff() as argument We might want to split show_diff() into config/option handling part and diff displayer function, but I'm not sure. Since some of the show_diff functions depends on ui, we can't isolate show_diff() from the ui object anyway. let opts = parse_diff_option(ui, args); // map config/option to diff opts show_diff(ui, formatter, opts, ...); // would be nice if ui could be removed --- src/commands.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index f9e99c6ea..7fbc1b991 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1993,7 +1993,14 @@ fn cmd_diff(ui: &mut Ui, command: &CommandHelper, args: &DiffArgs) -> Result<(), let workspace_root = workspace_command.workspace_root(); let matcher = matcher_from_values(ui, workspace_root, &args.paths)?; let diff_iterator = from_tree.diff(&to_tree, matcher.as_ref()); - show_diff(ui, repo, workspace_root, &args.format, diff_iterator)?; + show_diff( + ui, + ui.stdout_formatter().as_mut(), + repo, + workspace_root, + &args.format, + diff_iterator, + )?; Ok(()) } @@ -2023,13 +2030,23 @@ fn cmd_show(ui: &mut Ui, command: &CommandHelper, args: &ShowArgs) -> Result<(), &workspace_command.workspace_id(), template_string, ); - template.format(&commit, ui.stdout_formatter().as_mut())?; - show_diff(ui, repo, workspace_root, &args.format, diff_iterator)?; + let mut formatter = ui.stdout_formatter(); + let formatter = formatter.as_mut(); + template.format(&commit, formatter)?; + show_diff( + ui, + formatter, + repo, + workspace_root, + &args.format, + diff_iterator, + )?; Ok(()) } fn show_diff( - ui: &mut Ui, + ui: &Ui, + formatter: &mut dyn Formatter, repo: &Arc, workspace_root: &Path, args: &DiffFormat, @@ -2056,8 +2073,6 @@ fn show_diff( } } }; - let mut formatter = ui.stdout_formatter(); - let formatter = formatter.as_mut(); match format { Format::Summary => { show_diff_summary(ui, formatter, workspace_root, tree_diff)?;