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

Add option to show line numbers #190

Merged
merged 1 commit into from
Jun 9, 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
64 changes: 64 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ 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 that have a name like --*-format allow you to specify a string to display for the line
number columns. The string should specify the location of the line number using the placeholder
%ln.

For example, to display the line numbers divided by specific characters:

8 ⋮ 9 │ Here is an output line
9 ⋮ 10 │ Here is another output line
10 ⋮ 11 │ Here is the line number

you would use the following input:

--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 @@ -245,6 +263,52 @@ 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-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-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.
/// Defaults to '%ln⋮'
#[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.
/// Defaults to '%ln│ '
#[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-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-style.
#[structopt(long = "number-plus-format-style", default_value = "auto")]
pub number_plus_format_style: String,

#[structopt(long = "color-only")]
/// Do not alter the input in any way other than applying colors. Equivalent to
/// `--keep-plus-minus-markers --width variable --tabs 0 --commit-decoration ''
Expand Down
79 changes: 79 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ pub struct Config<'a> {
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 paging_mode: PagingMode,
pub plus_emph_style: Style,
pub plus_file: Option<PathBuf>,
pub plus_line_marker: &'a str,
pub plus_non_emph_style: Style,
pub plus_style: Style,
pub show_line_numbers: bool,
pub syntax_dummy_theme: SyntaxTheme,
pub syntax_set: SyntaxSet,
pub syntax_theme: Option<SyntaxTheme>,
Expand Down Expand Up @@ -120,6 +127,17 @@ pub fn get_config<'a>(
let (commit_style, file_style, hunk_header_style) =
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,
);

let syntax_theme = if syntax_theme::is_no_syntax_highlighting_theme_name(&syntax_theme_name) {
None
} else {
Expand Down Expand Up @@ -164,12 +182,19 @@ pub fn get_config<'a>(
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_format_style,
number_minus_style: number_minus_style,
number_plus_format: opt.number_plus_format,
number_plus_format_style: number_plus_format_style,
number_plus_style: number_plus_style,
paging_mode,
plus_emph_style,
plus_file: opt.plus_file.map(|s| s.clone()),
plus_line_marker,
plus_non_emph_style,
plus_style,
show_line_numbers: opt.show_line_numbers,
syntax_dummy_theme,
syntax_set,
syntax_theme,
Expand Down Expand Up @@ -264,6 +289,60 @@ 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),
};

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

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

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

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

(
number_minus_format_style,
number_minus_style,
number_plus_format_style,
number_plus_style,
)
}

fn make_commit_file_hunk_header_styles(opt: &cli::Opt, true_color: bool) -> (Style, Style, Style) {
(
Style::from_str_with_handling_of_special_decoration_attributes_and_respecting_deprecated_foreground_color_arg(
Expand Down
23 changes: 18 additions & 5 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ fn handle_hunk_header_line(
draw::write_no_decoration
}
};
let (raw_code_fragment, line_number) = parse::parse_hunk_metadata(&line);
let (raw_code_fragment, line_numbers) = parse::parse_hunk_metadata(&line);
painter.minus_line_number = line_numbers[0];
painter.plus_line_number = line_numbers[line_numbers.len() - 1];
if config.hunk_header_style.is_raw {
writeln!(painter.writer)?;
draw_fn(
Expand Down Expand Up @@ -397,6 +399,7 @@ fn handle_hunk_header_line(
Painter::paint_lines(
syntax_style_sections,
vec![vec![(config.hunk_header_style, &lines[0])]],
vec![None],
&mut painter.output_buffer,
config,
"",
Expand All @@ -418,10 +421,14 @@ fn handle_hunk_header_line(
};
}
};
match config.hunk_header_style.decoration_ansi_term_style() {
Some(style) => writeln!(painter.writer, "{}", style.paint(line_number))?,
None => writeln!(painter.writer, "{}", line_number)?,
};

if !config.show_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))?,
None => writeln!(painter.writer, "{}", line_number)?,
}
}
Ok(())
}

Expand Down Expand Up @@ -474,13 +481,19 @@ fn handle_hunk_line(
Painter::paint_lines(
syntax_style_sections,
vec![diff_style_sections],
vec![Some((
Some(painter.minus_line_number),
Some(painter.plus_line_number),
))],
&mut painter.output_buffer,
config,
prefix,
config.zero_style,
config.zero_style,
None,
);
painter.minus_line_number += 1;
painter.plus_line_number += 1;
state
}
_ => {
Expand Down
Loading