diff options
author | Catherine Noll <noll.catherine@gmail.com> | 2021-03-21 11:45:54 -0500 |
---|---|---|
committer | Catherine Noll <noll.catherine@gmail.com> | 2021-03-21 11:45:54 -0500 |
commit | 3572ec451f78270fc5aa8348e6ac716a32ab366c (patch) | |
tree | 79aa714c552508e83ddeaadf17ca4c52ef3ac413 | |
parent | 43f971512594d931b97a61313612601ea5af6d0c (diff) |
Navigate from theme to theme with n/N when --show-themes
-rw-r--r-- | src/config.rs | 31 | ||||
-rw-r--r-- | src/features/navigate.rs | 26 | ||||
-rw-r--r-- | src/main.rs | 10 |
3 files changed, 52 insertions, 15 deletions
diff --git a/src/config.rs b/src/config.rs index 23b7b97d..eaa295ea 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ use crate::cli; use crate::color; use crate::delta::State; use crate::env; +use crate::features::navigate; use crate::features::side_by_side; use crate::git_config::GitConfigEntry; use crate::style::{self, Style}; @@ -58,6 +59,7 @@ pub struct Config { pub minus_non_emph_style: Style, pub minus_style: Style, pub navigate: bool, + pub navigate_regexp: Option<String>, pub null_style: Style, pub null_syntect_style: SyntectStyle, pub paging_mode: PagingMode, @@ -154,6 +156,24 @@ impl From<cli::Opt> for Config { _ => *style::GIT_DEFAULT_PLUS_STYLE, }; + let file_added_label = opt.file_added_label; + let file_copied_label = opt.file_copied_label; + 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 navigate_regexp = if opt.navigate || opt.show_themes { + Some(navigate::make_navigate_regexp( + opt.show_themes, + &file_modified_label, + &file_added_label, + &file_removed_label, + &file_renamed_label, + )) + } else { + None + }; + Self { available_terminal_width: opt.computed.available_terminal_width, background_color_extends_to_terminal_width: opt @@ -163,11 +183,11 @@ impl From<cli::Opt> for Config { color_only: opt.color_only, decorations_width: opt.computed.decorations_width, error_exit_code: 2, // Use 2 for error because diff uses 0 and 1 for non-error. - file_added_label: opt.file_added_label, - file_copied_label: opt.file_copied_label, - file_modified_label: opt.file_modified_label, - file_removed_label: opt.file_removed_label, - file_renamed_label: opt.file_renamed_label, + file_added_label, + file_copied_label, + file_modified_label, + file_removed_label, + file_renamed_label, file_style, git_config_entries: opt.git_config_entries, hunk_header_file_style, @@ -203,6 +223,7 @@ impl From<cli::Opt> for Config { minus_non_emph_style, minus_style, navigate: opt.navigate, + navigate_regexp, null_style: Style::new(), null_syntect_style: SyntectStyle::default(), paging_mode: opt.computed.paging_mode, diff --git a/src/features/navigate.rs b/src/features/navigate.rs index a38815a6..0020d54b 100644 --- a/src/features/navigate.rs +++ b/src/features/navigate.rs @@ -23,14 +23,22 @@ pub fn make_feature() -> Vec<(String, OptionValueFunction)> { ]) } -fn make_navigate_regexp(config: &Config) -> String { - format!( - "^(commit|{}|{}|{}|{})", - config.file_modified_label, - config.file_added_label, - config.file_removed_label, - config.file_renamed_label - ) +// Construct the regexp used by less for paging, if --show-themes or --navigate is enabled. +pub fn make_navigate_regexp( + show_themes: bool, + file_modified_label: &str, + file_added_label: &str, + file_removed_label: &str, + file_renamed_label: &str, +) -> String { + if show_themes { + "^Theme:".to_string() + } else { + format!( + "^(commit|{}|{}|{}|{})", + file_modified_label, file_added_label, file_removed_label, file_renamed_label, + ) + } } // Create a less history file to be used by delta's child less process. This file is initialized @@ -57,7 +65,7 @@ pub fn copy_less_hist_file_and_append_navigate_regexp(config: &Config) -> std::i std::fs::File::create(&delta_less_hist_file)?, "{}\"{}", contents, - make_navigate_regexp(config) + config.navigate_regexp.as_ref().unwrap(), )?; Ok(delta_less_hist_file) } diff --git a/src/main.rs b/src/main.rs index d48a2961..35edb03c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -258,6 +258,7 @@ fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::Resu " max-line-distance = {max_line_distance} max-line-length = {max_line_length} navigate = {navigate} + navigate-regexp = {navigate_regexp} paging = {paging_mode} side-by-side = {side_by_side} syntax-theme = {syntax_theme} @@ -267,6 +268,10 @@ fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::Resu max_line_distance = config.max_line_distance, max_line_length = config.max_line_length, navigate = config.navigate, + navigate_regexp = match &config.navigate_regexp { + None => "".to_string(), + Some(s) => s.to_string(), + }, paging_mode = match config.paging_mode { PagingMode::Always => "always", PagingMode::Never => "never", @@ -328,7 +333,10 @@ fn show_themes() -> std::io::Result<()> { }; let mut git_config = git_config::GitConfig::try_create(); - let opt = cli::Opt::from_iter_and_git_config(&["", "", "--navigate"], &mut git_config); + let opt = cli::Opt::from_iter_and_git_config( + &["", "", "--navigate", "--show-themes"], + &mut git_config, + ); let mut output_type = OutputType::from_mode(PagingMode::Always, None, &config::Config::from(opt)).unwrap(); |