From 32e3a871351d7bf742a64cbd4a556714b5e3d92b Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 12 Jan 2023 15:46:41 +0900 Subject: [PATCH] cli: add formatter.labeled() for short --- src/cli_util.rs | 2 +- src/commands.rs | 131 ++++++++++++++++++++++++----------------------- src/diff_util.rs | 88 +++++++++++++++---------------- src/formatter.rs | 7 +++ 4 files changed, 117 insertions(+), 111 deletions(-) diff --git a/src/cli_util.rs b/src/cli_util.rs index 06fd3a43d..ae55be670 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -1087,7 +1087,7 @@ pub fn print_failed_git_export( let mut formatter = ui.stderr_formatter(); for branch_name in failed_branches { formatter.write_str(" ")?; - formatter.with_label("branch", |formatter| formatter.write_str(branch_name))?; + write!(formatter.labeled("branch"), "{branch_name}")?; formatter.write_str("\n")?; } drop(formatter); diff --git a/src/commands.rs b/src/commands.rs index 76c78a162..a8d4908e3 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1534,12 +1534,13 @@ fn cmd_status( } } if !conflicted_local_branches.is_empty() { - formatter.with_label("conflict", |formatter| { - writeln!(formatter, "These branches have conflicts:") - })?; + writeln!( + formatter.labeled("conflict"), + "These branches have conflicts:" + )?; for branch_name in conflicted_local_branches { write!(formatter, " ")?; - formatter.with_label("branch", |formatter| write!(formatter, "{branch_name}"))?; + write!(formatter.labeled("branch"), "{branch_name}")?; writeln!(formatter)?; } writeln!( @@ -1549,14 +1550,13 @@ fn cmd_status( )?; } if !conflicted_remote_branches.is_empty() { - formatter.with_label("conflict", |formatter| { - writeln!(formatter, "These remote branches have conflicts:") - })?; + writeln!( + formatter.labeled("conflict"), + "These remote branches have conflicts:" + )?; for (branch_name, remote_name) in conflicted_remote_branches { write!(formatter, " ")?; - formatter.with_label("branch", |formatter| { - write!(formatter, "{branch_name}@{remote_name}") - })?; + write!(formatter.labeled("branch"), "{branch_name}@{remote_name}")?; writeln!(formatter)?; } writeln!( @@ -1581,9 +1581,10 @@ fn cmd_status( let conflicts = tree.conflicts(); if !conflicts.is_empty() { - formatter.with_label("conflict", |formatter| { - writeln!(formatter, "There are unresolved conflicts at these paths:") - })?; + writeln!( + formatter.labeled("conflict"), + "There are unresolved conflicts at these paths:" + )?; print_conflicted_paths(&conflicts, &tree, formatter, &workspace_command)? } } @@ -3260,29 +3261,12 @@ fn list_branches( let repo = workspace_command.repo(); let workspace_id = workspace_command.workspace_id(); - let print_branch_target = |formatter: &mut dyn Formatter, - target: Option<&RefTarget>| - -> Result<(), CommandError> { - match target { - Some(RefTarget::Normal(id)) => { - write!(formatter, ": ")?; - let commit = repo.store().get_commit(id)?; - write_commit_summary( - formatter, - repo.as_repo_ref(), - &workspace_id, - &commit, - command.settings(), - )?; - writeln!(formatter)?; - } - Some(RefTarget::Conflict { adds, removes }) => { - write!(formatter, " ")?; - formatter.with_label("conflict", |formatter| write!(formatter, "(conflicted)"))?; - writeln!(formatter, ":")?; - for id in removes { + let print_branch_target = + |formatter: &mut dyn Formatter, target: Option<&RefTarget>| -> Result<(), CommandError> { + match target { + Some(RefTarget::Normal(id)) => { + write!(formatter, ": ")?; let commit = repo.store().get_commit(id)?; - write!(formatter, " - ")?; write_commit_summary( formatter, repo.as_repo_ref(), @@ -3292,31 +3276,47 @@ fn list_branches( )?; writeln!(formatter)?; } - for id in adds { - let commit = repo.store().get_commit(id)?; - write!(formatter, " + ")?; - write_commit_summary( - formatter, - repo.as_repo_ref(), - &workspace_id, - &commit, - command.settings(), - )?; - writeln!(formatter)?; + Some(RefTarget::Conflict { adds, removes }) => { + write!(formatter, " ")?; + write!(formatter.labeled("conflict"), "(conflicted)")?; + writeln!(formatter, ":")?; + for id in removes { + let commit = repo.store().get_commit(id)?; + write!(formatter, " - ")?; + write_commit_summary( + formatter, + repo.as_repo_ref(), + &workspace_id, + &commit, + command.settings(), + )?; + writeln!(formatter)?; + } + for id in adds { + let commit = repo.store().get_commit(id)?; + write!(formatter, " + ")?; + write_commit_summary( + formatter, + repo.as_repo_ref(), + &workspace_id, + &commit, + command.settings(), + )?; + writeln!(formatter)?; + } + } + None => { + writeln!(formatter, " (deleted)")?; } } - None => { - writeln!(formatter, " (deleted)")?; - } - } - Ok(()) - }; + Ok(()) + }; let mut formatter = ui.stdout_formatter(); let formatter = formatter.as_mut(); let index = repo.index(); for (name, branch_target) in repo.view().branches() { - formatter.with_label("branch", |formatter| write!(formatter, "{name}"))?; + write!(formatter.labeled("branch"), "{name}")?; print_branch_target(formatter, branch_target.local_target.as_ref())?; for (remote, remote_target) in branch_target @@ -3328,7 +3328,7 @@ fn list_branches( continue; } write!(formatter, " ")?; - formatter.with_label("branch", |formatter| write!(formatter, "@{remote}"))?; + write!(formatter.labeled("branch"), "@{remote}")?; if let Some(local_target) = branch_target.local_target.as_ref() { let remote_ahead_count = index .walk_revs(&remote_target.adds(), &local_target.adds()) @@ -3463,12 +3463,15 @@ fn cmd_op_log( impl Template for OpTemplate { fn format(&self, op: &Operation, formatter: &mut dyn Formatter) -> io::Result<()> { // TODO: Make this templated - formatter.with_label("id", |formatter| formatter.write_str(&op.id().hex()[0..12]))?; + write!(formatter.labeled("id"), "{}", &op.id().hex()[0..12])?; formatter.write_str(" ")?; let metadata = &op.store_operation().metadata; - formatter.with_label("user", |formatter| { - formatter.write_str(&format!("{}@{}", metadata.username, metadata.hostname)) - })?; + write!( + formatter.labeled("user"), + "{}@{}", + metadata.username, + metadata.hostname + )?; formatter.write_str(" ")?; formatter.with_label("time", |formatter| { formatter.write_str( @@ -3492,13 +3495,13 @@ fn cmd_op_log( ) })?; formatter.write_str("\n")?; - formatter.with_label("description", |formatter| { - formatter.write_str(&metadata.description) - })?; + write!( + formatter.labeled("description"), + "{}", + &metadata.description + )?; for (key, value) in &metadata.tags { - formatter.with_label("tags", |formatter| { - formatter.write_str(&format!("\n{key}: {value}")) - })?; + write!(formatter.labeled("tags"), "\n{key}: {value}")?; } Ok(()) } diff --git a/src/diff_util.rs b/src/diff_util.rs index a159503cb..aef0de575 100644 --- a/src/diff_util.rs +++ b/src/diff_util.rs @@ -216,17 +216,21 @@ fn show_color_words_diff_line( diff_line: &DiffLine, ) -> io::Result<()> { if diff_line.has_left_content { - formatter.with_label("removed", |formatter| { - formatter.write_bytes(format!("{:>4}", diff_line.left_line_number).as_bytes()) - })?; + write!( + formatter.labeled("removed"), + "{:>4}", + diff_line.left_line_number + )?; formatter.write_bytes(b" ")?; } else { formatter.write_bytes(b" ")?; } if diff_line.has_right_content { - formatter.with_label("added", |formatter| { - formatter.write_bytes(format!("{:>4}", diff_line.right_line_number).as_bytes()) - })?; + write!( + formatter.labeled("added"), + "{:>4}", + diff_line.right_line_number + )?; formatter.write_bytes(b": ")?; } else { formatter.write_bytes(b" : ")?; @@ -315,9 +319,10 @@ pub fn show_color_words_diff( tree::Diff::Added(right_value) => { let right_content = diff_content(repo, &path, &right_value)?; let description = basic_diff_file_type(&right_value); - formatter.with_label("header", |formatter| { - formatter.write_str(&format!("Added {description} {ui_path}:\n")) - })?; + writeln!( + formatter.labeled("header"), + "Added {description} {ui_path}:" + )?; show_color_words_diff_hunks(&[], &right_content, formatter)?; } tree::Diff::Modified(left_value, right_value) => { @@ -364,17 +369,16 @@ pub fn show_color_words_diff( ) } }; - formatter.with_label("header", |formatter| { - formatter.write_str(&format!("{description} {ui_path}:\n")) - })?; + writeln!(formatter.labeled("header"), "{description} {ui_path}:")?; show_color_words_diff_hunks(&left_content, &right_content, formatter)?; } tree::Diff::Removed(left_value) => { let left_content = diff_content(repo, &path, &left_value)?; let description = basic_diff_file_type(&left_value); - formatter.with_label("header", |formatter| { - formatter.write_str(&format!("Removed {description} {ui_path}:\n")) - })?; + writeln!( + formatter.labeled("header"), + "Removed {description} {ui_path}:" + )?; show_color_words_diff_hunks(&left_content, &[], formatter)?; } } @@ -539,16 +543,14 @@ fn show_unified_diff_hunks( right_content: &[u8], ) -> Result<(), CommandError> { for hunk in unified_diff_hunks(left_content, right_content, 3) { - formatter.with_label("hunk_header", |formatter| { - writeln!( - formatter, - "@@ -{},{} +{},{} @@", - hunk.left_line_range.start, - hunk.left_line_range.len(), - hunk.right_line_range.start, - hunk.right_line_range.len() - ) - })?; + writeln!( + formatter.labeled("hunk_header"), + "@@ -{},{} +{},{} @@", + hunk.left_line_range.start, + hunk.left_line_range.len(), + hunk.right_line_range.start, + hunk.right_line_range.len() + )?; for (line_type, content) in hunk.lines { match line_type { DiffLineType::Context => { @@ -651,31 +653,25 @@ pub fn show_diff_summary( for (repo_path, diff) in tree_diff { match diff { tree::Diff::Modified(_, _) => { - formatter.with_label("modified", |formatter| { - writeln!( - formatter, - "M {}", - workspace_command.format_file_path(&repo_path) - ) - })?; + writeln!( + formatter.labeled("modified"), + "M {}", + workspace_command.format_file_path(&repo_path) + )?; } tree::Diff::Added(_) => { - formatter.with_label("added", |formatter| { - writeln!( - formatter, - "A {}", - workspace_command.format_file_path(&repo_path) - ) - })?; + writeln!( + formatter.labeled("added"), + "A {}", + workspace_command.format_file_path(&repo_path) + )?; } tree::Diff::Removed(_) => { - formatter.with_label("removed", |formatter| { - writeln!( - formatter, - "R {}", - workspace_command.format_file_path(&repo_path) - ) - })?; + writeln!( + formatter.labeled("removed"), + "R {}", + workspace_command.format_file_path(&repo_path) + )?; } } } diff --git a/src/formatter.rs b/src/formatter.rs index 880ef128e..702c20dbb 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -34,6 +34,13 @@ pub trait Formatter: Write { } impl dyn Formatter + '_ { + pub fn labeled>(&mut self, label: S) -> LabeledWriter<&mut Self, S> { + LabeledWriter { + formatter: self, + label, + } + } + pub fn with_label( &mut self, label: &str,