Skip to content

Commit

Permalink
Introduce grep-output-type option: ripgrep or classic
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Jun 2, 2023
1 parent 9b4eb75 commit 514cce9
Show file tree
Hide file tree
Showing 12 changed files with 553 additions and 126 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ Side-by-side view wraps long lines automatically:

[[User manual](https://dandavison.github.io/delta/grep.html)]

<table><tr><td><img width=400px src="https://user-images.githubusercontent.com/52205/225271024-a01367f0-af1b-466a-9b9e-b1ced7f80031.png" alt="image" /></td></tr></table>
<table><tr><td>
<img width="600px" alt="image" src="https://github.com/dandavison/open-in-editor/assets/52205/d203d380-5acb-4296-aeb9-e38c73d6c27f">
</td></tr></table>

### Installation and usage

Expand Down
19 changes: 16 additions & 3 deletions manual/src/grep.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,28 @@ To customize the colors and syntax highlighting, see `grep-match-line-style`, `g
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`.

```sh
rg --json handle | delta
rg --json -C 2 handle | delta
```

<table><tr><td><img width=400px src="https://user-images.githubusercontent.com/52205/225271024-a01367f0-af1b-466a-9b9e-b1ced7f80031.png" alt="image" /></td></tr></table>
<table><tr><td>
<img width="600px" alt="image" src="https://github.com/dandavison/open-in-editor/assets/52205/d203d380-5acb-4296-aeb9-e38c73d6c27f">
</td></tr></table>

If you enable hyperlinks then grep hits will be formatted as [OSC8 hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) in terminal emulators that support the feature. It is then possible to make your OS handle a click on those links by opening your editor at the correct file and line number (e.g. via <https://github.com/dandavison/open-in-editor/>).
If you enable hyperlinks then grep hits will be formatted as [OSC8 hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) in terminal emulators that support the feature. If you're using VSCode, IntelliJ, or PyCharm, then use the dedicated URL handlers. I.e. one of the following lines:

```gitconfig
[delta]
hyperlinks = true
hyperlinks-file-link-format = "vscode://file/{path}:{line}"
# or: hyperlinks-file-link-format = "idea://open?file={path}&line={line}"
# or: hyperlinks-file-link-format = "pycharm://open?file={path}&line={line}"
```

For editors that don't have special URL handlers, it is possible to use a tool like <https://github.com/dandavison/open-in-editor/> to make your OS handle a click on those links by opening your editor at the correct file and line number, e.g.

```gitconfig
[delta]
hyperlinks = true
hyperlinks-file-link-format = "file-line://{path}:{line}"
# Now configure your OS to handle "file-line" URLs
```
46 changes: 40 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,51 @@ pub struct Opt {
/// See STYLES section. Defaults to zero-style.
pub grep_context_line_style: Option<String>,

#[arg(long = "grep-file-style", value_name = "STYLE")]
#[arg(
long = "grep-file-style",
default_value = "magenta",
value_name = "STYLE"
)]
/// Style string for file paths in grep output.
///
/// See STYLES section. Defaults to hunk-header-file-path-style.
pub grep_file_style: Option<String>,
/// See STYLES section.
pub grep_file_style: String,

#[arg(long = "grep-header-decoration-style", value_name = "STYLE")]
/// Style string for the header decoration in grep output.
///
/// Default is "none" when grep-ouput-type-is "ripgrep", otherwise defaults
/// to value of header-decoration-style. See hunk-header-decoration-style.
pub grep_header_decoration_style: Option<String>,

#[arg(long = "grep-header-file-style", value_name = "STYLE")]
/// Style string for the file path part of the header in grep output.
///
/// See hunk_header_file_style.
pub grep_header_file_style: Option<String>,

#[arg(long = "grep-header-style", value_name = "STYLE")]
/// Style string for the header in grep output.
///
/// See hunk-header-style.
pub grep_header_style: Option<String>,

#[arg(long = "grep-line-number-style", value_name = "STYLE")]
#[arg(
long = "grep-line-number-style",
default_value = "green",
value_name = "STYLE"
)]
/// Style string for line numbers in grep output.
///
/// See STYLES section. Defaults to hunk-header-line-number-style.
pub grep_line_number_style: Option<String>,
/// See STYLES section.
pub grep_line_number_style: String,

#[arg(long = "grep-output-type", value_name = "OUTPUT_TYPE")]
/// Grep output format. Possible values:
/// "ripgrep" - file name printed once, followed by matching lines within that file, each preceded by a line number.
/// "classic" - file name:line number, followed by matching line.
/// Default is "ripgrep" if `rg --json` format is detected, otherwise "classic".
pub grep_output_type: Option<String>,

