skrupellos

bash promt

Jul 5th, 2011
54
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # This script creates a fancy promt for bash.
  2. # To try it out, use "source promt.sh"
  3.  
  4.  
  5. function commpilePromt {
  6.     # Calculate the remaining space, considering the length of invisible color
  7.     # codes passed as first argument
  8.     let pRemainingSpace=$COLUMNS-${#pLeftPart}-${#pRightPart}+${1}
  9.    
  10.     if [[ ${1} == 0 || "${pRemainingSpace}" -ge 0 ]]
  11.     then
  12.         # If we have enough space left, fill it and return successfully
  13.         pFill="$(perl -e "print('─'x${pRemainingSpace});")"
  14.        
  15.         # Define the "normal" promt
  16.         PS1="${pNotice}${pLeftPart}${pFill}${pRightPart}\n${pLineC}└─${pPromtC}>>${pNormalC} "
  17.         return 0
  18.     else
  19.         # If we haven't enough space left, return unsuccessfully
  20.         return 1
  21.     fi
  22. }
  23.  
  24.  
  25.  
  26. function createPromt {
  27.     # == Create a notice if last command failed
  28.    
  29.     local returncode=$?
  30.    
  31.     # Workaround: We need a character after the "\n" to be recognized by PS1,
  32.     # therefore we take the color code of the next line as our last bit.
  33.     if [[ "${returncode}" == 0 ]]
  34.     then
  35.         pNotice="${pLineC}"
  36.     else
  37.         pNotice="$(echo -ne "${pFailedNoticeC}The last command FAILED" \
  38.             "with the exit code ${returncode}\n${pLineC}")"
  39.     fi
  40.    
  41.    
  42.     # == Set title if terminal is xterm (from ubuntu .bashrc)
  43.     case "$TERM" in
  44.     xterm*|rxvt*)
  45.         echo -ne "\033]0;${fqdn} - ${USER}: ${pwd}\007"
  46.     ;;
  47.    
  48.     *)
  49.     ;;
  50.     esac
  51.    
  52.    
  53.     # == Try if the promt is long enough. otherwise short it
  54.     # ---- 1. try: full promt
  55.     # replaces $HOME dir with ~ ; dir level 3
  56.     local pwd="$(echo "${PWD/#$HOME/~}" | sed  's/.*\/\(.*\/.*\/.*\)$/\1/')"
  57.     local time="$(date +%T)"
  58.     pHost="$(hostname --fqdn)"
  59.     local fqdn="$pHost"
  60.    
  61.     pLeftPart="${pLineC}┌───────┤${pHostC} ${pHost} ${pLineC}├───────┤${pPwdC} ${pwd} ${pLineC}"
  62.     pRightPart="───────┤${pTimeC} ${time} ${pLineC}├────"
  63.    
  64.     commpilePromt 77 && return 0
  65.    
  66.     # ---- 2. try: no clock
  67.     pRightPart=""
  68.     commpilePromt 55 && return 0
  69.    
  70.     # ---- 3. try: only hostname
  71.     pHost=$(hostname)
  72.     pLeftPart="${pLineC}┌───────┤${pHostC} ${pHost} ${pLineC}├───────┤${pPwdC} ${pwd} ${pLineC}"
  73.     commpilePromt 55 && return 0
  74.    
  75.     # ---- 4. try: only pwd
  76.     pLeftPart="${pLineC}┌───────┤${pPwdC} ${pwd} ${pLineC}"
  77.     commpilePromt 33 && return 0
  78.    
  79.     # ---- 5. try: fall back, no graphical gimmicks
  80.     pLeftPart="${pPwdC}${pwd}"
  81.     pFill=""
  82.     commpilePromt 0
  83. }
  84.  
  85.  
  86.  
  87. # Define static things only once here at beginning, not at each promt creation!
  88.  
  89. # == colors
  90. # ---- define
  91.  
  92. pNormalC=$(echo -e "\[\033[m\]")
  93. pLineC=$(echo -e "\[\033[0;36m\]")
  94. pRootC=$(echo -e "\[\033[1;31m\]")
  95. pUserC=$(echo -e "\[\033[1;32m\]")
  96. pTextC=$(echo -e "\[\033[1;33m\]")
  97. pHostC=$pTextC
  98. pPwdC=$pTextC
  99. pTimeC=$pTextC
  100. pFailedNoticeC=$(echo -e "\[\033[1;31m\]")
  101.  
  102.  
  103. # ---- select depending on user
  104.  
  105. if [[ "$UID" == 0 ]]
  106. then
  107.     pPromtC="${pRootC}"
  108. else
  109.     pPromtC="${pUserC}"
  110. fi
  111.  
  112.  
  113. # == The rest of definitions
  114.  
  115. # check the window size after each command and, if necessary,
  116. # update the values of LINES and COLUMNS.
  117. shopt -s checkwinsize
  118.  
  119. # Prevent that multiple bash instances overwrite the readline history of each
  120. # other.
  121. shopt -s histappend
  122.  
  123. # This function will be called before PS1 is evaluated
  124. PROMPT_COMMAND=createPromt
  125.  
  126. # Define the promt for multi line commands
  127. PS2="  ${pPromtC}>>${pNormalC} "
RAW Paste Data