prompt.zsh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # prompt config
  2. export PR_OS_NAME=$(uname -s)
  3. export PR_OS_ARCH=$(uname -m)
  4. if [ -f /etc/debian_version ]; then
  5. echo $PR_OS_NAME | grep -i linux >/dev/null 2>&1
  6. if [ $? = 0 ]; then
  7. PR_OS_NAME="Debian GNU/${PR_OS_NAME}"
  8. else
  9. PR_OS_NAME="Debian ${PR_OS_NAME}"
  10. fi
  11. debian_version=$(</etc/debian_version)
  12. [ $debian_version = 'testing/unstable' ] && PR_OS_NAME="${PR_OS_NAME} sid" || PR_OS_NAME="${PR_OS_NAME} ${debian_version}"
  13. if [ -f /etc/debian_chroot ]; then
  14. chroot_name=(${(s:-:)$(</etc/debian_chroot)})
  15. chroot_name=$chroot_name[0]
  16. PR_OS_NAME="${PR_OS_NAME} [chroot: ${chroot_name}]"
  17. fi
  18. fi
  19. [ "$TERM" = "screen" -a -n "$WINDOW" ] && PR_OS_PTY="screen/$WINDOW" || PR_OS_PTY=$(print -P %l)
  20. export PR_OS_PTY
  21. function preexec {
  22. binonly=(emacs mplayer lftp most more less rsu)
  23. args=(${=1})
  24. argsr=(${=2})
  25. if [[ $args[0] != $argsr[0] ]]; then
  26. args[0]=$argsr[0]
  27. fi
  28. [[ -n ${(M)binonly:#$args[0]} ]] && args=$args[0]
  29. if [ $UID = 0 ]; then
  30. [ "$KONSOLE_DCOP_SESSION" ] && dcop $KONSOLE_DCOP_SESSION renameSession "*$args"
  31. [ "$TERM" = "screen" ] && echo -ne "\033k*$args\033\\"
  32. else
  33. [ "$KONSOLE_DCOP_SESSION" ] && dcop $KONSOLE_DCOP_SESSION renameSession "$args"
  34. [ "$TERM" = "screen" ] && echo -ne "\033k$args\033\\"
  35. fi
  36. }
  37. function precmd {
  38. # function called before displaying prompt
  39. local TERMWIDTH
  40. (( TERMWIDTH = ${COLUMNS} - 1 ))
  41. # Truncate the path if it's too long.
  42. local promptsize=${#${(%):---(%m)-(%n)-(%D{%H:%M})-($PR_OS_PTY)-($PR_OS_NAME)-($PR_OS_ARCH)---}}
  43. PR_FILLBAR="\${(l.(($TERMWIDTH - $promptsize))..${PR_HBAR}.)}"
  44. if [ $UID = 0 ]; then
  45. [ "$KONSOLE_DCOP_SESSION" ] && dcop $KONSOLE_DCOP_SESSION renameSession "*$(print -P %~)"
  46. [ "$TERM" = "screen" ] && echo -ne "\033k*$(print -P %~)\033\\"
  47. else
  48. # if running konsole, setting current directory name in tab
  49. [ "$KONSOLE_DCOP_SESSION" ] && dcop $KONSOLE_DCOP_SESSION renameSession "$(print -P %~)"
  50. # if running screen, setting current directory name in caption/statusbar
  51. [ "$TERM" = "screen" ] && echo -ne "\033k$(print -P %~)\033\\"
  52. fi
  53. }
  54. myprompt() {
  55. setopt prompt_subst
  56. #enable colors
  57. autoload colors zsh/terminfo
  58. if [[ "$terminfo[colors]" -ge 8 ]]; then
  59. colors
  60. fi
  61. for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
  62. eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
  63. eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
  64. (( count = $count + 1 ))
  65. done
  66. PR_NO_COLOUR="%{$terminfo[sgr0]%}"
  67. typeset -A altchar
  68. set -A altchar ${(s..)terminfo[acsc]}
  69. PR_SET_CHARSET="%{$terminfo[enacs]%}"
  70. PR_SHIFT_IN="%{$terminfo[smacs]%}"
  71. PR_SHIFT_OUT="%{$terminfo[rmacs]%}"
  72. PR_HBAR=${altchar[q]:--}
  73. PR_ULCORNER=${altchar[l]:--}
  74. PR_LLCORNER=${altchar[m]:--}
  75. PR_LRCORNER=${altchar[j]:--}
  76. PR_URCORNER=${altchar[k]:--}
  77. if [ "$TERM" = "dumb" ]; then
  78. PROMPT="-(%n@%m)-(%(!.%d.%~))->"
  79. else
  80. PROMPT='\
  81. $PR_SET_CHARSET\
  82. $PR_CYAN$PR_SHIFT_IN$PR_ULCORNER$PR_CYAN$PR_HBAR$PR_SHIFT_OUT(\
  83. $PR_WHITE%m$PR_CYAN\
  84. $PR_SHIFT_IN$PR_CYAN)$PR_HBAR$PR_SHIFT_OUT(\
  85. %(!.$PR_RED%n.$PR_LIGHT_GREEN%n)\
  86. $PR_SHIFT_IN$PR_CYAN)$PR_HBAR$PR_SHIFT_OUT(\
  87. $PR_WHITE%D{%H:%M}$PR_CYAN\
  88. $PR_SHIFT_IN$PR_CYAN)$PR_HBAR$PR_SHIFT_OUT(\
  89. $PR_WHITE$PR_OS_PTY$PR_CYAN\
  90. $PR_SHIFT_IN$PR_CYAN)$PR_HBAR$PR_SHIFT_OUT(\
  91. $PR_WHITE$PR_OS_NAME$PR_CYAN\
  92. $PR_SHIFT_IN$PR_CYAN)$PR_HBAR$PR_SHIFT_OUT(\
  93. $PR_WHITE$PR_OS_ARCH$PR_CYAN\
  94. $PR_SHIFT_IN$PR_CYAN)$PR_HBAR$PR_SHIFT_OUT\
  95. $PR_SHIFT_IN$PR_HBAR$PR_CYAN$PR_HBAR${(e)PR_FILLBAR}$PR_CYAN$PR_HBAR$PR_SHIFT_OUT\
  96. $PR_CYAN$PR_SHIFT_IN$PR_LLCORNER$PR_HBAR$PR_SHIFT_OUT\
  97. %1(j.{${PR_YELLOW}jobs$PR_CYAN:$PR_WHITE%j$PR_CYAN}$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT.)\
  98. %0(?..{${PR_RED}ret$PR_CYAN:$PR_WHITE%?$PR_CYAN}$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT)\
  99. ($PR_WHITE%(!.%d.%~)$PR_CYAN)$PR_SHIFT_IN$PR_HBAR$PR_LRCORNER$PR_SHIFT_OUT\
  100. >$PR_NO_COLOUR'
  101. fi
  102. }
  103. myprompt