-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add ability to customize the amount of path shortening in prompt_pwd #2473
Conversation
Allows the length of each shortened path component to be customized by setting the `fish_prompt_pwd_abbr` variable to the number of extra characters to include. Maintains the default behavior of shortening path components to just one character. You can also set `fish_prompt_pwd_abbr` to the empty value to disable shortening completely.
I see that people want this (I can't say I'm in love with one-char-per-dir), but I'm not sure about this solution. I think of functions as the better customization point - the reasoning being that you can actually add comments to them, and have more space to explain what it does in general. If I want to change how the pwd is printed in my prompt, I type either However, I'm not completely opposed, but I have three reservations regarding the finer details of the implementation too:
I've been trying the following match-regex:
which will print up to fish_prompt_pwd_abbr plus a leading dot (if any exists). The issue being that it completely obliterates all directories when set to 0, which doesn't seem useful. (Of course an explicit check could be added that sets it to 1 locally if it's set to 0) \3. Can anyone think of a better name for the variable? "fish_prompt_pwd_abbr" sounds like an abbreviation for the prompt or a variable that turns abbreviation on. It doesn't need to be "fish_number_of_characters_per_directory_in_prompt_pwd", but maybe fish_prompt_pwd_abbrchars or so? (Markdowns numbered lists are kind of annoying) There might be other things to do with prompt_pwd - maybe only shorten if a directory is more than a certain number of characters and then mark it (with a nice unicode "…" or so), or actually ellipsize it (i.e. cut in the middle). Or make the pwd actually fill the prompt up to some number of characters (though this would need a bit of re-architecting). |
@garyp: Ping? Do you want to work on my criticisms or should I? |
Yeah, there are definitely many prompt customizations that people might want and it's hard to know where to draw the line between customization that can be accomplished via knobs such as this one, and customization that requires the user to override the whole I'd like to think that changing the number of characters is going to be a common-enough need that it should be supported within the default There is precedent for non-function customization within fish by using universal variables: they're used for configuring colors. However if there's a different preferred mechanism for exposing knobs to users, I can update this pull request to use it instead.
Makes sense. I'll make this change.
Agreed. I also wasn't too happy with having it represent extra characters instead of total characters. I'll try to come up with an expression that would allow
How about
I actually really like the idea of adding a Unicode ellipsis to indicate shortened paths. I'll add it as default behavior in this pull request unless you think that it warrants a separate discussion. |
Yeah, though I do think in this particular case prompt_pwd should be reasonably stable - I don't anticipate many more changes.
I don't generally buy the idea of the slippery slope - we're capable of evaluating every step on its own, and we're capable of saying "stop".
That's not quite the same thing - the colors are used all over the place, even in C++ code (AFAIK). Anyway, I've thought about this, and I think using variables is okay - though I've also thought about allowing it as an argument to prompt_pwd - this would allow prompts to override it (though I'm not too sure how useful that is). So what I'd say is:
I've also worked on this a bit, and what I've come up with is: function prompt_pwd --description 'Print the current working directory, shortened to fit the prompt'
set realhome ~
# This allows overriding fish_prompt_pwd_abbr from the outside (global or universal) without leaking it
set -q fish_prompt_pwd_abbr; or set -l fish_prompt_pwd_abbr 1
# An unfortunate technicality means a value of 0 would shorten the prompt to (e.g.) //////lastdirectory - disable shortening instead
[ $fish_prompt_pwd_abbr -eq 0 ]; and set -l fish_prompt_pwd_abbr 1
# Replace $HOME with "~"
set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $PWD)
# Shorten to at most $fish_prompt_pwd_abbr characters per directory
string replace -ar '(\.{0,1})([^/]{0,'"$fish_prompt_pwd_abbr"'})[^/]*/' '$1$2/' -- $tmp
end which does just that.
Sounds groovy! (I've been working on my adjectives)
Humm.... I'll have to check how well we're currently doing with non-Unicode locales. If we're usually doing great, this would break it more (so it'd need a fallback if we're not unicode-capable). Otherwise it'd be nice as default. |
Oh yes. I've gotten too used to having Unicode support everywhere I care about. I'll leave out the Unicode ellipsis then unless there's a recommended way of testing for Unicode support from a fish function? |
@faho let me know what you think of the updated version. I wasn't sure if your previous comment was a request to add support for a I'm not familiar with the code review process the fish-shell project uses. Do you prefer that I squash my commits as I go or do it at the end? Also wasn't sure what the whitespace rules are. The |
I'll do it at the end.
We do whatever |
I've now tested this a bit and it looks good - it also disables shortening for invalid values, which is nice. The last thing I've been thinking about is how to document this. I mean it should be MENTIONED IN THE RELEASE NOTES anyway, but I'm not sure if we don't want to add a section to the docs - maybe a "Useful functions for writing your own prompt" section like we have for completions, or a complete section on special variables (or customization points in general)? Of course that can be done later - I'll add a label to remind me (or anyone else looking to write documentation). @garyp: Is there anything else you'd like to do or can I merge this? |
@faho I'm all done. Feel free to squash & merge. |
@faho, OK, OK, shall we make a separate tag? |
@zanchey: Made the label "releasenotes". Now to apply it. |
Merged as 09bd938. Thanks! |
- VIMODE: remove force delete of fish_mode_prompt as it was shown the first time nonetheless. Will require the user to deactivate the default fish_mode_promopt by creating an empty function - PWD: now uses prompt_pwd instead of the path directly as some modification will be added to this function alowing the user to set the abbreviation length (fish-shell/fish-shell#2473)
Allows the length of each shortened path component to be customized by setting the
fish_prompt_pwd_abbr
variable to the number of extra characters to include. Maintains the default behavior of shortening path components to just one character. You can also setfish_prompt_pwd_abbr
to the empty value to disable shortening completely.