bug-bash
[Top][All Lists]
Advanced

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

Re: Lengthy working dir (\w) breaks wrapping in a multi line coloured PS


From: Linda Walsh
Subject: Re: Lengthy working dir (\w) breaks wrapping in a multi line coloured PS1 prompt
Date: Tue, 24 Dec 2013 17:08:48 -0800
User-agent: Thunderbird



Use something to trim your prompt usefully (or rigidly like bash prompt builtin '\w'):

------- in your .bashrc define these:

# return a shorted path when displayed path would take up > 50% width of screen

alias int=declare\ -i _e=echo _pf=printf exp=export ret=return
exp __dpf__='local -a PF=(
                "/$1/$2/$3/../\${$[$#-1]}/\${$#}"
                "/$1/$2/../\${$[$#-1]}/\${$#}"
                "/$1/../\${$[$#-1]}/\${$#}"
                "/$1/../\${$#}"
                ".../\${$#}"
                "..." )'
function spwd () {  \
  (($#)) || { _e "spwd called with null arg"; return 1; }; \
  int w=COLUMNS/2                         ;\
  ( _pf -v _p "%s" "$1" ; export IFS=/    ;\
    set $_p; shift; unset IFS             ;\
    t="${_p#${HOME%${USER}}}"             ;\
    int tl=${#t}                          ;\
    if (($#<=6 && tl<w));then ((tl<=2)) && \
      { _e -En "${_p}";return 0; }        ;\
    else                                   \
      eval "$__dpf__"                     ;\
      int i pfl=${#PF[*]}                 ;\
      for ((i=0; i<pfl; ++i)); do eval     \
        "_pf -v _pa %s \"${PF[i]}\""      ;\
        _p="$(eval "_pf %s \"$_pa\"")"    ;\
        ((${#_p}<w)) && break;  done      ;\
    fi                                    ;\
    _e -En "${_p#${HOME%${USER}}}" )
}
------------------------

Then set your prompt (this one turns red for root, and sets the window title)


  #specific to xterm & linux console compat emulations
  read _CRST < <(tput sgr0)   #Reset
  read _CRed < <(tput setaf 1)  #Red
  read _CBLD < <(tput bold)   #Bold
  declare _prompt_open="" _prompt_close="" _prompt=">"
  declare _disp_port=${DISPLAY/[^:]*:/}
  [[ $UID -eq 0 ]] && {
    _prompt_open="$_CBLD$_CRed"
    _prompt="#"
    _prompt_close="$_CRST"
  }
  PS1='\['"$_prompt_open"'\]$(spwd "$PWD" )'"$_prompt"'\['"$_prompt_close"'\] ';

  if [[ -n ${REMOTEHOST:-""} ]]; then
    function titlebar { \
      printf "\033]1;address@hidden:%q\007" "$(spwd "$PWD")" ;\
    }
    export -f titlebar

PS1='\[$(titlebar)'"$_prompt_open"'\]${HOSTNAME}:$(spwd "$PWD")'"$_prompt"'\['"$_prompt_close"'\] '
  fi



--------------------------

cd 
"/tmp/a_long_directory_name/getting_even_bigger_so_that_I_can_test/this_prompt_is_working_fine_or_not"


Observe that the prompt is now displayed as:

    Ishtar:.../this_prompt_is_working_fine_or_not>

Notice it doesn't take more than half the width of your screen..

It only displays host if you are logged in remotely (relies on REMOTEHOST being 
set
via pam_env.dll)






reply via email to

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