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
refactor(directory): Introduce logical-path
argument which allows a shell to explicitly specify both a logical and physical filesystem path
#2104
Conversation
… shell to explicitly specify both a logical and physical filesystem path Fix `directory::module` to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction behavior is maintained, based on the logical path if it is set, or the physical path if it is not. The custom "get_current_dir" logic has been removed entirely, and the `directory` module now relies on `context.current_dir` / `context.logical_dir` entirely. Changes have been made to `init/starship.ps1` to work with this new flag: - Calculate and pass "physical" and "logical" paths explicitly (as other shells do not pass `--logical-path` that they fall back to rendering the physical path) - Moved the "powershell provider prefix" cleanup code to the PowerShell script - this code _should_ now support any kind of powershell path prefix.
b5ea856
to
7ccb0c5
Compare
Something I noted during testing is that navigating to a repository directory with a non-physical logical directory "breaks" the repo truncation behavior: I don't think this is a regression, but something we could improve. Edit: diff --git a/src/modules/directory.rs b/src/modules/directory.rs
index 85da1c3..80a9a82 100644
--- a/src/modules/directory.rs
+++ b/src/modules/directory.rs
@@ -56,7 +56,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let dir_string = repo
.and_then(|r| r.root.as_ref())
.filter(|root| *root != &home_dir)
- .and_then(|root| contract_repo_path(&logical_dir, root))
+ .and_then(|root| contract_repo_path(&physical_dir, root))
.unwrap_or_else(|| contract_path(&logical_dir, &home_dir, HOME_SYMBOL));
log::debug!("Dir string: {}", dir_string); |
… causing command line parsing issues. This is a bit of a footgun! The work-around chosen is to append a trailing space when a path string ends with a backslash, and then trim any extra whitespace away in the Context constructor. Other alternatives considered and rejected: 1. Always trim trailing backslashes as the filesystem generally doesn't need them. 2. Escape trailing backslashes with another backslash. This proved complex as PS only quotes string args when the string includes some whitespace, and other backslashes within the string apparently don't need to be escaped.
But this could cause unexpected behavious outside powershell (symlinks etc). Maybe a third |
Other shells do not pass |
… executables with strings which may contain characters which need to be escaped carefully.
These were in place to clean up extra whitespace sometimes injected by starship.ps1::prompt, and are no longer required with the new Invoke-Native helper in place.
In traditional shells: The physical path is the path on disk with all symlinks resolved. ( You can't just remove the logical handling for the traditional shells. I think something like this might work for
|
Ok, I think I understand what you're getting at here now. It seems like we should aim to standardize what data
|
That would work, but
I think part of the aim for this pr for |
Looking at the existing code, I can only see one call to
My initial goal at least was just to make it possible to override the logical path from the command line and the addition of |
4986a1b
to
7e9a8b4
Compare
…ng it to `current_dir` but overridable by the `--logical-dir` flag. - Restore `use_logical_path` config flag. - Always attempt to contract repo paths from the `current_dir`.
7e9a8b4
to
892ce25
Compare
With the old code
I think having |
@davidkna On further investigation it may not be safe to always
|
This keeps the two calls to contract_path in sync.
54874f5
to
6e0a083
Compare
This might have the upside of getting starship to support long paths on windows! (I think it might be best to run that on |
… when use_logical_path = false - This requires some clean-up to remove the extended-path prefix on Windows - The configured logical_dir is ignored entirely in this mode - we calculate a new logical_dir by cleaning up the physical_dir path for display. - Test coverage
…ay string later in the render process
@davidkna I think I've found a robust solution for dealing with with comparisons between verbatim and non-verbatim paths (verbatim is the Rust std lib's name for the Windows extended-path prefixes). I've added two routines for doing If there are any other modules doing "starts with" checks on |
I've raised a PR to fix path-slash: rhysd/path-slash#5 |
…h` with variations of verbatim and non-verbatim paths
81d5a40
to
0274ad7
Compare
This fixes the issue with the trailing character of some Windows paths being truncated, e.g. `\\server\share` and `C:`
Updated path-slash to latest. This fixes the issue with the trailing character of some Windows paths being truncated, e.g. |
- Use `ProcessStartInfo` to launch native executable, replacing manual UTF8 output encoding handling - If we detect we're on PWSH6+ use the new `System.Diagnostics.ProcessStartInfo.ArgumentList` parameter, otherwise manually escape the argument string - Move `Get-Cwd` and `Invoke-Native` into the prompt function scope so that they don't leak into the user's shell scope
86bed93
to
00309ae
Compare
…handling Conflicts: - src/test/mod.rs - took local changes, ported upstream changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. But because this PR changes some of the fundamentals I'd prefer a second review.
…-path-handling Conflicts: - src/modules/directory.rs - Took local, ported remote changes (use context.get_home).
…l-path-handling Conflicts: - src/modules/directory.rs - ported new home_symbol handling
@davidkna Is there still interest in merging this? |
I agree that this should be merged soon. Unless someone finds any issues or objects to merging I think I'll merge this tomorrow. |
Description
Introduce
logical-path
argument which allows a shell to explicitly specify both a logical and physical filesystem path.Fix
directory::module
to consume both path and logical-path (if provided). The "logical" path is preferred when rendering the "display path", while the "physical" path is used to resolve the "read only" flag. Repo- and home-directory contraction is based on the logical path if it is set, or the physical path if it is not.The custom "get_current_dir" logic has been removed entirely, and the
directory
module now relies oncontext.current_dir
/context.logical_dir
directly.Changes have been made to
init/starship.ps1
to work with this new flag:--logical-path
they always rendering the based on the physical path)Motivation and Context
I discovered this issue while testing
starship
with the Nu shell. It's a WIP shell, so still has some bugs. I switched to passing--path
to work around one such issue and found thatstarship
was inconsistent with how it handled the argument.This screenshot demonstrates the problem:
Specifically, invoking the command
starship prompt --path C:\Windows
:PWD
environment variable, printsC:/Windows {lock symbol}
✔️PWD
environment variable, prints{the PWD env variable} {lock symbol}
❌The issue is caused by the
directory
module functionget_current_dir
which looks to thePWD
environment variable to select the path it is going to display to the user, overriding the provided--path
argument.To resolve this, I've introduced a new
prompt
argument--logical-path
- rather than setting the PWD environment variable, a shell with "logical" path display requirements can instead provide--logical-path
to set the logical path.The same command as above, but with the fix applied:
See discussion on #2095 for more context.
This (abandoned?) PR #1209 also looked to introduce
--logical-path
as an alternative to PWD.How Has This Been Tested?
I tested manually using the following scripts:
Windows/PowerShell:
(results)
Linux/bash:
(results)
Checklist: