David the H. |
03-27-2011 06:07 AM |
A couple of years ago I posted a script that I created that will truncate the $PWD and format it for use in the prompt.
Since then I've modified it some more and converted it into a function, and I use it like this:
Code:
#Set some color escape sequences.
#================================#
export RESET='\e[0m' #disable any colors
# Regular colors.
export BLACK='\e[0;30m' RED='\e[0;31m' GREEN='\e[0;32m' YELLOW='\e[0;33m'
export BLUE='\e[0;34m' MAGENTA='\e[0;35m' CYAN='\e[0;36m' WHITE='\e[0;37m'
# Bolded colors.
export BBLACK='\e[1;30m' BRED='\e[1;31m' BGREEN='\e[1;32m' BYELLOW='\e[1;33m'
export BBLUE='\e[1;34m' BMAGENTA='\e[1;35m' BCYAN='\e[1;36m' BWHITE='\e[1;37m'
#=========================#
# Command prompt settings #
#=========================#
# Function for setting a nice variable-length shell prompt.
#==========================================================#
function variprompt {
# This function takes the $PWD or other input string and truncates it to a
# set percentage of the terminal width. It's designed for use in the bash
# $PS1 prompt to keep it from taking up too much space. There may be minor
# variations in the actual length due to rounding in the arithmetic.
# Set the desired width of the string below as the percentage of the total
# terminal width. Remember that any other parts you add to the prompt (such as
# username) will take up additional space. Also be aware that this script is
# designed for reasonable numbers, and may act in unpredictable ways if the
# target and/or terminal width are very small. Target values under 25 are not
# recommended. Will use $1 if it exists, or defaults to the set value.
TARGETPCT=${1:-40}
# Get the current directory or input string. This will $2 if it exists, or else
# It will default to $PWD. In addition, if the string includes the $HOME
# directory, it will truncate it to "~". Simply comment out the second line if
# this behavior is unwanted and you always want the full path.
CURRENTDIR="${2:-$PWD}"
CURRENTDIR="${CURRENTDIR/#$HOME/~}"
# Be sure that the shopt option "checkwinsize" is enabled, so that the curent
# shell width (COLUMNS) is available to this function. If the $COLUMNS variable
# is unavailable, the following line will set it to 100 instead. Alternatively,
# you can manually set this variable to a fixed value of 100, and the output
# will always be TARGETPCT columns wide.
COLUMNS=${COLUMNS:-100}
# Determine the number of characters in the input string and calculate
# the total target length and the sizes of the starting and ending segments.
CURRENTLENGTH="${#CURRENTDIR}"
TARGETLENGTH="$(( COLUMNS * TARGETPCT / 100 ))"
FIRSTPART="$(( TARGETLENGTH / 3 - 3 ))"
LASTPART="$(( CURRENTLENGTH - ( TARGETLENGTH * 2/3 ) ))"
# Finally we compare the current length of the prompt to the target length
# and print the appropriate output string.
if (( CURRENTLENGTH <= TARGETLENGTH )); then
# No modifications are necessary. Print the unmodified string.
echo "$CURRENTDIR"
else
# Very small target or column values can make "FIRSTPART" zero or less.
# So test it to avoid throwing out an error, and set it to display only the
# first two characters of the string in such situations. Otherwise, print
# the desired truncated string.
if (( FIRSTPART > 1 )); then
echo "${CURRENTDIR:0:$FIRSTPART}...${CURRENTDIR:$LASTPART}"
else
echo "${CURRENTDIR:0:2}...${CURRENTDIR:$LASTPART}"
fi
fi
}
# End function.
#==============#
# Set the prompt.
#================#
# Set checkwinsize option, it's needed to ensure COLUMNS variable is read properly.
shopt -s checkwinsize
# Pattern to set the window title portion of the prompt, if in an xterm.
# The formatting pattern goes between '\e]0;' and '\a' (\a = \007).
if [[ "$TERM" =~ xterm ]]; then
XTITLE='\e]0;\u:$(variprompt 55 2>/dev/null)\a'
else
unset XTITLE
fi
# Define PS1 for root and regular users. Must unquote all the variables.
# Escape sequences must be enclosed in \[ and \] brackets.
if (( UID == 0 )); then
export PS1='\['${XTITLE}$BRED'\]\u:[\['$MAGENTA'\]$(variprompt 2>/dev/null)\['$BRED'\]]##\['$RESET'\] '
else
export PS1='\['${XTITLE}$BGREEN'\]\u:[\['$BBLUE'\]$(variprompt 2>/dev/null)\['$BGREEN'\]]$\['$RESET'\] '
fi
Yeah, it seems quite long, but it's mostly comments. :)
The result looks something like this:
Code:
david:[~]$ cd /mnt/musicdir/western_artists/Beatles--Albums/08_Sgt_Peppers_Lonely_Hearts_Club_Band/
david:[/mnt/musicdi...ppers_Lonely_Hearts_Club_Band]$
...but in color. Note also how the window title string can be set to a different length from the prompt itself.
|