Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store history file in data_local_dir for Windows #847

Merged
merged 2 commits into from
Dec 11, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/features/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ pub fn copy_less_hist_file_and_append_navigate_regex(config: &Config) -> std::io

#[cfg(target_os = "windows")]
fn get_delta_less_hist_file() -> std::io::Result<PathBuf> {
let mut path = dirs_next::home_dir()
.ok_or_else(|| Error::new(ErrorKind::NotFound, "Can't determine home dir"))?;
path.push(".delta.lesshst");
let mut path = dirs_next::data_local_dir()
.ok_or_else(|| Error::new(ErrorKind::NotFound, "Can't determine LocalAppData dir"))?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my only question is this:

How often will data_local_dir return None on users' Windows systems? Should we fall back to home_dir, or something that we can guarantee will exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LocalAppData folder has been a part of Windows since XP if I remember correctly (it might even be older, but I don't think any Rust toolchain would support that far back).

I think it is safe to assume it will always exist, unless a user explicitly deletes it (which is highly unlikely, because this is a hidden folder). Even after deletion, Windows will recreate it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rashil2000.

"Can't determine LocalAppData dir"

Would this be slightly more helpful to Windows users if it said

"Can't find AppData\Local folder"

?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I guess I have in mind making it helpful to less expert as well as more expert users)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think that makes sense.

Before Vista, the location was actually different, but we can leave that out for the sake of brevity.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks @rashil2000.

For the record, I see that we're not currently emitting this error message: https://github.com/dandavison/delta/blob/master/src/utils/bat/output.rs#L165-L170

But that's not your fault and a distinct issue from what you're solving here. (I haven't been quite sure how/whether to alert the user to a problem like that: a fatal error is maybe too much? But randomly emitting messages on stderr interleaved with their expected output on stdout is not very elegant.)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that your message is displayed as expected on macOS with this patch:

diff --git a/src/features/navigate.rs b/src/features/navigate.rs
index db582dd..a12e87e 100644
--- a/src/features/navigate.rs
+++ b/src/features/navigate.rs
@@ -1,5 +1,4 @@
 use std::io::Write;
-#[cfg(target_os = "windows")]
 use std::io::{Error, ErrorKind};
 use std::path::PathBuf;
 
@@ -88,22 +87,15 @@ pub fn copy_less_hist_file_and_append_navigate_regex(config: &Config) -> std::io
     Ok(delta_less_hist_file)
 }
 
-#[cfg(target_os = "windows")]
 fn get_delta_less_hist_file() -> std::io::Result<PathBuf> {
-    let mut path = dirs_next::data_local_dir()
-        .ok_or_else(|| Error::new(ErrorKind::NotFound, "Can't find AppData\\Local folder"))?;
+    let mut path: PathBuf =
+        None.ok_or_else(|| Error::new(ErrorKind::NotFound, "Can't find AppData\\Local folder"))?;
     path.push("delta");
     std::fs::create_dir_all(&path)?;
     path.push("delta.lesshst");
     Ok(path)
 }
 
-#[cfg(not(target_os = "windows"))]
-fn get_delta_less_hist_file() -> std::io::Result<PathBuf> {
-    let dir = xdg::BaseDirectories::with_prefix("delta")?;
-    dir.place_data_file("lesshst")
-}
-
 // LESSHISTFILE
 //        Name of the history file used to remember search commands
 //        and shell commands between invocations of less.  If set to
diff --git a/src/utils/bat/output.rs b/src/utils/bat/output.rs
index 62761c2..3e390a4 100644
--- a/src/utils/bat/output.rs
+++ b/src/utils/bat/output.rs
@@ -162,10 +162,15 @@ fn _make_process_from_less_path(
         p.env("LESSCHARSET", "UTF-8");
         p.env("LESSANSIENDCHARS", "mK");
         if config.navigate {
-            if let Ok(hist_file) = navigate::copy_less_hist_file_and_append_navigate_regex(config) {
-                p.env("LESSHISTFILE", hist_file);
-                if config.show_themes {
-                    p.arg("+n");
+            match navigate::copy_less_hist_file_and_append_navigate_regex(config) {
+                Ok(hist_file) => {
+                    p.env("LESSHISTFILE", hist_file);
+                    if config.show_themes {
+                        p.arg("+n");
+                    }
+                }
+                Err(err) => {
+                    fatal(format!("Error: {}", err));
                 }
             }
         }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's not your fault and a distinct issue from what you're solving here. (I haven't been quite sure how/whether to alert the user to a problem like that: a fatal error is maybe too much? But randomly emitting messages on stderr interleaved with their expected output on stdout is not very elegant.)

Is there an issue open for this? I'll take a look and try to gather what I can.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no open issue yet. (There are arguably more important bugs though!)

path.push("delta");
std::fs::create_dir_all(&path)?;
path.push("delta.lesshst");
Ok(path)
}

Expand Down