Skip to content

Commit

Permalink
Make it possible to jump between files when navigate is active (#684)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dandavison committed Aug 21, 2021
1 parent 512ccbc commit 130b0b6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -186,6 +188,7 @@ impl From<cli::Opt> for Config {
&file_added_label,
&file_removed_label,
&file_renamed_label,
&hunk_label,
))
} else {
None
Expand All @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions src/features/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub fn make_feature() -> Vec<(String, OptionValueFunction)> {
String,
None,
_opt => "Δ"
),
(
"hunk-label",
String,
None,
_opt => "δ"
)
])
}
Expand All @@ -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),
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/hunk_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 130b0b6

Please sign in to comment.