bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Compact-pwd patch for bash-3.0 announced


From: Stephane Chazelas
Subject: Re: Compact-pwd patch for bash-3.0 announced
Date: Fri, 3 Dec 2004 13:44:22 +0000
User-agent: Mutt/1.5.6i

On Fri, Dec 03, 2004 at 01:19:11PM +0100, Zsban Ambrus wrote:
> I announce my compact-pwd patch for bash-3.0. 
> This patch allows you to display an abbreviated pathname in your prompt,
> thus making your prompt shorter.  For example:
> 
>     /usr/src/linux-2.4.23$ PS1='\S\$ '
>     /u/sr/l$ pwd
>     /usr/src/linux-2.4.23
>     /u/sr/l$
[...]

One can do the same with bash code:
(not much tested)

### code begins
PROMPT_COMMAND=update-SHORTPWD
PS1='$SHORTPWD$ '
update-SHORTPWD() {
  [ "$OLDPWD" = "$PWD" ] && return
  local -a oldpwd newpwd shortpwd matching
  local IFS result current i j component savedopt
  savedopt=$(set +o; shopt -p)
  shopt -s nullglob
  shopt -u failglob
  shopt -s dotglob
  shopt -u nocaseglob
  shopt -u extglob
  set -f
  IFS=/
  oldpwd=($OLDPWD)
  newpwd=($PWD)
  shortpwd=($SHORTPWD)
  
  current= result= i=0
  if [ "${shortpwd[0]}" = "~" ]; then
    local -a home
    home=($HOME)
    shortpwd=("${home[@]}" "${shortpwd[@]:1}")
    case $PWD in
      "$HOME" | "$HOME/"*) i=${#home[@]}; result='~/';;
    esac
  fi
  while [ "${oldpwd[i]}" = "${newpwd[i]}" ]; do
    current=$current${newpwd[i]}/
    result=$result${shortpwd[i]}/
    i=$(($i + 1))
  done
  
  set +f
  while [ "$i" -lt "${#newpwd[@]}" ]; do
    component=${newpwd[i]}
    if [ "$current$component" = "$HOME" ]; then
      result='~/'
    else
      j=1
      while [ "$j" -le "${#component}" ] &&
        matching=("$current${component:0:$j}"*) &&
        [ "${#matching[@]}" -gt 1 ]; do
        j=$(($j + 1))
      done
      result=$result${component:0:$j}/
      current=$current$component/
    fi
    i=$(($i + 1))
  done
  case $result in
    /) SHORTPWD=/;;
    *) SHORTPWD=${result%/};;
  esac
  : ${OLDPWD:="$PWD"}
  eval "$savedopt"
}
### code ends

~$ cd /usr/local/share/zsh-beta/site-functions
/u/lo/sh/z/s$ cd /usr/lib/perl/5.8/MIME/
/u/li/perl/5.8/M$ cd /usr/local/share/zsh-beta/site-functions
/u/lo/sh/z/s$

-- 
Stéphane





reply via email to

[Prev in Thread] Current Thread [Next in Thread]