| 1 | use std::io::Write; |
| 2 | #[cfg(target_os = "windows")] |
| 3 | use std::io::{Error, ErrorKind}; |
| 4 | use std::path::PathBuf; |
| 5 | |
| 6 | use crate::features::OptionValueFunction; |
| 7 | use crate::utils::bat::output::PagerCfg; |
| 8 | |
| 9 | pub fn make_feature() -> Vec<(String, OptionValueFunction)> { |
| 10 | builtin_feature!([ |
| 11 | ( |
| 12 | "navigate", |
| 13 | bool, |
| 14 | None, |
| 15 | _opt => true |
| 16 | ), |
| 17 | ( |
| 18 | "file-modified-label", |
| 19 | String, |
| 20 | None, |
| 21 | _opt => "Δ" |
| 22 | ), |
| 23 | ( |
| 24 | "hunk-label", |
| 25 | String, |
| 26 | None, |
| 27 | _opt => "•" |
| 28 | ) |
| 29 | ]) |
| 30 | } |
| 31 | |
| 32 | // Construct the regexp used by less for paging, if --show-themes or --navigate is enabled. |
| 33 | pub fn make_navigate_regex( |
| 34 | show_themes: bool, |
| 35 | file_modified_label: &str, |
| 36 | file_added_label: &str, |
| 37 | file_removed_label: &str, |
| 38 | file_renamed_label: &str, |
| 39 | hunk_label: &str, |
| 40 | ) -> String { |
| 41 | if show_themes { |
| 42 | "^Theme:".to_string() |
| 43 | } else { |
| 44 | let optional_regexp = |find: &str| { |
| 45 | if !find.is_empty() { |
| 46 | format!("|{}", regex::escape(find)) |
| 47 | } else { |
| 48 | "".to_string() |
| 49 | } |
| 50 | }; |
| 51 | format!( |
| 52 | "^(commit{}{}{}{}{})", |
| 53 | optional_regexp(file_added_label), |
| 54 | optional_regexp(file_removed_label), |
| 55 | optional_regexp(file_renamed_label), |
| 56 | optional_regexp(file_modified_label), |
| 57 | optional_regexp(hunk_label), |
| 58 | ) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Create a less history file to be used by delta's child less process. This file is initialized |
| 63 | // with the contents of user's real less hist file, to which the navigate regex is appended. This |
| 64 | // has the effect that 'n' or 'N' in delta's less process will search for the navigate regex, |
| 65 | // without the undesirable aspects of using --pattern, yet without polluting the user's less search |
| 66 | // history with delta's navigate regex. See |
| 67 | // https://github.com/dandavison/delta/issues/237#issuecomment-780654036. Note that with the |
| 68 | // current implementation, no writes to the delta less history file are propagated back to the real |
| 69 | // history file so, for example, a (non-navigate) search performed in the delta less process will |
| 70 | // not be stored in history. |
| 71 | pub fn copy_less_hist_file_and_append_navigate_regex( |
| 72 | config: &PagerCfg, |
| 73 | ) -> std::io::Result<PathBuf> { |
| 74 | let delta_less_hist_file = get_delta_less_hist_file()?; |
| 75 | let initial_contents = ".less-history-file:\n".to_string(); |
| 76 | let mut contents = if let Some(hist_file) = get_less_hist_file() { |
| 77 | std::fs::read_to_string(hist_file).unwrap_or(initial_contents) |
| 78 | } else { |
| 79 | initial_contents |
| 80 | }; |
| 81 | if !contents.ends_with(".search\n") { |
| 82 | contents = format!("{contents}.search\n"); |
| 83 | } |
| 84 | writeln!( |
| 85 | std::fs::File::create(&delta_less_hist_file)?, |
| 86 | "{}\"{}", |
| 87 | contents, |
| 88 | config.navigate_regex.as_ref().unwrap(), |
| 89 | )?; |
| 90 | Ok(delta_less_hist_file) |
| 91 | } |
| 92 | |
| 93 | #[cfg(target_os = "windows")] |
| 94 | fn get_delta_less_hist_file() -> std::io::Result<PathBuf> { |
| 95 | let mut path = dirs::data_local_dir() |
| 96 | .ok_or_else(|| Error::new(ErrorKind::NotFound, "Can't find AppData\\Local folder"))?; |
| 97 | path.push("delta"); |
| 98 | std::fs::create_dir_all(&path)?; |
| 99 | path.push("delta.lesshst"); |
| 100 | Ok(path) |
| 101 | } |
| 102 | |
| 103 | #[cfg(not(target_os = "windows"))] |
| 104 | fn get_delta_less_hist_file() -> std::io::Result<PathBuf> { |
| 105 | let dir = xdg::BaseDirectories::with_prefix("delta")?; |
| 106 | dir.place_data_file("lesshst") |
| 107 | } |
| 108 | |
| 109 | // Get path of the less history file. See `man less` for more details. |
| 110 | // On Unix, check all possible locations and pick the newest file. |
| 111 | fn get_less_hist_file() -> Option<PathBuf> { |
| 112 | if let Some(home_dir) = dirs::home_dir() { |
| 113 | match std::env::var("LESSHISTFILE").as_deref() { |
| 114 | Ok("-") | Ok("/dev/null") => { |
| 115 | // The user has explicitly disabled less history. |
| 116 | None |
| 117 | } |
| 118 | Ok(path) => { |
| 119 | // The user has specified a custom histfile. |
| 120 | Some(PathBuf::from(path)) |
| 121 | } |
| 122 | Err(_) => { |
| 123 | // The user is using the default less histfile location. |
| 124 | #[cfg(unix)] |
| 125 | { |
| 126 | // According to the less 643 manual: |
| 127 | // "$XDG_STATE_HOME/lesshst" or "$HOME/.local/state/lesshst" or |
| 128 | // "$XDG_DATA_HOME/lesshst" or "$HOME/.lesshst". |
| 129 | let xdg_dirs = xdg::BaseDirectories::new().ok()?; |
| 130 | [ |
| 131 | xdg_dirs.get_state_home().join("lesshst"), |
| 132 | xdg_dirs.get_data_home().join("lesshst"), |
| 133 | home_dir.join(".lesshst"), |
| 134 | ] |
| 135 | .iter() |
| 136 | .filter(|path| path.exists()) |
| 137 | .max_by_key(|path| { |
| 138 | std::fs::metadata(path) |
| 139 | .and_then(|m| m.modified()) |
| 140 | .unwrap_or(std::time::UNIX_EPOCH) |
| 141 | }) |
| 142 | .cloned() |
| 143 | } |
| 144 | #[cfg(not(unix))] |
| 145 | { |
| 146 | let mut hist_file = home_dir; |
| 147 | hist_file.push(if cfg!(windows) { |
| 148 | "_lesshst" |
| 149 | } else { |
| 150 | ".lesshst" |
| 151 | }); |
| 152 | Some(hist_file) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } else { |
| 157 | None |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | #[cfg(test)] |
| 162 | mod tests { |
| 163 | use std::fs::remove_file; |
| 164 | |
| 165 | use crate::tests::integration_test_utils; |
| 166 | |
| 167 | #[test] |
| 168 | #[ignore] |
| 169 | // manually verify: cargo test -- test_get_less_hist_file --ignored --nocapture |
| 170 | fn test_get_less_hist_file() { |
| 171 | let hist_file = super::get_less_hist_file(); |
| 172 | dbg!(hist_file); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn test_navigate_with_overridden_key_in_main_section() { |
| 177 | let git_config_contents = b" |
| 178 | [delta] |
| 179 | features = navigate |
| 180 | file-modified-label = \"modified: \" |
| 181 | "; |
| 182 | let git_config_path = "delta__test_navigate_with_overridden_key_in_main_section.gitconfig"; |
| 183 | |
| 184 | assert_eq!( |
| 185 | integration_test_utils::make_options_from_args_and_git_config(&[], None, None) |
| 186 | .file_modified_label, |
| 187 | "" |
| 188 | ); |
| 189 | assert_eq!( |
| 190 | integration_test_utils::make_options_from_args_and_git_config( |
| 191 | &["--features", "navigate"], |
| 192 | None, |
| 193 | None |
| 194 | ) |
| 195 | .file_modified_label, |
| 196 | "Δ" |
| 197 | ); |
| 198 | assert_eq!( |
| 199 | integration_test_utils::make_options_from_args_and_git_config( |
| 200 | &["--navigate"], |
| 201 | None, |
| 202 | None |
| 203 | ) |
| 204 | .file_modified_label, |
| 205 | "Δ" |
| 206 | ); |
| 207 | assert_eq!( |
| 208 | integration_test_utils::make_options_from_args_and_git_config( |
| 209 | &[], |
| 210 | Some(git_config_contents), |
| 211 | Some(git_config_path) |
| 212 | ) |
| 213 | .file_modified_label, |
| 214 | "modified: " |
| 215 | ); |
| 216 | |
| 217 | remove_file(git_config_path).unwrap(); |
| 218 | } |
| 219 | |
| 220 | #[test] |
| 221 | fn test_navigate_with_overridden_key_in_custom_navigate_section() { |
| 222 | let git_config_contents = b" |
| 223 | [delta] |
| 224 | features = navigate |
| 225 | |
| 226 | [delta \"navigate\"] |
| 227 | file-modified-label = \"modified: \" |
| 228 | "; |
| 229 | let git_config_path = |
| 230 | "delta__test_navigate_with_overridden_key_in_custom_navigate_section.gitconfig"; |
| 231 | |
| 232 | assert_eq!( |
| 233 | integration_test_utils::make_options_from_args_and_git_config(&[], None, None) |
| 234 | .file_modified_label, |
| 235 | "" |
| 236 | ); |
| 237 | assert_eq!( |
| 238 | integration_test_utils::make_options_from_args_and_git_config( |
| 239 | &["--features", "navigate"], |
| 240 | None, |
| 241 | None |
| 242 | ) |
| 243 | .file_modified_label, |
| 244 | "Δ" |
| 245 | ); |
| 246 | assert_eq!( |
| 247 | integration_test_utils::make_options_from_args_and_git_config( |
| 248 | &[], |
| 249 | Some(git_config_contents), |
| 250 | Some(git_config_path) |
| 251 | ) |
| 252 | .file_modified_label, |
| 253 | "modified: " |
| 254 | ); |
| 255 | |
| 256 | remove_file(git_config_path).unwrap(); |
| 257 | } |
| 258 | |
| 259 | #[test] |
| 260 | fn test_navigate_activated_by_custom_feature() { |
| 261 | let git_config_contents = b" |
| 262 | [delta \"my-navigate-feature\"] |
| 263 | features = navigate |
| 264 | file-modified-label = \"modified: \" |
| 265 | "; |
| 266 | let git_config_path = "delta__test_navigate_activated_by_custom_feature.gitconfig"; |
| 267 | |
| 268 | assert_eq!( |
| 269 | integration_test_utils::make_options_from_args_and_git_config( |
| 270 | &[], |
| 271 | Some(git_config_contents), |
| 272 | Some(git_config_path) |
| 273 | ) |
| 274 | .file_modified_label, |
| 275 | "" |
| 276 | ); |
| 277 | assert_eq!( |
| 278 | integration_test_utils::make_options_from_args_and_git_config( |
| 279 | &["--features", "my-navigate-feature"], |
| 280 | Some(git_config_contents), |
| 281 | Some(git_config_path) |
| 282 | ) |
| 283 | .file_modified_label, |
| 284 | "modified: " |
| 285 | ); |
| 286 | |
| 287 | remove_file(git_config_path).unwrap(); |
| 288 | } |
| 289 | } |