Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

[Bugfix] Fix extra tilde when using truncate_with_folder_marker under $HOME #1271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mr-salty
Copy link

truncate_with_folder_marker adds a tilde when it finds HOME, but
POWERLEVEL9K_HOME_FOLDER_ABBREVIATION is also prepended, so if you're
in a subdirectory of $HOME you get an extra tilde (although this does not
occur if you're actually in $HOME)

Without the change, the new test I added fails:

testHomeWithFolderMarkerWorks
ASSERT:expected:<%K{004} %F{000}~/powerlevel9k-test %k%F{004}%f > but was:<%K{004} %F{000}~~/powerlevel9k-test %k%F{004}%f >

truncate_with_folder_marker adds a tilde when it finds HOME, but
POWERLEVEL9K_HOME_FOLDER_ABBREVIATION is also prepended, so if you're
in a subdirectory of $HOME you get an extra tilde (although this does not
occur if you're actually in $HOME)

Without the change, the new test I added fails:
```
testHomeWithFolderMarkerWorks
ASSERT:expected:<%K{004} %F{000}~/powerlevel9k-test %k%F{004}%f > but was:<%K{004} %F{000}~~/powerlevel9k-test %k%F{004}%f >
```
@romkatv
Copy link

romkatv commented May 15, 2019

truncate_with_folder_marker might be more broken than this fix implies. This PR makes it work correctly as long as there are no markers, but in this case there is also no truncation. Try creating some markers and see what you get. Given how broken it is, I doubt anyone is using this truncation strategy other than by mistake.

@mr-salty
Copy link
Author

ah, I see what you mean. I can take a shot at fixing that as well. I filed #1272 to track it, if you have anything to add.

FWIW, I'm using it for a specific purpose where I didn't like any of the other strategies, but the directories where I have folder markers aren't under $HOME so I didn't notice.

@romkatv
Copy link

romkatv commented May 15, 2019

FWIW, I'm using it for a specific purpose where I didn't like any of the other strategies, but the directories where I have folder markers aren't under $HOME so I didn't notice.

Can you share extra details? What are the directories outside of your $HOME where you add markers? Are you committing markers to piper so that your citc paths are shortened?

In my opinion this shortening strategy shouldn't have existed. Instead, Powerlevel9k should respect ZSH named directories, both static and dynamic. If you can shed some light on how you are using truncate_with_folder_marker, it would help me understand whether my hunch is right. I could then add support for named directories, which not only would make paths look nicer for you, but those paths would be real in a sense that you can cd into them or use them as an argument to ls or anything else.

@mr-salty
Copy link
Author

Yeah, that's right (ah, didn't realize you were an xoogler), my directory is i.e.: /google/src/cloud/salty/client-name/google3; helpfully each client has .citc in its root, so I use that as a marker and get /…/client-name/google3 which saves real estate. I'd be happy to have some alias like citc show up there instead.

Semi-unrelated, but ideally I'd like to use the perforce vcs stuff and have that interact with the directory name. When I was using tcsh, my prompt was just <client-name>/google3. I hacked the zsh perforce vcs stuff to work but it was so slow I turned it back off until I have some time to speed it up (maybe I'll try it with 10k)... but it also didn't do anything for the directory name.

@romkatv
Copy link

romkatv commented May 15, 2019

Thanks for the explanation. I'll leave the discussion of p4/g4 integration for another time. As far as directory is concerned, here's what you can do in ZSH without any plugins or themes.

First, start zsh like so:

PROMPT='%~ ' zsh -df

This will give you a simple prompt that contains just your current directory. Notice how your home directory is abbreviated as ~.

You can define a static named directory for citc.

hash -d citc=/google/src/cloud/salty

Now, if you are in /google/src/cloud/salty/client-name/google3, your prompt will show ~citc/client-name/google3. Moreover, you can type cd ~citc/client-name and it'll work. You can even type cd ~citc/<TAB> and the completion system will work, too.

This is static named directory. It's static because for every named directory of this kind we need to run hash (you usually put these calls in ~/.zshrc).

There are also dynamic named directories, which allow you to have unlimited number of named directories whose names and targets depend on external data. They look like this: ~[name]. For example, you could make ~[client-name] map to /google/src/cloud/salty/client-name. Once again, this mapping will work everywhere the same way ~ <=> $HOME works. http://zsh.sourceforge.net/Doc/Release/Expansion.html#Dynamic-named-directories explains how to define dynamic named directories.

Static and dynamic named directories give you a lot of flexibility that doesn't require builtin support from Powerlevel9k. Plus, they integrate with ZSH seamlessly.

Ideally, Powerlevel9k should show named directories in the prompt just like native ZSH prompt does it. Unfortunately, it doesn't work. I think making it work would be valuable, and it's also not difficult.

FWIW, simple_dir in Powerlevel10k does support named directories. If you just want to try them, you can replace dir with simple_dir in your config, define a bunch of named directories and see if you like it. Note, however, that simple_dir doesn't support truncation and a few other configuration options. On the positive side it's super fast. Keep in mind that it's an undocumented segment that I threw together as a demo and thus it can disappear at any time.

@romkatv
Copy link

romkatv commented May 19, 2019

@mr-salty FYI: Powerlevel10k now supports named directories, both static and dynamic. All shortening strategies and other dir prompt options work properly when you are under a named directory.

You don't need to do anything special to enable it. Just update Powerlevel10k, add hash -d citc=/google/src/cloud/salty to your ~/.zshrc and you'll get a nice prompt.

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

Successfully merging this pull request may close these issues.

None yet

2 participants