#[arg(long = "grep-match-line-style", value_name = "STYLE")]
/// Style string for matching lines of grep output.
Expand Down
56 changes: 50 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,19 @@ pub struct Config {
pub git_plus_style: Style,
pub grep_context_line_style: Style,
pub grep_file_style: Style,
pub classic_grep_header_file_style: Style,
pub classic_grep_header_style: Style,
pub ripgrep_header_style: Style,
pub grep_line_number_style: Style,
pub grep_match_line_style: Style,
pub grep_match_word_style: Style,
pub grep_output_type: Option<GrepType>,
pub grep_separator_symbol: String,
pub handle_merge_conflicts: bool,
pub hunk_header_file_style: Style,
pub hunk_header_line_number_style: Style,
pub hunk_header_style_include_file_path: bool,
pub hunk_header_style_include_line_number: bool,
pub hunk_header_style_include_file_path: HunkHeaderIncludeFilePath,
pub hunk_header_style_include_line_number: HunkHeaderIncludeLineNumber,
pub hunk_header_style: Style,
pub hunk_label: String,
pub hyperlinks_commit_link_format: Option<String>,
Expand Down Expand Up @@ -129,6 +133,24 @@ pub struct Config {
pub zero_style: Style,
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum GrepType {
Ripgrep,
Classic,
}

#[cfg_attr(test, derive(Clone))]
pub enum HunkHeaderIncludeFilePath {
Yes,
No,
}

#[cfg_attr(test, derive(Clone))]
pub enum HunkHeaderIncludeLineNumber {
Yes,
No,
}

impl Config {
pub fn get_style(&self, state: &State) -> &Style {
match state {
Expand All @@ -137,6 +159,7 @@ impl Config {
State::HunkPlus(_, _) => &self.plus_style,
State::CommitMeta => &self.commit_style,
State::DiffHeader(_) => &self.file_style,
State::Grep(GrepType::Ripgrep, _, _, _) => &self.classic_grep_header_style,
State::HunkHeader(_, _, _, _) => &self.hunk_header_style,
State::SubmoduleLog => &self.file_style,
_ => delta_unreachable("Unreachable code reached in get_style."),
Expand Down Expand Up @@ -222,6 +245,13 @@ impl From<cli::Opt> for Config {
opt.navigate_regex
};

let grep_output_type = match opt.grep_output_type.as_deref() {
Some("ripgrep") => Some(GrepType::Ripgrep),
Some("classic") => Some(GrepType::Classic),
None => None,
_ => fatal("Invalid option for grep-output-type: Expected \"ripgrep\" or \"classic\"."),
};

#[cfg(not(test))]
let cwd_of_delta_process = opt.env.current_dir;
#[cfg(test)]
Expand Down Expand Up @@ -271,22 +301,36 @@ impl From<cli::Opt> for Config {
git_config: opt.git_config,
grep_context_line_style: styles["grep-context-line-style"],
grep_file_style: styles["grep-file-style"],
classic_grep_header_file_style: styles["classic-grep-header-file-style"],
classic_grep_header_style: styles["classic-grep-header-style"],
ripgrep_header_style: styles["ripgrep-header-style"],
grep_line_number_style: styles["grep-line-number-style"],
grep_match_line_style: styles["grep-match-line-style"],
grep_match_word_style: styles["grep-match-word-style"],
grep_output_type,
grep_separator_symbol: opt.grep_separator_symbol,
handle_merge_conflicts: !opt.raw,
hunk_header_file_style: styles["hunk-header-file-style"],
hunk_header_line_number_style: styles["hunk-header-line-number-style"],
hunk_header_style: styles["hunk-header-style"],
hunk_header_style_include_file_path: opt
hunk_header_style_include_file_path: if opt
.hunk_header_style
.split(' ')
.any(|s| s == "file"),
hunk_header_style_include_line_number: opt
.any(|s| s == "file")
{
HunkHeaderIncludeFilePath::Yes
} else {
HunkHeaderIncludeFilePath::No
},
hunk_header_style_include_line_number: if opt
.hunk_header_style
.split(' ')
.any(|s| s == "line-number"),
.any(|s| s == "line-number")
{
HunkHeaderIncludeLineNumber::Yes
} else {
HunkHeaderIncludeLineNumber::No
},
hyperlinks: opt.hyperlinks,
hyperlinks_commit_link_format: opt.hyperlinks_commit_link_format,
hyperlinks_file_link_format: opt.hyperlinks_file_link_format,
Expand Down
4 changes: 3 additions & 1 deletion src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use bytelines::ByteLines;
use crate::ansi;
use crate::config::delta_unreachable;
use crate::config::Config;
use crate::config::GrepType;
use crate::features;
use crate::handlers::grep;
use crate::handlers::hunk_header::ParsedHunkHeader;
use crate::handlers::{self, merge_conflict};
use crate::paint::Painter;
Expand All @@ -28,7 +30,7 @@ pub enum State {
SubmoduleShort(String), // In a submodule section, with gitconfig diff.submodule = short
Blame(String), // In a line of `git blame` output (key).
GitShowFile, // In a line of `git show $revision:./path/to/file.ext` output
Grep, // In a line of `git grep` output
Grep(GrepType, grep::LineType, String, Option<usize>), // In a line of `git grep` output (grep_type, line_type, path, line_number)
Unknown,
// The following elements are created when a line is wrapped to display it:
HunkZeroWrapped, // Wrapped unchanged line
Expand Down
2 changes: 2 additions & 0 deletions src/features/hyperlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ __path__: some matching line
"raw",
"--grep-line-number-style",
"raw",
"--grep-output-type",
"classic",
"--hunk-header-file-style",
"raw",
"--hunk-header-line-number-style",
Expand Down
Loading

0 comments on commit 514cce9

Please sign in to comment.