Advertisement
Guest User

ps1 v2

a guest
Sep 28th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.18 KB | None | 0 0
  1. # PS1 {{{
  2.  
  3. # attributes and colors {{{
  4.  
  5. # bg/fg
  6. FG=3
  7. BG=4
  8.  
  9. # attributes
  10. BOLD=1
  11. FAINT=2
  12. ITALIC=3
  13. UNDERLINE=4
  14. BLINK=5
  15. FAST_BLINK=6
  16. REVERSE=7
  17. CONCEAL=8
  18. CROSSED_OUT=9
  19.  
  20. # colors
  21. BLACK=0
  22. RED=1
  23. GREEN=2
  24. YELLOW=3
  25. BLUE=4
  26. MAGENTA=5
  27. CYAN=6
  28. WHITE=7
  29.  
  30. DEFAULT=9
  31.  
  32. # }}}
  33.  
  34. function toggle_ps1_dir_type { # {{{
  35.     if [[ $_ps1_dir_type == 1 ]]; then
  36.         [[ $2 == reverse ]] && _ps1_dir_type=3 || _ps1_dir_type=2
  37.     elif [[ $_ps1_dir_type == 2 ]]; then
  38.         [[ $2 == reverse ]] && _ps1_dir_type=1 || _ps1_dir_type=3
  39.     else
  40.         [[ $2 == reverse ]] && _ps1_dir_type=2 || _ps1_dir_type='1'
  41.     fi
  42. } # }}}
  43. # alt+d - toggle _ps1_dir_type {{{
  44. bind -x '"\ed":"toggle_ps1_dir_type from_bind"'
  45. bind -x '"\eD":"toggle_ps1_dir_type from_bind reverse"'
  46. # }}}
  47.  
  48. function _ps1_setup { # {{{
  49.     esc=$'\e'
  50.  
  51.     [[ -z $_ps1_short_symbol ]] && _ps1_short_symbol='%'
  52.     [[ -z $_ps1_short_at ]]     && _ps1_short_at="3${BLUE};${REVERSE}"
  53.  
  54.     [[ -z $_ps1_dir_type ]] && _ps1_dir_type='1'
  55.     [[ -z $_ps1_dir_fg ]]   && _ps1_dir_fg="$BLACK"
  56.     [[ -z $_ps1_dir_bg ]]   && _ps1_dir_bg="$BLUE"
  57.     [[ -z $_ps1_dir_at ]] && _ps1_dir_at="$ITALIC"
  58.  
  59.     [[ -z $_ps1_mid_bg ]] && _ps1_mid_bg="$BLACK"
  60.  
  61.     [[ -z $_ps1_user_fg ]] && _ps1_user_fg="$BLUE"
  62.     [[ -z $_ps1_user_bg ]] && _ps1_user_bg="$BLACK"
  63.     # [[ -z $_ps1_user_at ]] && _ps1_user_at=''
  64.  
  65.     [[ -z $_ps1_at_fg ]] && _ps1_at_fg="$WHITE"
  66.     [[ -z $_ps1_at_bg ]] && _ps1_at_bg="$BLACK"
  67.     # [[ -z $_ps1_at_at ]] && _ps1_at_at=''
  68.  
  69.     [[ -z $_ps1_host_fg ]] && _ps1_host_fg="$YELLOW"
  70.     [[ -z $_ps1_host_bg ]] && _ps1_host_bg="$BLACK"
  71.     # [[ -z $_ps1_host_at ]] && _ps1_host_at=''
  72.  
  73.     [[ -z $_ps1_git_symbol_pre ]] && _ps1_git_symbol_pre=' '
  74.     # [[ -z $_ps1_git_symbol_post ]] && _ps1_git_symbol_post=''
  75.  
  76.     [[ -z $_ps1_ret_fg ]] && _ps1_ret_fg="$WHITE"
  77.     [[ -z $_ps1_ret_bg ]] && _ps1_ret_bg="$RED"
  78.     [[ -z $_ps1_ret_at ]] && _ps1_ret_at="$BOLD"
  79.  
  80.     # _ps1_dir_full_last="${esc}[0;34;49m"
  81.  
  82.     PS1='$(_ps1 "\w" "\W" "$?" "\u" "\h")'
  83. } # }}}
  84. function _ps1 { # {{{
  85.     # $1 = "\w" (pwd)
  86.     # $2 = "\W" (short pwd)
  87.     # $3 = "$?" (ret)
  88.     # $4 = "\u" (user)
  89.     # $4 = "\h" (host)
  90.     IFS=';' read -sdR -p $'\e[6n' row col
  91.     # short
  92.     (( $col > 1 )) && sleep .01 && printf '\001\e[0;%sm\002%s\001\e[0m\002\n\001\r\002' "$_ps1_short_at" "$_ps1_short_symbol"
  93.  
  94.     # dir (type 2)
  95.     [[ $_ps1_dir_type == 2 ]] && printf '\n\001\r%s\002 %s (%s .%s) \001%s\002\n\001\r\002' "${esc}[0${_ps1_dir_at:+;$_ps1_dir_at};3${_ps1_dir_fg};4${_ps1_dir_bg}m" "$1" "$(echo * | wc -w)" "$(echo .* | wc -w)" "${esc}[0;3${_ps1_dir_bg};4${DEFAULT}m"
  96.  
  97.     # ret
  98.     (( $3 != 0 )) && printf '\001%s\002%s\001%s\002' "${esc}[0${_ps1_ret_at:+;$_ret_dir_at};3${_ps1_ret_fg};4${_ps1_ret_bg}m" "$3" "${esc}[0;3${_ps1_ret_bg};4${_ps1_user_bg}m"
  99.     printf '\001%s\002 \001%s\002%s\001%s\002 \001%s\002at\001%s\002 \001%s\002%s\001%s\002'\
  100.         "${esc}[0;4${_ps1_mid_bg}m"\
  101.         "${esc}[0${_ps1_user_at:+;$_ps1_user_at};3${_ps1_user_fg};4${_ps1_user_bg}m"\
  102.         "$4"\
  103.         "${esc}[0;4${_ps1_mid_bg}m"\
  104.         "${esc}[0${_ps1_at_at:+;$_ps1_at_at};3${_ps1_at_fg};4${_ps1_at_bg}m"\
  105.         "${esc}[0;4${_ps1_mid_bg}m"\
  106.         "${esc}[0${_ps1_host_at:+;$_ps1_host_at};3${_ps1_host_fg};4${_ps1_host_bg}m"\
  107.         "$5"\
  108.         "${esc}[0;4${_ps1_mid_bg}m"
  109.  
  110.     # git
  111.     _ps1_git_branch="$(git symbolic-ref --short -q HEAD 2> /dev/null)"
  112.     printf "${_ps1_git_branch:+\001\e[0;34;40m\002  \001%s\002%s\001%s\002%s\001%s\002%s}"\
  113.         $'\e[0;34;40m'\
  114.         "${_ps1_git_symbol_pre}"\
  115.         $'\e[0;32;40m'\
  116.         "${_ps1_git_branch}"\
  117.         $'\e[0;34;40m'\
  118.         "${_ps1_git_symbol_post}"
  119.  
  120.     # dir (type 1)
  121.     [[ $_ps1_dir_type == 1 ]] &&\
  122.         printf ' \001\e[0;30;44m\002\001\e[0;30;44m\002 %s \001\e[0;34;49m\002\001\e[0m\002 '\
  123.         "$2"\
  124.     ||\
  125.         printf ' \001\e[0;30;49m\002\001\e[0m\002 '
  126. } # }}}
  127.  
  128.  
  129. # }}}
  130.  
  131.  
  132. # only use italics outside screen/tmux
  133. [[ $TERM =~ ^screen ]] && _ps1_dir_at="$UNDERLINE" || _ps1_dir_at="$ITALIC"
  134.  
  135. # make red if super user
  136. [[ $(id -u) == 0 ]] && _ps1_user_fg=$RED
  137.  
  138. # don't use fancy prompt in tty
  139. [[ $(tty) == /dev/tty[0-9] ]] || _ps1_setup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement