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

Conversation

rashil2000
Copy link
Contributor

Closes #772

@dandavison
Copy link
Owner

In case it's helpful at any point, the original PR adding Windows and non-Windows navigate history files is #526

.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!)

@dandavison dandavison merged commit 87156fb into dandavison:master Dec 11, 2021
@dandavison
Copy link
Owner

Thanks very much for the work here @rashil2000. If you're interested in making any more improvements to delta that would of course be fantastic. We have a guide to the codebase here: ARCHITECTURE.md, and I am always happy to answer questions about development directions, implementation, etc.

@rashil2000 rashil2000 deleted the lesshst-win branch December 11, 2021 19:32
@rashil2000
Copy link
Contributor Author

Thank you @dandavison!

I use delta all day, and will be very happy to contribute to it! But I'm very new to Rust itself, so it might be a while before I make any productive contribution 😅

@dandavison
Copy link
Owner

I use delta all day, and will be very happy to contribute to it!

Excellent!

But I'm very new to Rust itself, so it might be a while before I make any productive contribution 😅

I wouldn't be so sure of that :) -- there's quite a lot of code in delta providing examples of how things are done, and lots of tests providing examples of how to test things, so it might not be a bad way to learn Rust.

Maybe the most important thing is finding something to work on which you personally care about, and which is also the right level of difficulty for an initial project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 Store .delta.lesshst in a location other than Home (Windows)
2 participants