Skip to content

Instantly share code, notes, and snippets.

@duckie
Created April 9, 2013 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duckie/5342014 to your computer and use it in GitHub Desktop.
Save duckie/5342014 to your computer and use it in GitHub Desktop.
A simple function to shrunken a Unix path to a descriptive fixed size string. I use this for my prompt.
function shrunken_dir(){
SEP="/"
ESC_SEP="\\$SEP"
HEADER_SIZE=5
SIZE_MAX=$1
current_disp=`pwd`
seen_path_max_length=$((SIZE_MAX-HEADER_SIZE))
current_seen_path=$(echo "$current_disp"|tr -s "$SEP")
nb_dirs=0
seen_path_length=$(expr length "$current_seen_path")
while [ $seen_path_length -gt $seen_path_max_length ]; do
current_seen_path=$(echo $current_seen_path|sed "s/^$ESC_SEP//"|grep -oE "$SEP.*")
seen_path_length=$(expr length "$current_seen_path")
nb_dirs=$((nb_dirs+1))
done
header="{$nb_dirs}"
padding=""
for i in $(seq $(expr length $header) $((SIZE_MAX-seen_path_length))); do
padding="$padding "
done
printf "${header}${current_seen_path}${padding}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment