Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Command line option to apply line number style to unchanged lines #219

Merged
merged 16 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 76 additions & 51 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,49 @@ within a style string):
Specifying colors like this is useful if your terminal only supports 256 colors (i.e. doesn\'t
support 24-bit color).


LINE NUMBERS
------------

Options --number-minus-format and --number-plus-format allow you to specify a custom string to
display for the line number columns. The string should specify the location of the line number
using the placeholder %ln.
To display line numbers, use --line-numbers.

Line numbers are displayed in two columns. Here's what it looks like by default:

1 ⋮ 1 │ unchanged line
2 ⋮ │ removed line
⋮ 2 │ added line

In that output, the line numbers for the old (minus) version of the file appear in the left column,
and the line numbers for the new (plus) version of the file appear in the right column. In an
unchanged (zero) line, both columns contain a line number.

The following options allow the line number display to be customized:

--line-numbers-left-format: Change the contents of the left column
--line-numbers-right-format: Change the contents of the right column
--line-numbers-left-style: Change the style applied to the left column
--line-numbers-right-style: Change the style applied to the right column
--line-numbers-minus-style: Change the style applied to line numbers in minus lines
--line-numbers-zero-style: Change the style applied to line numbers in unchanged lines
--line-numbers-plus-style: Change the style applied to line numbers in plus lines

Options --line-numbers-left-format and --line-numbers-right-format allow you to change the contents
of the line number columns. Their values are arbitrary format strings, which are allowed to contain
the placeholders {nm} for the line number associated with the old version of the file and {np} for
the line number associated with the new version of the file. The placeholders support a subset of
the string formatting syntax documented here: https://doc.rust-lang.org/std/fmt/#formatting-parameters.
Specifically, you can use the alignment, width, and fill syntax.

For example, to display the line numbers like
For example, the default value of --line-numbers-left-format is '{nm:^4}⋮'. This means that the
left column should display the minus line number (nm), center-aligned, padded with spaces to a
width of 4 characters, followed by a unicode dividing-line character (⋮).

8 ⋮ 9 │ Here is an output line
Similarly, the default value of --line-numbers-right-format is '{np:^4}│ '. This means that the
right column should display the plus line number (np), center-aligned, padded with spaces to a
width of 4 characters, followed by a unicode dividing-line character (│), and a space.

you would use
Use '<' for left-align, '^' for center-align, and '>' for right-align.

--number-minus-format '%ln ⋮'
--number-plus-format '%ln │'

