Permalink
Browse files

Dynamically shortening path in prompt.

  • Loading branch information...
1 parent afc0826 commit 46f01078b4e621b1e0705e812340e086ba2c9ba3 @jhedstrom committed Mar 31, 2012
Showing with 39 additions and 1 deletion.
  1. +4 −1 .bashrc
  2. +35 −0 bin/short-path
View
@@ -54,13 +54,16 @@ if [ -f ~/.drush_bashrc ] ; then
. ~/.drush_bashrc
fi
+# Export columns var so python has access to this.
+export COLUMNS
+
# Git goodness:
# http://www.metaltoad.com/blog/git-drupal-primer
# GIT_PS1_SHOWDIRTYSTATE=true
# export PS1='[\u@mb \w$(__git_ps1)]\$ '
GIT_PS1_SHOWDIRTYSTATE=true
if [ "$color_prompt" = yes ]; then
- PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)$(__drush_ps1)\[\033[01;36m\]☠\[\033[00m\] '
+ PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]`short-path`\[\033[00m\]$(__git_ps1)$(__drush_ps1)\[\033[01;36m\]☠\[\033[00m\] '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)☠ '
fi
View
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+#
+# Shorten PWD for use in a shell prompt
+#
+import os
+
+# Shorten path down to length N
+PWD_MAX_LEN = int(os.getenv('COLUMNS')) / 3
+
+# Always include the last N dirs
+PWD_LAST=2
+
+pwd = os.getenv('PWD')
+home = os.getenv('HOME')
+
+if pwd.startswith(home):
+ pwd = pwd.replace(home, '~', 1)
+
+dirs = pwd.split('/')
+
+def path(dirs):
+ return '/'.join(dirs)
+
+def shorten(d):
+ if not d:
+ return d
+ return d[0]
+
+for (i, p) in enumerate(dirs[:-PWD_LAST]):
+ if len(path(dirs)) <= PWD_MAX_LEN:
+ break
+
+ dirs[i] = shorten(p)
+
+print path(dirs)

0 comments on commit 46f0107

Please sign in to comment.