[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

colored bash prompt



On Tue, Jul 13 at 09:16AM -0500, Reid Priedhorsky wrote:
> On Tue, 13 Jul 2004 09:30:18 +0200, Silvan wrote:
> >> Also look into the tput program. You tell it what you want (bold,
> >> green, etc.) and it outputs appropriate magic for your current
> >> terminal.

great idea -- i'll have to snoop around in there for a while.
good modular solution! i've been using perl's Term::ANSIColor
module, which is really hard-wired cheat-code, but it works for
the most part.

> > Sounds interesting, but any syntax examples?  I couldn't make heads or
> > tails of it.
> 
> Here's a bash prompt I used for a while. It makes the hostname part bold.
> 
>   # my prompt
>   BOLD=`tput bold`
>   NORM=`tput sgr0`
>   export PS1="\# [\u@\[$BOLD\]\h\[$NORM\] \w]\$ "
> 
> (Yes, srg0 means normal.)

here's my setup -- comments welcome.



# System-wide /etc/bash.bashrc file for interactive bash(1) shells.

umask 022

# If running interactively, then:
if [ "$PS1" ]; then

	function makePrompt
	{
		local ESC='\e';
		# control-O restores charset after 'cat <binaryfile>'
		local CHARSET='\017'

		# define USERCOLOR, ROOTCOLOR, SYSCOLOR, HOSTCOLOR
		. /etc/bash.bashrc.local

		local CHR='\$'

		if [ "`id -u`" -eq 0 ]; then
			USERCOLOR=$ROOTCOLOR
			#PATH="/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games"
			PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"
		elif [ "`id -u`" -lt 1000 ]; then
			USERCOLOR=$SYSCOLOR
		fi

		if [ "$BASH" ]; then
			#PS1='\u@\h:\w\$ '

			# get ANSI esc seq for colorization, and bracket
			# with \[...\]
			function bracket
			{
				local seq=`perl -MTerm::ANSIColor -e "print color(qw/$*/)"`
				echo "\[$seq\]"
			}

			USERCOLOR=`bracket $USERCOLOR`
			HOSTCOLOR=`bracket $HOSTCOLOR`
			local DECOLOR=`bracket RESET`

			unset bracket

			local LOGIN="${USERCOLOR}\u${DECOLOR}@${HOSTCOLOR}\H"
			local WHERE="\w${DECOLOR}"
			local WHEN="\[$ESC[\$LINES;\$((\$COLUMNS - 19))H\]${HOSTCOLOR}\d \t${DECOLOR}"
			PS1="$CHARSET$LOGIN:$WHERE$WHEN\n$USERCOLOR$CHR $DECOLOR "
			PS1=`echo $PS1 | sed -e 's/\\\\\\]\\\\\\[//g'`
		else
			PS1="$CHARSET`whoami`@`hostname` $CHR "
		fi

set -

		unset USERCOLOR ROOTCOLOR SYSCOLOR HOSTCOLOR
	}

	makePrompt
	unset makePrompt

	export PATH PS1

	# set a fancy prompt (overwrite the one in /etc/profile)
	#PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

	# check the window size after each command and, if necessary,
	# update the values of LINES and COLUMNS.
	shopt -s checkwinsize

	# don't put duplicate lines in the history. See bash(1) for more options
	export HISTCONTROL=ignoredups

	# enable bash completion in interactive shells
	if [ -f /etc/bash_completion ]; then
		. /etc/bash_completion
	fi
fi



and here's the /etc/bash.bashrc.local for modularity -- all i
change on different hosts is the HOSTCOLOR. so at a glance i can
see if i'm root (red) a sysuser (green) or a normal user (blue).
and after a few moments of use, i can get quite familiar with
which host i'm on as well.



# local settings for use in /etc/bash.bashrc

USERCOLOR="BOLD WHITE ON_BLUE"
ROOTCOLOR="BOLD YELLOW ON_RED"
SYSCOLOR="BOLD YELLOW ON_GREEN"
HOSTCOLOR="BOLD RED ON_BLUE"


-- 
I use Debian/GNU Linux version 3.0;
Linux boss 2.4.18-bf2.4 #1 Son Apr 14 09:53:28 CEST 2002 i586 unknown
 
DEBIAN NEWBIE TIP #41 from Colin Watson <cjw44@flatline.org.uk>
:
Do you need to MASSAGE A BUNCH OF FILE NAMES? There's more
than one way to skin a cat -- here are some examples of
canonicalizing file names to lower-case:
	mmv \* \#l1
	rename 'tr/A-Z/a-z/' *
	zsh -c 'for x in *; do mv "$x" "${x:l}"; done'
(The "rename" command is a standard perl script, by the way.)

Also see http://newbieDoc.sourceForge.net/ ...



Reply to: