LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-27-2011, 06:07 AM   #46
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013

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.

Last edited by David the H.; 03-27-2011 at 06:24 AM. Reason: comment fix
 
Old 03-27-2011, 01:25 PM   #47
Scruff
Member
 
Registered: Oct 2002
Location: Stoughton, MA
Distribution: Gentoo x86_64 & PPC
Posts: 949

Rep: Reputation: 30
Whoa, haven't seen this thread in awhile. Mine now looks like:

[sean.bitwise: /home/sean]$

It also turns to red when a command fails, back to green under normal conditions.

PHP Code:
export PROMPT_COMMAND='PS1="\`if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\`[\u.\h: \`if [[ `pwd|wc -c|tr -d " "` > 18 ]]; then echo "\\W"; else echo "\\w"; fi\`]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"' 
 
Old 03-30-2011, 06:06 PM   #48
bret381
Member
 
Registered: Nov 2009
Location: Alabama
Distribution: Arch x86_64
Posts: 650

Rep: Reputation: 79
mine looks like this:

┌─[18:04:17]──[bret]──[Ziggy]:~$
└──(28 files, 3.5Gb)>>

Code:
PS1="\[\e[01;37m\]┌─[\t]──[\[\e[01;37m\u\e[01;37m\]]──[\[\e[00;37m\]${HOSTNAME%%.*}\[\e[01;37m\]]:\w$\[\e[01;37m\]\n\[\e[01;37m\]└──\[\e[01;37m\](\[\e[32;1m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[01;37m\])>>\[\e[0m\]"

Last edited by bret381; 03-30-2011 at 06:10 PM.
 
Old 03-30-2011, 08:15 PM   #49
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,720
Blog Entries: 29

Rep: Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930
Quote:
Originally Posted by bret381 View Post
mine looks like this:

┌─[18:04:17]──[bret]──[Ziggy]:~$
└──(28 files, 3.5Gb)>>
What is the way to type that special character there? The starting character I mean.

Last edited by TheIndependentAquarius; 03-31-2011 at 01:59 AM.
 
Old 03-31-2011, 08:30 AM   #50
bret381
Member
 
Registered: Nov 2009
Location: Alabama
Distribution: Arch x86_64
Posts: 650

Rep: Reputation: 79
Quote:
What is the way to type that special character there? The starting character I mean.

I don't have a clue. haha, I used the archbang default prompt (which has that character) and added some stuff.
 
Old 04-01-2011, 12:15 AM   #51
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,720
Blog Entries: 29

Rep: Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930Reputation: 930
But there is a way to enter the special characters in the prompt, I know that, but how to type them?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Different name at shell prompt XJNick Linux - General 3 08-25-2005 10:20 AM
change the shell prompt ust Linux - Distributions 5 04-28-2005 03:26 PM
Shell Prompt xneowolf Linux - General 5 01-26-2003 02:06 AM
Shell prompt setup NSKL Linux - Newbie 1 06-15-2002 11:55 AM
C shell prompt robson Linux - General 2 11-12-2001 08:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration