diff options
-rw-r--r-- | src/cli.rs | 5 | ||||
-rw-r--r-- | src/config.rs | 2 | ||||
-rw-r--r-- | src/delta.rs | 8 | ||||
-rw-r--r-- | src/options/set.rs | 1 | ||||
-rw-r--r-- | src/parse.rs | 7 |
5 files changed, 19 insertions, 4 deletions
@@ -482,6 +482,11 @@ pub struct Opt { #[structopt(short = "w", long = "width")] pub width: Option<String>, + /// Width allocated for file paths in a diff stat section. If a relativized + /// file path exceeds this width then the diff stat will be misaligned. + #[structopt(long = "diff-stat-align-width", default_value = "48")] + pub diff_stat_align_width: usize, + /// The number of spaces to replace tab characters with. Use --tabs=0 to pass tab characters /// through directly, but note that in that case delta will calculate line widths assuming tabs /// occupy one character's width on the screen: if your terminal renders tabs as more than than diff --git a/src/config.rs b/src/config.rs index c6719839..6d949dff 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,7 @@ pub struct Config { pub color_only: bool, pub cwd_relative_to_repo_root: Option<String>, pub decorations_width: cli::Width, + pub diff_stat_align_width: usize, pub error_exit_code: i32, pub file_added_label: String, pub file_copied_label: String, @@ -185,6 +186,7 @@ impl From<cli::Opt> for Config { color_only: opt.color_only, cwd_relative_to_repo_root: std::env::var("GIT_PREFIX").ok(), decorations_width: opt.computed.decorations_width, + diff_stat_align_width: opt.diff_stat_align_width, error_exit_code: 2, // Use 2 for error because diff uses 0 and 1 for non-error. file_added_label, file_copied_label, diff --git a/src/delta.rs b/src/delta.rs index 869bd4a4..608b89c9 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -231,9 +231,11 @@ impl<'a> StateMachine<'a> { fn handle_diff_stat_line(&mut self) -> std::io::Result<bool> { let mut handled_line = false; if let Some(cwd) = self.config.cwd_relative_to_repo_root.as_deref() { - if let Some(replacement_line) = - parse::relativize_path_in_diff_stat_line(&self.raw_line, cwd) - { + if let Some(replacement_line) = parse::relativize_path_in_diff_stat_line( + &self.raw_line, + cwd, + self.config.diff_stat_align_width, + ) { self.painter.emit()?; writeln!(self.painter.writer, "{}", replacement_line)?; handled_line = true diff --git a/src/options/set.rs b/src/options/set.rs index 2d112f87..8373e86b 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -128,6 +128,7 @@ pub fn set_options( color_only, commit_decoration_style, commit_style, + diff_stat_align_width, file_added_label, file_copied_label, file_decoration_style, diff --git a/src/parse.rs b/src/parse.rs index 65ad72f1..a90323c3 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -85,6 +85,7 @@ lazy_static! { pub fn relativize_path_in_diff_stat_line( line: &str, cwd_relative_to_repo_root: &str, + diff_stat_align_width: usize, ) -> Option<String> { if let Some(caps) = DIFF_STAT_LINE_REGEX.captures(line) { let path_relative_to_repo_root = caps.get(1).unwrap().as_str(); @@ -93,7 +94,11 @@ pub fn relativize_path_in_diff_stat_line( { if let Some(relative_path) = relative_path.to_str() { let suffix = caps.get(2).unwrap().as_str(); - return Some(format!(" {:<30}{}", relative_path, suffix,)); + let pad_width = diff_stat_align_width + .checked_sub(relative_path.len()) + .unwrap_or(0); + let padding = " ".repeat(pad_width); + return Some(format!(" {}{}{}", relative_path, padding, suffix)); } } } |