How to achieve short working directory
Closed this issue · 9 comments
Like from this: ~/Documents/GitHub
To this: ~/D/GitHub
And apply it to my gitster theme
Hi @KiteAB, this is the code that shortens the pwd:
local current_dir=${PWD/#${HOME}/~}"
if [[ ${current_dir} != '~' ]]; then
current_dir="${${(@j:/:M)${(@s:/:)current_dir:h}#?}%/}/${current_dir:t}"
fi
The longest line is doing most of the work:
/${current_dir:t}
: keep the tail directory (last directory in the path)${(@s:/:)current_dir:h}
: split the remaining directories into an array${(@j:/:M)
...#?}
: join the first letter of the array elements${
...%/}
: remove extra slash in case we're in the root directory
You can apply the same code to your custom prompt. If you want to use that with gitster, it will look like:
local git_root current_dir
if git_root=$(command git rev-parse --show-toplevel 2>/dev/null); then
current_dir="${PWD#${git_root:h}/}"
else
current_dir=${PWD/#${HOME}/~}"
fi
if [[ ${current_dir} != '~' ]]; then
current_dir="${${(@j:/:M)${(@s:/:)current_dir:h}#?}%/}/${current_dir:t}"
fi
print -n "%F{white}${current_dir}"
@KiteAB, it's been only 14 hours since your reply, and now you sound demanding. It's not fair to address me or any other person who puts their free time in trying to build a great piece of software, in this manner.
@KiteAB, it's been only 14 hours since your reply, and now you sound demanding. It's not fair to address me or any other person who puts their free time in trying to build a great piece of software, in this manner.
Oh, sorry. Sorry to bother you, my problem has been basically solved, thank you
@KiteAB, you might want to use this with gitster. It will keep the git root directory unshortened too, which I think goes well with the spirit of this prompt:
local git_root current_dir
if git_root=$(command git rev-parse --show-toplevel 2>/dev/null); then
current_dir="${PWD#${git_root:h}/}"
else
current_dir=${(%):-%~}
fi
local -r dirs=("${(@s:/:)current_dir}")
if (( ${#dirs} > 2 )); then
current_dir="${dirs[1]}/${(@j:/:M)dirs[2,-2]#?}/${dirs[-1]}"
fi
print -n "%F{white}${current_dir}"
I am surprised that you can give the final answer, thank you very much!