changeset 915:1f0f8f832bf4

Change rules size only on SIGWINCH. $COLUMNS isn't updated on SIGWINCH yet so "tput" is used. For better performance ruler is calculated by substring. Moving ruler handler to trap cleans console from garbage when "set -x" is set in interactive session. It is useful during debugging of bash completion.
author Oleksandr Gavenko <gavenkoa@gmail.com>
date Sun, 30 Dec 2018 12:26:48 +0200
parents dd9d7a0dde2e
children 33637db38e26
files .bashrc
diffstat 1 files changed, 12 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/.bashrc	Sun Dec 30 12:21:48 2018 +0200
+++ b/.bashrc	Sun Dec 30 12:26:48 2018 +0200
@@ -9,20 +9,24 @@
   . ~/.env
 fi
 
-_my_ruler() {
-  local ruler=''
-  for (( i=${COLUMNS:-78}; i>0; i-=1 )) do
-      ruler+='='
-  done
-  echo $ruler
+shopt -s checkwinsize
+shopt -s promptvars
+
+_my_ruler_handler() {
+  local ruler='================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================'
+  local size=`tput cols`
+  ruler=${ruler:0:${size}}
+  _my_ruler=$ruler
 }
+_my_ruler_handler
+trap '_my_ruler_handler' SIGWINCH
 
-PS1='\n$(_my_ruler)\r$?|\u@\h \w \nbash# '
+PS1='\n${_my_ruler}\r$?|\u@\h \w \nbash# '
 PS2='> '
 
 case "$TERM" in
   xterm*|eterm-color|screen|linux|cygwin)
-    PS1=$'\n\e[34m\e[1m$(_my_ruler)\r$?|\e[31m\u\e[35m@\h\e[0m\e[36m \w \n\[\e[1m\e[31m\]bash#\[\e[0m\] '
+    PS1=$'\n\e[34m\e[1m${_my_ruler}\r$?|\e[31m\u\e[35m@\h\e[0m\e[36m \w \n\[\e[1m\e[31m\]bash#\[\e[0m\] '
     PS2=$'\[\e[32m\]> \[\e[0m\]'
   ;;
 esac