summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWayne Davison <wayne@opencoder.net>2021-11-22 17:19:23 -0800
committerGitHub <noreply@github.com>2021-11-22 20:19:23 -0500
commit0c0043b428834e9323eaaaf5795d55209fb3dd5a (patch)
treef18e1ab1e99b513e1b722534767b98f6227e2226
parentc2743f513176cf75c0733e4752a3c90000dded1b (diff)
Navigate regexp (#782)
* Allow navigate-regexp value to be overridden. * Don't add an empty label to the navigate_regexp. * Make --hunk-label=str not require --navigate. * Change navigate regexp to navigate regex. * Turn navigate-regex into an Option<String>.
-rw-r--r--README.md3
-rw-r--r--src/cli.rs4
-rw-r--r--src/config.rs12
-rw-r--r--src/features/navigate.rs31
-rw-r--r--src/handlers/hunk_header.rs2
-rw-r--r--src/options/set.rs3
-rw-r--r--src/subcommands/show_config.rs6
-rw-r--r--src/utils/bat/output.rs3
8 files changed, 41 insertions, 23 deletions
diff --git a/README.md b/README.md
index 8a2aa1f4..1c4b8701 100644
--- a/README.md
+++ b/README.md
@@ -860,6 +860,9 @@ OPTIONS:
--wrap-right-prefix-symbol <wrap-right-prefix-symbol>
Symbol displayed in front of right-aligned wrapped content [default: …]
+ --navigate-regex <navigate-regex>
+ A regexp to use in the less pager when navigating (auto-generated when unspecified)
+
--file-modified-label <file-modified-label>
Text to display in front of a modified file path [default: ]
diff --git a/src/cli.rs b/src/cli.rs
index 9f4a18cf..bc928291 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -573,6 +573,10 @@ pub struct Opt {
#[structopt(long = "wrap-right-prefix-symbol", default_value = "…")]
pub wrap_right_prefix_symbol: String,
+ #[structopt(long = "navigate-regex")]
+ /// A regexp to use in the less pager when navigating (auto-generated when unspecified)
+ pub navigate_regex: Option<String>,
+
#[structopt(long = "file-modified-label", default_value = "")]
/// Text to display in front of a modified file path.
pub file_modified_label: String,
diff --git a/src/config.rs b/src/config.rs
index 12b82945..1c28d060 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -113,7 +113,7 @@ pub struct Config {
pub minus_file: Option<PathBuf>,
pub minus_non_emph_style: Style,
pub minus_style: Style,
- pub navigate_regexp: Option<String>,
+ pub navigate_regex: Option<String>,
pub navigate: bool,
pub null_style: Style,
pub null_syntect_style: SyntectStyle,
@@ -208,8 +208,10 @@ impl From<cli::Opt> for Config {
side_by_side_data,
);
- let navigate_regexp = if opt.navigate || opt.show_themes {
- Some(navigate::make_navigate_regexp(
+ let navigate_regex = if (opt.navigate || opt.show_themes)
+ && (opt.navigate_regex.is_none() || opt.navigate_regex == Some("".to_string()))
+ {
+ Some(navigate::make_navigate_regex(
opt.show_themes,
&file_modified_label,
&file_added_label,
@@ -218,7 +220,7 @@ impl From<cli::Opt> for Config {
&hunk_label,
))
} else {
- None
+ opt.navigate_regex
};
let wrap_max_lines_plus1 = adapt_wrap_max_lines_argument(opt.wrap_max_lines);
@@ -320,7 +322,7 @@ impl From<cli::Opt> for Config {
minus_non_emph_style: styles["minus-non-emph-style"],
minus_style: styles["minus-style"],
navigate: opt.navigate,
- navigate_regexp,
+ navigate_regex,
null_style: Style::new(),
null_syntect_style: SyntectStyle::default(),
pager: opt.pager,
diff --git a/src/features/navigate.rs b/src/features/navigate.rs
index cb5fb144..60182907 100644
--- a/src/features/navigate.rs
+++ b/src/features/navigate.rs
@@ -30,7 +30,7 @@ pub fn make_feature() -> Vec<(String, OptionValueFunction)> {
}
// Construct the regexp used by less for paging, if --show-themes or --navigate is enabled.
-pub fn make_navigate_regexp(
+pub fn make_navigate_regex(
show_themes: bool,
file_modified_label: &str,
file_added_label: &str,
@@ -41,27 +41,34 @@ pub fn make_navigate_regexp(
if show_themes {
"^Theme:".to_string()
} else {
+ let optional_regexp = |find: &str| {
+ if !find.is_empty() {
+ format!("|{}", regex::escape(find))
+ } else {
+ "".to_string()
+ }
+ };
format!(
- "^(commit|{}|{}|{}|{}|{})",
- regex::escape(file_added_label),
- regex::escape(file_removed_label),
- regex::escape(file_renamed_label),
- regex::escape(file_modified_label),
- regex::escape(hunk_label),
+ "^(commit{}{}{}{}{})",
+ optional_regexp(file_added_label),
+ optional_regexp(file_removed_label),
+ optional_regexp(file_renamed_label),
+ optional_regexp(file_modified_label),
+ optional_regexp(hunk_label),
)
}
}
// Create a less history file to be used by delta's child less process. This file is initialized
-// with the contents of user's real less hist file, to which the navigate regexp is appended. This
-// has the effect that 'n' or 'N' in delta's less process will search for the navigate regexp,
+// with the contents of user's real less hist file, to which the navigate regex is appended. This
+// has the effect that 'n' or 'N' in delta's less process will search for the navigate regex,
// without the undesirable aspects of using --pattern, yet without polluting the user's less search
-// history with delta's navigate regexp. See
+// history with delta's navigate regex. See
// https://github.com/dandavison/delta/issues/237#issuecomment-780654036. Note that with the
// current implementation, no writes to the delta less history file are propagated back to the real
// history file so, for example, a (non-navigate) search performed in the delta less process will
// not be stored in history.
-pub fn copy_less_hist_file_and_append_navigate_regexp(config: &Config) -> std::io::Result<PathBuf> {
+pub fn copy_less_hist_file_and_append_navigate_regex(config: &Config) -> std::io::Result<PathBuf> {
let delta_less_hist_file = get_delta_less_hist_file()?;
let initial_contents = ".less-history-file:\n".to_string();
let mut contents = if let Some(hist_file) = get_less_hist_file() {
@@ -76,7 +83,7 @@ pub fn copy_less_hist_file_and_append_navigate_regexp(config: &Config) -> std::i
std::fs::File::create(&delta_less_hist_file)?,
"{}\"{}",
contents,
- config.navigate_regexp.as_ref().unwrap(),
+ config.navigate_regex.as_ref().unwrap(),
)?;
Ok(delta_less_hist_file)
}
diff --git a/src/handlers/hunk_header.rs b/src/handlers/hunk_header.rs
index 1a336992..dd851509 100644
--- a/src/handlers/hunk_header.rs
+++ b/src/handlers/hunk_header.rs
@@ -230,7 +230,7 @@ fn write_to_output_buffer(
painter: &mut Painter,
config: &Config,
) {
- if config.navigate {
+ if !config.hunk_label.is_empty() {
let _ = write!(
&mut painter.output_buffer,
"{} ",
diff --git a/src/options/set.rs b/src/options/set.rs
index faa2398b..b1dd49d0 100644
--- a/src/options/set.rs
+++ b/src/options/set.rs
@@ -167,6 +167,7 @@ pub fn set_options(
minus_non_emph_style,
minus_non_emph_style,
navigate,
+ navigate_regex,
line_fill_method,
line_numbers,
line_numbers_left_format,
@@ -696,6 +697,7 @@ pub mod tests {
minus-non-emph-style = black black
minus-style = black black
navigate = true
+ navigate-regex = xxxyyyzzz
paging = never
plus-emph-style = black black
plus-empty-line-marker-style = black black
@@ -756,6 +758,7 @@ pub mod tests {
assert_eq!(opt.minus_non_emph_style, "black black");
assert_eq!(opt.minus_style, "black black");
assert_eq!(opt.navigate, true);
+ assert_eq!(opt.navigate_regex, Some("xxxyyyzzz".to_string()));
assert_eq!(opt.paging_mode, "never");
assert_eq!(opt.plus_emph_style, "black black");
assert_eq!(opt.plus_empty_line_marker_style, "black black");
diff --git a/src/subcommands/show_config.rs b/src/subcommands/show_config.rs
index 594dca68..cf86d09d 100644
--- a/src/subcommands/show_config.rs
+++ b/src/subcommands/show_config.rs
@@ -117,7 +117,7 @@ pub fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::
max-line-length = {max_line_length}
line-fill-method = {line_fill_method}
navigate = {navigate}
- navigate-regexp = {navigate_regexp}
+ navigate-regex = {navigate_regex}
pager = {pager}
paging = {paging_mode}
side-by-side = {side_by_side}
@@ -132,9 +132,9 @@ pub fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::
BgFillMethod::Spaces => "spaces",
},
navigate = config.navigate,
- navigate_regexp = match &config.navigate_regexp {
+ navigate_regex = match &config.navigate_regex {
None => "".to_string(),
- Some(s) => s.to_string(),
+ Some(s) => format_option_value(s.to_string()),
},
pager = config.pager.clone().unwrap_or_else(|| "none".to_string()),
paging_mode = match config.paging_mode {
diff --git a/src/utils/bat/output.rs b/src/utils/bat/output.rs
index a231168b..62761c25 100644
--- a/src/utils/bat/output.rs
+++ b/src/utils/bat/output.rs
@@ -162,8 +162,7 @@ fn _make_process_from_less_path(
p.env("LESSCHARSET", "UTF-8");
p.env("LESSANSIENDCHARS", "mK");
if config.navigate {
- if let Ok(hist_file) = navigate::copy_less_hist_file_and_append_navigate_regexp(config)
- {
+ if let Ok(hist_file) = navigate::copy_less_hist_file_and_append_navigate_regex(config) {
p.env("LESSHISTFILE", hist_file);
if config.show_themes {
p.arg("+n");