Skip to content

Commit bae289e

Browse files
authored
less hist file: look at xdg paths (#2065)
Newer less versions prefer xdg compliant paths, but will continue to use existing locations.
1 parent a7b14ea commit bae289e

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/features/navigate.rs

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,8 @@ fn get_delta_less_hist_file() -> std::io::Result<PathBuf> {
106106
dir.place_data_file("lesshst")
107107
}
108108

109-
// LESSHISTFILE
110-
// Name of the history file used to remember search commands
111-
// and shell commands between invocations of less. If set to
112-
// "-" or "/dev/null", a history file is not used. The
113-
// default is "$HOME/.lesshst" on Unix systems,
114-
// "$HOME/_lesshst" on DOS and Windows systems, or
115-
// "$HOME/lesshst.ini" or "$INIT/lesshst.ini" on OS/2
116-
// systems.
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.
117111
fn get_less_hist_file() -> Option<PathBuf> {
118112
if let Some(home_dir) = dirs::home_dir() {
119113
match std::env::var("LESSHISTFILE").as_deref() {
@@ -122,18 +116,41 @@ fn get_less_hist_file() -> Option<PathBuf> {
122116
None
123117
}
124118
Ok(path) => {
125-
// The user has specified a custom histfile
119+
// The user has specified a custom histfile.
126120
Some(PathBuf::from(path))
127121
}
128122
Err(_) => {
129123
// The user is using the default less histfile location.
130-
let mut hist_file = home_dir;
131-
hist_file.push(if cfg!(windows) {
132-
"_lesshst"
133-
} else {
134-
".lesshst"
135-
});
136-
Some(hist_file)
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+
}
137154
}
138155
}
139156
} else {
@@ -147,6 +164,14 @@ mod tests {
147164

148165
use crate::tests::integration_test_utils;
149166

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+
150175
#[test]
151176
fn test_navigate_with_overridden_key_in_main_section() {
152177
let git_config_contents = b"

0 commit comments

Comments
 (0)