Skip to content

Commit

Permalink
Handle grep output
Browse files Browse the repository at this point in the history
- Handle standard filepath:code and filepath:line_number:code output
  as produced by `git grep`, `rg -H`, `grep -H`, etc (with -n for line
  numbers).

- Retain the match highlighting as produced by the grep tool, and
  expose it in delta's color output styled with grep-match-style.
  (Note that --color=always is needed to retain the color if piping
  into delta, but not for `git grep` when delta is configured as git's
  pager)

- Special handling of -p, and -W options of `git grep`: these display
  the function context in which the matches occur.

- `navigate` keybindings jump between match function contexts under
  `git grep -p` and between matching lines under `git grep -W`.

Thanks @zachriggle for the proposal.
Fixes #769
  • Loading branch information
dandavison committed Nov 15, 2021
1 parent e3ae226 commit 5e8c209
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -23,6 +23,7 @@
show = delta
log = delta
blame = delta
grep = delta
reflog = delta

[interactive]
Expand Down Expand Up @@ -64,6 +65,7 @@ Code evolves, and we all spend time studying diffs. Delta aims to make this both
- [Choosing colors (styles)](#choosing-colors-styles)
- [Line numbers](#line-numbers)
- [Side-by-side view](#side-by-side-view)
- [grep](#grep)
- [git blame](#git-blame)
- ["Features": named groups of settings](#features-named-groups-of-settings)
- [Custom themes](#custom-themes)
Expand Down Expand Up @@ -152,6 +154,7 @@ Here's what `git show` can look like with git configured to use delta:
- Git style strings (foreground color, background color, font attributes) are supported for >20 stylable elements
- Side-by-side view with line-wrapping
- Line numbering
- Handles grep output with file paths from `rg`, `git grep`, `grep`, etc
- `diff-highlight` and `diff-so-fancy` emulation modes
- Stylable box/line decorations to draw attention to commit, file and hunk header sections.
- Support for Git's `--color-moved` feature.
Expand Down Expand Up @@ -404,6 +407,13 @@ In contrast, the long replacement line in the right panel overflows by almost an
For control over the details of line wrapping, see `--wrap-max-lines`, `--wrap-left-symbol`, `--wrap-right-symbol`, `--wrap-right-percent`, `--wrap-right-prefix-symbol`, `--inline-hint-style`.
Line wrapping was implemented by @th1000s.

### grep

To use with `git grep`, set delta as the pager for `grep` in the `[pager]` section of your gitconfig. See the example at the [top of the page](#get-started).
Output from other grep tools can be piped to delta: use `rg -Hn --color=always`, `grep -Hn --color=always`, etc.
Grep matches are styled with `grep-match-style`, `grep-match-style`, and `grep-match-line-number-style`.
Note that `git grep` can display the "function context" for matches and that delta handles this output specially: see the `-p` and `-W` options of `git grep`.

### git blame

Set delta as the pager for `blame` in the `[pager]` section of your gitconfig. See the example at the [top of the page](#get-started).
Expand Down
15 changes: 15 additions & 0 deletions src/cli.rs
Expand Up @@ -441,6 +441,21 @@ pub struct Opt {
)]
pub blame_timestamp_format: String,

#[structopt(long = "grep-match-style")]
/// Style (foreground, background, attributes) for grep matches. See STYLES
/// section. Defaults to plus-style.
pub grep_match_style: Option<String>,

#[structopt(long = "grep-match-file-style")]
/// Style (foreground, background, attributes) for grep match file paths. See STYLES
/// section. Defaults to hunk-header-file-path-style.
pub grep_match_file_style: Option<String>,

#[structopt(long = "grep-match-line-number-style")]
/// Style (foreground, background, attributes) for grep match line numbers. See STYLES
/// section. Defaults to hunk-header-line-number-style.
pub grep_match_line_number_style: Option<String>,

/// Default language used for syntax highlighting when this cannot be
/// inferred from a filename. It will typically make sense to set this in
/// per-repository git config (.git/config)
Expand Down
22 changes: 22 additions & 0 deletions src/config.rs
Expand Up @@ -78,6 +78,9 @@ pub struct Config {
pub git_config: Option<GitConfig>,
pub git_minus_style: Style,
pub git_plus_style: Style,
pub grep_match_file_style: Style,
pub grep_match_line_number_style: Style,
pub grep_match_style: Style,
pub hunk_header_file_style: Style,
pub hunk_header_line_number_style: Style,
pub hunk_header_style_include_file_path: bool,
Expand Down Expand Up @@ -216,6 +219,22 @@ impl From<cli::Opt> for Config {
_ => *style::GIT_DEFAULT_PLUS_STYLE,
};

let grep_match_style = if let Some(s) = opt.grep_match_style {
Style::from_str(&s, None, None, opt.computed.true_color, false)
} else {
plus_emph_style
};
let grep_match_file_style = if let Some(s) = opt.grep_match_file_style {
Style::from_str(&s, None, None, opt.computed.true_color, false)
} else {
hunk_header_file_style
};
let grep_match_line_number_style = if let Some(s) = opt.grep_match_line_number_style {
Style::from_str(&s, None, None, opt.computed.true_color, false)
} else {
hunk_header_line_number_style
};

let blame_palette = make_blame_palette(opt.blame_palette, opt.computed.is_light_mode);

let file_added_label = opt.file_added_label;
Expand Down Expand Up @@ -282,6 +301,9 @@ impl From<cli::Opt> for Config {
file_style,
git_config: opt.git_config,
git_config_entries: opt.git_config_entries,
grep_match_file_style,
grep_match_line_number_style,
grep_match_style,
hunk_header_file_style,
hunk_header_line_number_style,
hunk_header_style,
Expand Down
2 changes: 2 additions & 0 deletions src/delta.rs
Expand Up @@ -23,6 +23,7 @@ pub enum State {
SubmoduleLog, // In a submodule section, with gitconfig diff.submodule = log
SubmoduleShort(String), // In a submodule section, with gitconfig diff.submodule = short
Blame(String, Option<String>), // In a line of `git blame` output (commit, repeat_blame_line).
Grep(String, Option<String>), // In a line of `git grep` output (file, repeat_grep_line).
Unknown,
// The following elements are created when a line is wrapped to display it:
HunkZeroWrapped, // Wrapped unchanged line
Expand Down Expand Up @@ -121,6 +122,7 @@ impl<'a> StateMachine<'a> {
|| self.handle_submodule_short_line()?
|| self.handle_hunk_line()?
|| self.handle_blame_line()?
|| self.handle_grep_line()?
|| self.should_skip_line()
|| self.emit_line_unchanged()?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/file_meta.rs
Expand Up @@ -210,7 +210,7 @@ fn get_file_extension_from_file_meta_line_file_path(path: &str) -> Option<&str>
}

/// Attempt to parse input as a file path and return extension as a &str.
fn get_extension(s: &str) -> Option<&str> {
pub fn get_extension(s: &str) -> Option<&str> {
let path = Path::new(s);
path.extension()
.and_then(|e| e.to_str())
Expand Down

0 comments on commit 5e8c209

Please sign in to comment.