If something isn't working correctly, or you have a feature request, please open an issue at
https://github.com/dandavison/delta/issues.
Expand Down Expand Up @@ -290,49 +318,46 @@ pub struct Opt {
/// given.
pub hunk_header_decoration_style: String,

/// Display line numbers next to the diff. The first column contains line
/// numbers in the previous version of the file, and the second column contains
/// line number in the new version of the file. A blank cell in the first or
/// second column indicates that the line does not exist in that file (it was
/// added or removed, respectively).
#[structopt(short = "n", long = "number")]
pub show_line_numbers: bool,

/// Style (foreground, background, attributes) for the left (minus) column of line numbers
/// (--number), if --number is set. See STYLES section. Defaults to
/// --hunk-header-decoration-style.
#[structopt(long = "number-minus-style", default_value = "auto")]
pub number_minus_style: String,

/// Style (foreground, background, attributes) for the right (plus) column of line numbers
/// (--number), if --number is set. See STYLES section. Defaults to
/// --hunk-header-decoration-style.
#[structopt(long = "number-plus-style", default_value = "auto")]
pub number_plus_style: String,

/// Format string for the left (minus) column of line numbers (--number), if --number is set.
/// Should include the placeholder %ln to indicate the position of the line number.
/// See the LINE NUMBERS section.
#[structopt(long = "number-minus-format", default_value = "%ln⋮")]
pub number_minus_format: String,

/// Format string for the right (plus) column of line numbers (--number), if --number is set.
/// Should include the placeholder %ln to indicate the position of the line number.
/// See the LINE NUMBERS section.
#[structopt(long = "number-plus-format", default_value = "%ln│ ")]
pub number_plus_format: String,

/// Style (foreground, background, attributes) for the left (minus) line number format string
/// (--number), if --number is set. See STYLES section. Defaults to
/// --hunk-header-decoration-style.
#[structopt(long = "number-minus-format-style", default_value = "auto")]
pub number_minus_format_style: String,

/// Style (foreground, background, attributes) for the right (plus) line number format string
/// (--number), if --number is set. See STYLES section. Defaults to
/// --hunk-header-decoration-style.
#[structopt(long = "number-plus-format-style", default_value = "auto")]
pub number_plus_format_style: String,
/// Display line numbers next to the diff. See LINE NUMBERS section.
#[structopt(short = "n", long = "line-numbers")]
pub line_numbers: bool,

/// Style (foreground, background, attributes) for line numbers in the old (minus) version of
/// the file. See STYLES and LINE NUMBERS sections.
#[structopt(long = "line-numbers-minus-style", default_value = "auto")]
pub line_numbers_minus_style: String,

/// Style (foreground, background, attributes) for line numbers in unchanged (zero) lines. See
/// STYLES and LINE NUMBERS sections.
#[structopt(long = "line-numbers-zero-style", default_value = "auto")]
pub line_numbers_zero_style: String,

/// Style (foreground, background, attributes) for line numbers in the new (plus) version of
/// the file. See STYLES and LINE NUMBERS sections.
#[structopt(long = "line-numbers-plus-style", default_value = "auto")]
pub line_numbers_plus_style: String,

/// Format string for the left column of line numbers. A typical value would be "{nm:^4}⋮"
/// which means to display the line numbers of the minus file (old version), followed by a
/// dividing character. See the LINE NUMBERS section.
#[structopt(long = "line-numbers-left-format", default_value = "{nm:^4}⋮")]
pub line_numbers_left_format: String,

/// Format string for the right column of line numbers. A typical value would be "{np:^4}│ "
/// which means to display the line numbers of the plus file (new version), followed by a
/// dividing character, and a space. See the LINE NUMBERS section.
#[structopt(long = "line-numbers-right-format", default_value = "{np:^4}│ ")]
pub line_numbers_right_format: String,

/// Style (foreground, background, attributes) for the left column of line numbers. See STYLES
/// and LINE NUMBERS sections.
#[structopt(long = "line-numbers-left-style", default_value = "auto")]
pub line_numbers_left_style: String,

/// Style (foreground, background, attributes) for the right column of line numbers. See STYLES
/// and LINE NUMBERS sections.
#[structopt(long = "line-numbers-right-style", default_value = "auto")]
pub line_numbers_right_style: String,

#[structopt(long = "color-only")]
/// Do not alter the input in any way other than applying colors. Equivalent to
Expand Down
101 changes: 52 additions & 49 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ pub struct Config {
pub navigate: bool,
pub null_style: Style,
pub null_syntect_style: SyntectStyle,
pub number_minus_format: String,
pub number_minus_format_style: Style,
pub number_minus_style: Style,
pub number_plus_format: String,
pub number_plus_format_style: Style,
pub number_plus_style: Style,
pub line_numbers_left_format: String,
pub line_numbers_left_style: Style,
pub line_numbers_minus_style: Style,
pub line_numbers_plus_style: Style,
pub line_numbers_right_format: String,
pub line_numbers_right_style: Style,
pub line_numbers_zero_style: Style,
pub paging_mode: PagingMode,
pub plus_emph_style: Style,
pub plus_empty_line_marker_style: Style,
pub plus_file: Option<PathBuf>,
pub plus_non_emph_style: Style,
pub plus_style: Style,
pub show_background_colors: bool,
pub show_line_numbers: bool,
pub line_numbers: bool,
pub syntax_dummy_theme: SyntaxTheme,
pub syntax_set: SyntaxSet,
pub syntax_theme: Option<SyntaxTheme>,
Expand Down Expand Up @@ -156,15 +157,12 @@ impl From<cli::Opt> for Config {
make_commit_file_hunk_header_styles(&opt, true_color);

let (
number_minus_format_style,
number_minus_style,
number_plus_format_style,
number_plus_style,
) = make_line_number_styles(
&opt,
hunk_header_style.decoration_ansi_term_style(),
true_color,
);
line_numbers_minus_style,
line_numbers_zero_style,
line_numbers_plus_style,
line_numbers_left_style,
line_numbers_right_style,
) = make_line_number_styles(&opt, true_color);

let syntax_theme = if syntax_theme::is_no_syntax_highlighting_theme_name(&syntax_theme_name)
{
Expand Down Expand Up @@ -214,20 +212,21 @@ impl From<cli::Opt> for Config {
navigate: opt.navigate,
null_style: Style::new(),
null_syntect_style: SyntectStyle::default(),
number_minus_format: opt.number_minus_format,
number_minus_format_style,
number_minus_style,
number_plus_format: opt.number_plus_format,
number_plus_format_style,
number_plus_style,
line_numbers_left_format: opt.line_numbers_left_format,
line_numbers_left_style,
line_numbers_minus_style,
line_numbers_plus_style,
line_numbers_right_format: opt.line_numbers_right_format,
line_numbers_right_style,
line_numbers_zero_style,
paging_mode,
plus_emph_style,
plus_empty_line_marker_style,
plus_file: opt.plus_file.map(|s| s.clone()),
plus_non_emph_style,
plus_style,
show_background_colors: opt.show_background_colors,
show_line_numbers: opt.show_line_numbers,
line_numbers: opt.line_numbers,
syntax_dummy_theme,
syntax_set: assets.syntax_set,
syntax_theme,
Expand Down Expand Up @@ -378,55 +377,59 @@ fn make_hunk_styles<'a>(

fn make_line_number_styles<'a>(
opt: &'a cli::Opt,
default_style: Option<ansi_term::Style>,
true_color: bool,
) -> (Style, Style, Style, Style) {
let (default_foreground, default_background) = match default_style {
Some(default_style) => (default_style.foreground, default_style.background),
None => (None, None),
};
) -> (Style, Style, Style, Style, Style) {
let line_numbers_left_style = Style::from_str(
&opt.line_numbers_left_style,
None,
None,
None,
true_color,
false,
);

let number_minus_format_style = Style::from_str(
&opt.number_minus_format_style,
default_foreground,
default_background,
let line_numbers_minus_style = Style::from_str(
&opt.line_numbers_minus_style,
None,
None,
None,
true_color,
false,
);

let number_minus_style = Style::from_str(
&opt.number_minus_style,
default_foreground,
default_background,
let line_numbers_zero_style = Style::from_str(
&opt.line_numbers_zero_style,
None,
None,
None,
true_color,
false,
);

let number_plus_format_style = Style::from_str(
&opt.number_plus_format_style,
default_foreground,
default_background,
let line_numbers_plus_style = Style::from_str(
&opt.line_numbers_plus_style,
None,
None,
None,
true_color,
false,
);

let number_plus_style = Style::from_str(
&opt.number_plus_style,
default_foreground,
default_background,
let line_numbers_right_style = Style::from_str(
&opt.line_numbers_right_style,
None,
None,
None,
true_color,
false,
);

(
number_minus_format_style,
number_minus_style,
number_plus_format_style,
number_plus_style,
line_numbers_minus_style,
line_numbers_zero_style,
line_numbers_plus_style,
line_numbers_left_style,
line_numbers_right_style,
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ fn handle_hunk_header_line(
}
};

if !config.show_line_numbers {
if !config.line_numbers {
let line_number = &format!("{}", painter.plus_line_number);
match config.hunk_header_style.decoration_ansi_term_style() {
Some(style) => writeln!(painter.writer, "{}", style.paint(line_number))?,
Expand Down
Loading