#!/bin/bash
##############################################################################
##
## Tööömas über prompt
## version 5
##
## Features:
## * works on Solaris, Mac OS X, *BSD, Cygwin, Putty, Linux, GNU screen
## * fast redraw
## * dynamicly set prompt string or title with 'prompt' and 'title' commands
## * set title for applications
## * visually distinctive, commands are well separated with a horizontal line
## * adapts to terminal resizing
## * no distracting colors.
##
## TODO:
## ksh rework
##
##############################################################################

## only load if interactive
test "x$PS1" = x && return 0

## pause one second to be sure the window has finished resizing
sleep 1

## get columns straight on solaris
case `uname` in
SunOS)
test "x$ROWS" = x && ROWS=`LC_ALL=C stty | grep "rows ="|cut -d" " -f3|cut -d";" -f1`
test "x$COLUMNS" = x && COLUMNS=`LC_ALL=C stty | grep "rows ="|cut -d" " -f6|cut -d";" -f1`
;;
*)
test "x$ROWS" = x && ROWS=`stty size | cut -d" " -f1`
test "x$COLUMNS" = x && COLUMNS=`stty size | cut -d" " -f2` 
esac
OLD_SIZE="$COLUMNS$LINES"
promptcmd() 
{
	title "$TITLE"
	test "x$OLD_SIZE" = "x$COLUMNS$LINES" && return 0
	updateprompt
}

if test "x$BASH" != x
then
## bash
updateprompt()
{
	OLD_SIZE="$COLUMNS$LINES"
	local UNDERLINE
	i=1
	while test "$i" -lt "$COLUMNS"
        do
                let i++
                UNDERLINE=$UNDERLINE"_"
        done
	## some crazy vt100 voodoo below to cope with writing a character in
	## the rightmost column without bash bailing out
	PS1="$UNDERLINE\n\[\033[A\033[${COLUMNS}G_\033[B\033[0G\033[1;7m\] "$PROMPTSTR" \[\033[0m\] "
}
elif test "x$ZSH_NAME" != x
then
## zsh
## PROMPT EXPANSION
setopt PROMPT_SUBST
updateprompt()
{
	OLD_SIZE="$COLUMNS$LINES"
	local UNDERLINE
	i=1
	while test "$i" -lt "$COLUMNS"
        do
                let i++
                UNDERLINE=$UNDERLINE"_"
        done
	PS1="$UNDERLINE
%{[A[${COLUMNS}G_[B[0G[1;7m%} "$PROMPTSTR" %{[0m%} "


}
elif test "x$RANDOM" != x
then
## some version of ksh
:

updateprompt()
{
	OLD_SIZE="$COLUMNS$LINES"
	typeset UNDERLINE=""
	i=1
	while test $i -lt "$COLUMNS"
        do
                let i++
                UNDERLINE=$UNDERLINE"_"
        done
	## some crazy vt100 voodoo below to cope with writing a character in
	## the rightmost column without bash bailing out
	
PS1="$UNDERLINE
[A[${COLUMNS}G_[B[0G[1;7m $PROMPTSTR [0m "
}


else
## bourne shell
:
fi
## set default title
title ()
{
	## set non-permanent title
	case "$@" in
	-h*) printf "\
title() is part of Uberprompt by Thomas Eriksson.
title sets the terminal window title.
Usage: title [-n] [TITLE]
-n sets a non-permanent title, otherwise the title will be re-set each time the prompt is displayed
Without title argument, a default title will be set.
"
	return 1
	;;
	-n*) shift; local TITLE;;
	esac
	test "x$DISPLAY" != x && {
		TITLE="$*"
	}
	if test "x$*" = x
	then
		TITLE="$HOSTNAME"
		test "x$UID" = x0 && TITLE="$TITLE #"
	fi
	export TITLE
	test "x$DISPLAY" != x && printf "\033]0;$TITLE\007"
	test "x$TERM" = xxterm && printf "\033]0;$TITLE\007"

}
title

## set default prompt string
prompt ()
{
	case "$@" in
	-h*) printf "\
prompt() is part of Uberprompt by Thomas Eriksson.
prompt sets the terminal prompt string in uberprompt.

Usage: prompt [PROMPT]
Without prompt argument, the hostname will be set.

Examples:

# slackwareisch prompt (bash specific)
prompt \"\\u@\\h:\\w\"

# run a command in the prompt, eg seconds since epoch
prompt \"\\\$(date +%s)\"
"
	return 1
	esac

	PROMPTSTR="$*"
	if test "x$*" = x
		
	then
test x$HOSTNAME = "x" && HOSTNAME=$(hostname)
PROMPTSTR="$HOSTNAME"
		test "x$UID" = x0 && PROMPTSTR="$PROMPTSTR #"
	fi
	updateprompt
}
prompt

## apptitle [APP] [TITLE]
apptitle ()
{
	case "$@" in
	-h*) printf "\
apptitle() is part of Uberprompt by Thomas Eriksson.
apptitle appends string to the title when an application is run.
Usage: apptitle [APP] [TITLE]
Without title argument, the application name is set appended to the title.
Examples:

# display arguments given to vim in the title, eg \"vim: foo.c\"
apptitle vim \"editing: \\\$@\"

# display the topmost path in the title, eg \"baking gcc-3.14\"
apptitle make \"baking \\\${PWD##*/}\"

# run a command in the title, eg \"Alien scanning started stardate 1198780941\"
apptitle setiathome \"Alien scanning started stardate \\\$(date +%%s)\"

"
	return 1
	esac

	which "$1" 1>/dev/null 2>/dev/null || { printf "apptitle: cannot find $1\n"; return 1;}
	## redirect title standard output to >/dev/tty to not interfere with other redirectings
	if test "x$2" = x
	then
		eval $1" (){ eval title -n \"\$TITLE: $1\" >/dev/tty ; $(which $1) \"\$@\"; }"
	else
		eval $1" (){ eval title -n \"\$TITLE: $2\" >/dev/tty ; $(which $1) \"\$@\"; }"
	fi
}

## Mac OS X Terminal.app and IRIX xwsh seems to send repetetive WINCH to solaris boxen
case "$TERM$OSTYPE" in
iris-ansisolaris*|screensolaris*|xterm-colorsolaris*) : ;;
*) trap updateprompt WINCH ;;
esac

cd () {
	unset -f cd
	updateprompt
	cd "$@"
	cd () {
		unset -f cd
		cd "$@"
	}
}
case $ZSH_NAME$BASH in
*zsh*) precmd() {
promptcmd
}
;;
*bash*)
PROMPT_COMMAND="promptcmd"
esac

