## Bash recognizess a variable called PROMPT_COMMAND and, if it is set, ## executes whatever it evaluates to. This script can be called from .bashrc or ## SOURCED (not executed) on demand. I define aliases "actcool" and actnormal" ## in my .alias file to facilitate turning this on and off. ## The first part defines a function, 'prompt_command', which ## evaluates the current working and directory, (and hostname and username if desired), ## then writes it to the upper right corner of the display in contrasting color. ## The variable PROMPT_COMMAND is then set equal to 'prompt_command'. ## The second part provides a short prompt string indicating the current input device. ############################################################ ## Step 1: Define PROMPT_COMMAND function and some variables. ############################################################ function prompt_command () { tput sc # Save the current cursor position ## set background, forground color, bold if [ $UID = 0 ] then tput setab 1 ; tput setaf 3 ; tput bold # yellow text on red background for root else tput setab 4 ; tput setaf 6 ; tput bold # cyan text on blue background for users fi ## Define DIR as current directory, with current users home reduced to ~ DIR=$(pwd | sed s?$(echo ~)?~?) ## OLDDIR is directory string most recently WRITTEN TO THE SCREEN. ## If $DIR is shorter than $OLDDIR it will be necessary to clean up ## artifacts which might remain to the left of the new one. This is ## done by over-writing with blank spaces. $OLDDIR must not be empty string. if [ -z "$OLDDIR" ] then OLDDIR=$DIR fi DIFF=$(($(echo "$OLDDIR" | wc -m)-$(echo "$DIR" | wc -m))) if [ $DIFF -lt 0 ] then DIFF=0 fi ## Define SPLASH_STRING - pick one # SPLASH_STRING="$DIR" # SPLASH_STRING="$HOSTNAME:$DIR" SPLASH_STRING="$(id -un)@$HOSTNAME:$DIR" ## Define PAD as the total width of the terminal minus the length ## of the splash string. Then position cursor to over write any ## artifacts then print $SPLASH_STRING at the top left (Y=0, X=PAD) let PAD=$(( $(tput cols) -$(echo "$SPLASH_STRING" | wc -m) +1 -$DIFF )) tput cup 0 $PAD # position cursor so the SPLASH_STRING will be left (Y=0, X=$PAD) for (($DIFF; DIFF>0; --DIFF)); do echo -n " "; done echo -n "$SPLASH_STRING" OLDDIR=$DIR # Save directory string for DIFF in next execution. tput rc # Return the cursor to the saved position } ######################################## ## Step 2: Set variable equal to function ######################################## PROMPT_COMMAND=prompt_command ############################################################ ## Step 3: Set prompt string to display current input device ############################################################ ## Evaluate current logged device from output of 'tty' tty=$(echo $(tty) |sed s%/dev/%%) PS1="$tty \\$ " ## Below is a nice idea, but it currently causes problems with display of command history. ## Set command prompt to show current console in UID apropriate colors. ## Colored prompts aid finding command lines among output lines in scrollback #if [ $UID = 0 ] # then PS1="\e[31m\e[103m$tty \\$\e[0m " # else PS1="\e[34m\e[106m$tty \\$\e[0m " #fi