Make it possible to jump between files when navigate is active (#684)

* Use distinct navigate label for file and hunk boundaries

Ref #680

* Make it easy to remove hunk label from navigate regex

Thanks @lepotic for the suggestion.
Fixes #680

* Allow regex meta characters to be used in navigate anchors
This commit is contained in:
Dan Davison 2021-08-20 18:34:50 -07:00 committed by GitHub
parent 512ccbc01a
commit 130b0b6a21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 5 deletions

View File

@ -490,6 +490,10 @@ pub struct Opt {
/// Text to display in front of a renamed file path.
pub file_renamed_label: String,
#[structopt(long = "hunk-label", default_value = "")]
/// Text to display in front of a hunk header.
pub hunk_label: String,
#[structopt(long = "max-line-length", default_value = "512")]
/// Truncate lines longer than this. To prevent any truncation, set to zero. Note that
/// syntax-highlighting very long lines (e.g. minified .js) will be very slow if they are not

View File

@ -33,6 +33,7 @@ pub struct Config {
pub file_modified_label: String,
pub file_removed_label: String,
pub file_renamed_label: String,
pub hunk_label: String,
pub file_style: Style,
pub git_config_entries: HashMap<String, GitConfigEntry>,
pub hunk_header_file_style: Style,
@ -178,6 +179,7 @@ impl From<cli::Opt> for Config {
let file_modified_label = opt.file_modified_label;
let file_removed_label = opt.file_removed_label;
let file_renamed_label = opt.file_renamed_label;
let hunk_label = opt.hunk_label;
let navigate_regexp = if opt.navigate || opt.show_themes {
Some(navigate::make_navigate_regexp(
@ -186,6 +188,7 @@ impl From<cli::Opt> for Config {
&file_added_label,
&file_removed_label,
&file_renamed_label,
&hunk_label,
))
} else {
None
@ -208,6 +211,7 @@ impl From<cli::Opt> for Config {
file_modified_label,
file_removed_label,
file_renamed_label,
hunk_label,
file_style,
git_config_entries: opt.git_config_entries,
hunk_header_file_style,

View File

@ -19,6 +19,12 @@ pub fn make_feature() -> Vec<(String, OptionValueFunction)> {
String,
None,
_opt => "Δ"
),
(
"hunk-label",
String,
None,
_opt => "δ"
)
])
}
@ -30,13 +36,18 @@ pub fn make_navigate_regexp(
file_added_label: &str,
file_removed_label: &str,
file_renamed_label: &str,
hunk_label: &str,
) -> String {
if show_themes {
"^Theme:".to_string()
} else {
format!(
"^(commit|{}|{}|{}|{})",
file_modified_label, file_added_label, file_removed_label, file_renamed_label,
"^(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),
)
}
}

View File

@ -94,10 +94,10 @@ fn get_painted_file_with_line_number(
config: &Config,
) -> String {
let mut file_with_line_number = Vec::new();
let modified_label;
let hunk_label;
if config.navigate {
modified_label = format!("{} ", config.file_modified_label);
file_with_line_number.push(config.hunk_header_file_style.paint(&modified_label));
hunk_label = format!("{} ", config.hunk_label);
file_with_line_number.push(config.hunk_header_file_style.paint(&hunk_label));
}
let plus_line_number = line_numbers[line_numbers.len() - 1].0;
if config.hunk_header_style_include_file_path {

View File

@ -132,6 +132,7 @@ pub fn set_options(
file_modified_label,
file_removed_label,
file_renamed_label,
hunk_label,
file_style,
hunk_header_decoration_style,
hunk_header_file_style,