I implemented a prompt path shortener for bash to be included in the PS1 environment variable, which shortens the working directory into something more compact but still descriptive. I'm curious what other ideas may exist.
Here's the challenge:
Create a bash function _dir_chomp which can be included into PS1 like this (line breaks inserted for readability):
PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] $(
_dir_chomp "$(pwd)" 20
)\[\033[01;37m\]$(parse_git_branch)\[\033[01;34m\] \$\[\033[00m\] '
with "20" being the parameter for the maximum length as soft limit. These are the examples:
/usr/portage/media-plugins/banshee-community-extensions/filesbecomes/u/p/m/b/files/home/user1/media/video/music/live-setsbecomes~/m/v/m/live-sets(note the ~ character as replacement for $HOME)/home/user2/mediadoes NOT change (20 char limit not exceeded)/home/user1/this-is-a-very-long-path-name-with-more-than-20-charsbecomes~/this-is-a-very-long-path-name-with-more-than-20-chars(last component stays unshortened: soft limit)/home/user1/srcbecomes~/src($HOME always shortened)/home/user1/.kde4/share/config/kresourcesbecomes~/.k/s/c/kresources(note the prefixing dot is preserved)
Current user is user1.
It's allowed to call external interpreters like awk, perl, ruby, python but not compiled C programs or similar. In other words: external source files are not allowed, code must be inline. Shortest version wins. The length of the bash function body (and called sub functions) counts, means:
_sub_helper() {
# this counts
}
_dir_chomp() {
# these characters count (between { and })
_sub_helper "foobar" # _sub_helper body counts, too
}