Allow users to override the tab width

Fixes #274
pass_end_node
Wilfred Hughes 2022-04-28 20:47:04 +07:00
parent 72eba2d426
commit e1cbdc1478
6 changed files with 52 additions and 3 deletions

@ -11,6 +11,10 @@ names are recognised as JSON, e.g. `.jsonl`.
Fixed crash in inline mode.
Added an option `--tab-width` that controls how many spaces are used
to display tabs. The default value is 8, consistent with older
versions.
### Diffing
Difftastic now diffs files in parallel when diffing whole directories,

@ -145,6 +145,9 @@ eeab25a68552f051a6392b5e713fbd29 -
sample_files/syntax_error_before.js sample_files/syntax_error_after.js
fe636ad27b1aa75e0b153dfe248023bb -
sample_files/tab_before.c sample_files/tab_after.c
36ba3231eeba6f0b67a6be9db454de19 -
sample_files/text_before.txt sample_files/text_after.txt
dfc3495b8d5931029b479f0c878a3219 -

@ -0,0 +1,6 @@
#include <stdio.h>
int main() {
printf("Goodbye World");
return 0;
}

@ -0,0 +1,6 @@
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}

@ -142,6 +142,7 @@ fn main() {
language_override,
lhs_path,
rhs_path,
tab_width,
..
} => {
let use_color = should_use_color(color_output);
@ -164,6 +165,7 @@ fn main() {
diff_directories(
lhs_path,
rhs_path,
tab_width,
node_limit,
byte_limit,
language_override,
@ -183,6 +185,7 @@ fn main() {
&display_path,
lhs_path,
rhs_path,
tab_width,
missing_as_empty,
node_limit,
byte_limit,
@ -206,6 +209,7 @@ fn diff_file(
display_path: &str,
lhs_path: &Path,
rhs_path: &Path,
tab_width: usize,
missing_as_empty: bool,
node_limit: u32,
byte_limit: usize,
@ -216,6 +220,7 @@ fn diff_file(
display_path,
&lhs_bytes,
&rhs_bytes,
tab_width,
node_limit,
byte_limit,
language_override,
@ -226,6 +231,7 @@ fn diff_file_content(
display_path: &str,
lhs_bytes: &[u8],
rhs_bytes: &[u8],
tab_width: usize,
node_limit: u32,
byte_limit: usize,
language_override: Option<guess_language::Language>,
@ -242,12 +248,13 @@ fn diff_file_content(
}
// TODO: don't replace tab characters inside string literals.
let tab_as_spaces = " ".repeat(tab_width);
let mut lhs_src = String::from_utf8_lossy(lhs_bytes)
.to_string()
.replace('\t', " ");
.replace('\t', &tab_as_spaces);
let mut rhs_src = String::from_utf8_lossy(rhs_bytes)
.to_string()
.replace('\t', " ");
.replace('\t', &tab_as_spaces);
// Ignore the trailing newline, if present.
// TODO: highlight if this has changes (#144).
@ -371,6 +378,7 @@ fn diff_file_content(
fn diff_directories<'a>(
lhs_dir: &'a Path,
rhs_dir: &'a Path,
tab_width: usize,
node_limit: u32,
byte_limit: usize,
language_override: Option<guess_language::Language>,
@ -390,6 +398,7 @@ fn diff_directories<'a>(
&rel_path.to_string_lossy(),
&lhs_path,
&rhs_path,
tab_width,
true,
node_limit,
byte_limit,
@ -521,7 +530,7 @@ fn max_num_nodes(roots_vec: &[(Vec<&syntax::Syntax>, Vec<&syntax::Syntax>)]) ->
#[cfg(test)]
mod tests {
use super::*;
use crate::options::{DEFAULT_BYTE_LIMIT, DEFAULT_NODE_LIMIT};
use crate::options::{DEFAULT_BYTE_LIMIT, DEFAULT_NODE_LIMIT, DEFAULT_TAB_WIDTH};
#[test]
fn test_diff_identical_content() {
@ -530,6 +539,7 @@ mod tests {
"foo.el",
s.as_bytes(),
s.as_bytes(),
DEFAULT_TAB_WIDTH,
DEFAULT_NODE_LIMIT,
DEFAULT_BYTE_LIMIT,
None,

@ -8,6 +8,7 @@ use crate::{guess_language, style::BackgroundColor};
pub const DEFAULT_NODE_LIMIT: u32 = 30_000;
pub const DEFAULT_BYTE_LIMIT: usize = 1_000_000;
pub const DEFAULT_TAB_WIDTH: usize = 8;
const USAGE: &str = concat!(env!("CARGO_BIN_NAME"), " [OPTIONS] OLD-PATH NEW-PATH");
@ -64,6 +65,17 @@ fn app() -> clap::Command<'static> {
.validator(|s| s.parse::<usize>())
.required(false),
)
.arg(
Arg::new("tab-width")
.long("tab-width")
.takes_value(true)
.value_name("NUM_SPACES")
.long_help("Treat a tab as this many spaces.")
.env("DFT_TAB_WIDTH")
.default_value(formatcp!("{}", DEFAULT_TAB_WIDTH))
.validator(|s| s.parse::<usize>())
.required(false),
)
.arg(
Arg::new("display").long("display")
.possible_values(["side-by-side", "side-by-side-show-both", "inline", ])
@ -142,6 +154,7 @@ pub enum Mode {
byte_limit: usize,
print_unchanged: bool,
missing_as_empty: bool,
tab_width: usize,
display_mode: DisplayMode,
background_color: BackgroundColor,
color_output: ColorOutput,
@ -292,6 +305,12 @@ pub fn parse_args() -> Mode {
.parse::<usize>()
.expect("Value already validated by clap");
let tab_width = matches
.value_of("tab-width")
.expect("Always present as we've given clap a default")
.parse::<usize>()
.expect("Value already validated by clap");
let print_unchanged = !matches.is_present("skip-unchanged");
let missing_as_empty = matches.is_present("missing-as-empty");
@ -300,6 +319,7 @@ pub fn parse_args() -> Mode {
byte_limit,
print_unchanged,
missing_as_empty,
tab_width,
display_mode,
background_color,
color_output,