Skip to content

Commit

Permalink
Allow user to specify tab replacement string
Browse files Browse the repository at this point in the history
Fixes #522
  • Loading branch information
dandavison committed Feb 12, 2021
1 parent 3fbb739 commit 6cdb57e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
14 changes: 10 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,16 @@ pub struct Opt {
#[structopt(short = "w", long = "width")]
pub width: Option<String>,

/// 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
/// one character wide then delta's output will look incorrect.
/// A string to use to display tab characters with. By default, each tab character will be
/// replaced by space characters. (See `tabs`)
#[structopt(long = "tab-string")]
pub tab_string: Option<String>,

/// The number of spaces to replace tab characters with, if tab-string has not been
/// supplied. 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 one character wide then delta's output will
/// look incorrect.
#[structopt(long = "tabs", default_value = "4")]
pub tab_width: usize,

Expand Down
8 changes: 6 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct Config {
pub syntax_dummy_theme: SyntaxTheme,
pub syntax_set: SyntaxSet,
pub syntax_theme: Option<SyntaxTheme>,
pub tab_width: usize,
pub tab_string: String,
pub tokenization_regex: Regex,
pub true_color: bool,
pub truncation_symbol: String,
Expand Down Expand Up @@ -218,7 +218,11 @@ impl From<cli::Opt> for Config {
syntax_dummy_theme: SyntaxTheme::default(),
syntax_set: opt.computed.syntax_set,
syntax_theme: opt.computed.syntax_theme,
tab_width: opt.tab_width,
tab_string: if let Some(s) = opt.tab_string {
s
} else {
" ".repeat(opt.tab_width)
},
tokenization_regex,
true_color: opt.computed.true_color,
truncation_symbol: "→".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::Resu
side-by-side = {side_by_side}
syntax-theme = {syntax_theme}
width = {width}
tabs = {tab_width}
tab_string = '{tab_string}'
word-diff-regex = {tokenization_regex}",
max_line_distance = config.max_line_distance,
max_line_length = config.max_line_length,
Expand All @@ -278,7 +278,7 @@ fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::Resu
cli::Width::Fixed(width) => width.to_string(),
cli::Width::Variable => "variable".to_string(),
},
tab_width = config.tab_width,
tab_string = config.tab_string,
tokenization_regex = format_option_value(&config.tokenization_regex.to_string()),
)?;
Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pub fn set_options(
plus_non_emph_style,
raw,
side_by_side,
tab_string,
tab_width,
tokenization_regex,
true_color,
Expand Down
13 changes: 9 additions & 4 deletions src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,15 @@ impl<'a> Painter<'a> {
where
I: Iterator<Item = &'b str>,
{
if self.config.tab_width > 0 {
let tab_replacement = " ".repeat(self.config.tab_width);
line.map(|s| if s == "\t" { &tab_replacement } else { s })
.collect::<String>()
if !self.config.tab_string.is_empty() {
line.map(|s| {
if s == "\t" {
&self.config.tab_string
} else {
s
}
})
.collect::<String>()
} else {
line.collect::<String>()
}
Expand Down

0 comments on commit 6cdb57e

Please sign in to comment.