summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2021-04-20 22:06:37 -0400
committerDan Davison <dandavison7@gmail.com>2021-04-24 16:43:24 -0400
commit717a63ac9051694efeeccd1c619ac984f1259142 (patch)
treedbf2e33545c1f1e55047a5e5fd29aacf9f817482
parent6630202259cf984a8b20d7cb76086986a107390c (diff)
Add --diff-stat-align-width to control alignment of relativized paths
-rw-r--r--src/cli.rs5
-rw-r--r--src/config.rs2
-rw-r--r--src/delta.rs8
-rw-r--r--src/options/set.rs1
-rw-r--r--src/parse.rs7
5 files changed, 19 insertions, 4 deletions
diff --git a/src/cli.rs b/src/cli.rs
index ca465454..4109269a 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -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));
}
}
}