Skip to content

Commit

Permalink
Handle git grep output
Browse files Browse the repository at this point in the history
Fixes #769
  • Loading branch information
dandavison committed Nov 14, 2021
1 parent 4130dd1 commit 7617f08
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 7617f08

Please sign in to comment.