Powerlevel9k / powerlevel9k

Powerlevel9k was a tool for building a beautiful and highly functional CLI, customized for you. P9k had a substantial impact on CLI UX, and its legacy is now continued by P10k.
https://github.com/romkatv/powerlevel10k
MIT License
13.46k stars 948 forks source link

POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_unique doesn't work #1277

Open romkatv opened 5 years ago

romkatv commented 5 years ago

To reproduce:

docker run -e LANG=C.UTF-8 -e LC_ALL=C.UTF-8 -e TERM=$TERM -it --rm ubuntu bash -uexc '
  apt update && apt install -y zsh git
  git clone https://github.com/bhilburn/powerlevel9k.git ~/powerlevel9k
  echo "
    POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir)
    POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=()
    POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_unique
    source ~/powerlevel9k/powerlevel9k.zsh-theme" >~/.zshrc
  cd ~/powerlevel9k/functions
  exec zsh'

Actual result: Directory in the prompt is not truncated.

prompt

Expected result: Directory in the prompt is truncated. It's unclear from the docs exactly how it should be truncated but truncated it should be.

Syphdias commented 5 years ago

Aww, man, I fixed that ages ago with expanding on this (my favourite) dir shorten strategy... #979

Currently requires POWERLEVEL9K_SHORTEN_DIR_LENGTH to be set to something...

romkatv commented 5 years ago

It's the best shortening strategy. Thanks for fixing it!

By the way, I've made a small change to it in Powerlevel10k. When it shortens a directory, it adds POWERLEVEL9K_SHORTEN_DELIMITER like all other strategies. However, it uses * as the default value of this parameter.

If you don't set POWERLEVEL9K_SHORTEN_DELIMITER, you get this (using the example directory from https://github.com/bhilburn/powerlevel9k/pull/979):

~/g*/pr*/co*/s*/powerlevel9k

I like that I can see which directories got shortened, and it only costs one character. You can still copy-paste and tab-complete this path, which is the reason I've made * the default delimiter for this strategy.

You can get the original behavior with POWERLEVEL9K_SHORTEN_DELIMITER="":

~/g/pr/co/s/powerlevel9k

Or if you don't care about tab-completion but do like the actual shortening algorithm, you could go with POWERLEVEL9K_SHORTEN_DELIMITER=$'\u2026' (dots) like in other strategies.

What do you think? Would this be useful for Powerlevel9k users?

Syphdias commented 5 years ago

I like that I can see which directories got shortened, and it only costs one character. You can still copy-paste and tab-complete this path, which is the reason I've made * the default delimiter for this strategy.

This is a great idea. I hope I get this PR merged someday 😅

A little bit off topic, but since you modified the behaviour. Have you changed how spaces and such are handled? There is probably a good way to handle it with (q) but I never got it to work properly so I ignored it as a known issue

Syphdias commented 5 years ago

Example:

mkdir /tmp/bla{\ foo,bar}
cd /tmp/bla\ foo

To make this usable in copy paste fashion it would need to be /t/bla\ or /t*/bla\ *

romkatv commented 5 years ago

In general, directory in the prompt is printed in unquoted form. Whether your prompt is %~ or %/, you cannot expect to be able to copy-paste the directory from the prompt and ls it or cd into it. Things like spaces, brackets and dollar signs will have to be escaped. It's a trade-off between copy-pastability and readability. The common practice is clearly favoring readability.

Just like you usually but not always can copy-paste a directory from a regular prompt and cd into it, you usually but not always can copy-paste the directory shortened with truncate_to_unique and tab-complete it. It's possible to make tab-completion always work, just like it's possible to make cd always work, but it'll make the path less readable and will run counter to the user expectations due to the prevailing practice of displaying unquoted directory in the prompt. So I think it's WAI.

By the way, I've made another change to truncate_to_unique in Powerlevel10k. It respects POWERLEVEL9K_SHORTEN_FOLDER_MARKER, which I also changed to default to (.bzr|CVS|.git|.hg|.svn|.citc) -- a much more useful default compared to the almost useless ".shorten_folder_marker". In plain English it means that in Powerlevel10k truncate_to_unique does not shorten the following directories:

  1. The first.
  2. The last few. More specifically, the last max(1, POWERLEVEL9K_SHORTEN_DIR_LENGTH).
  3. All intermediate directories that are roots of vcs repositories. That is, if you are deep in oh-my-zsh tree, you'll still see untruncated /oh-my-zsh/ in the path because there is .git in it. This can be disabled by explicitly setting POWERLEVEL9K_SHORTEN_FOLDER_MARKER="".

This is again a compromise between verbosity and information content. There aren't that many parent directories that are vcs repository roots and they are rather important for orienting oneself, so I find it valuable to always see them.

Syphdias commented 5 years ago

That is a nice idea! I had some thoughts about restructuring the dir segment a little bit...

luisdavim commented 4 years ago

how can I achieve something like:

~/.../parent/current