summary refs log blame commit diff stats
path: root/portable-prompt.sh
blob: eaecac2462a7281ab1006a4e2fb73c4f4ddfd4a6 (plain) (tree)








































                                                                                                  










                                     









                                                                               
                                                                                           














                                                                                        
                                    


















                                                


















































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

                                                                             




















                                            






                                 




















                                       
                                      












                                                                                                                              
#set -x
#alias local=typeset
# don't activate for bash or zsh for now
#[ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ] && return

# If not running interactively, don't do anything
#[ -z "$PS1" ] && return
setopt PROMPT_SUBST

if [ "$$" = "1" ]; then
	echo "Whoah?! I'm init!"
	PREFIX="${PREFIX}[I AM INIT] "
fi

SHNAME=$(. whatshell)

SERIAL=''
if [ -r /proc/self/fd/0 ]; then
	TTY_FULL=$(LANG=C readlink /proc/self/fd/0 2>/dev/null || LANG=C realpath /proc/self/fd/0)
else
	TTY_FULL=$(tty)
fi
TTY=${TTY_FULL#/dev/}
export TTY

case "$TTY_FULL" in
	/dev/ttyS*)
		SERIAL=1
		;;
esac


prompt_helper(){
	# display return code in prompt 
	RET=$?
	if [ $RET = 0 ]; then
		RET=
	else
		RET="[$RET]"
	fi

	local pwd
	case $PWD in
	$HOME*)
		pwd="~${PWD#"$HOME"}"
		;;
	*)
		pwd=$PWD
		;;
	esac
	PWEXT=$pwd

	if [ "$CHECKWINSIZE" ]; then
		set -- $(stty size)
		export LINES=$1
		export COLUMNS=$2		
		#echo "$COLUMNS x $LINES"
	fi
	# workaround for updating terminal size when connecting via serial port
	if [ "$SERIAL" ]; then
		local rows
		local cols
		IFS='[;' read -rp $'\033\033[r\033[999;999H\033[6n\033' -d R -s _ rows cols
		if [ "x$rows $cols" != "x$LINES $COLUMNS" ]; then
			stty rows "$rows" cols "$cols"
		fi
	fi

	if [ "$RPROMPT" ]; then
		#local RP1=${RPROMPT@P}
		#local RP2=${RP1//$'\1'+([!$'\2'])$'\2'/}
		local RP1=$RPROMPT
		local RP2=$RPROMPT
		printf "\0337\033[$((COLUMNS - ${#RP2} + 1 ))G%s\0338" "$RP1" > /dev/tty
	fi
#printf "%s" "$PS1" > /dev/tty

	case "$TERM" in
	(xterm*|rxvt*|vt102|screen*)
		pretty_title "$SHNAME";;
	esac
	return 0
}

set_title(){
	#set -x
        printf "\033]0;%s\a" "${*-}" > /dev/tty
}

pretty_title(){
        local extra=${*-}

        local user=
        if [ "$EUID" -ne 0 ]; then
                user="/${USER}"
        fi
        local host=${DISPLAYHOST-$HOSTNAME}
        local tty="(${COLUMNS}x${LINES} ${TTY})"
        #local pwd=${PWD/$HOME/~}

        if [ -n "$extra" ]; then
                extra=" <$extra>"
        fi

        set_title "$host$user:$extra $pwd $tty"
}

blackterm(){
        setfg ffffff
        setbg 000000
        tango
        color dark
}
whiteterm(){
        setfg 000000
        setbg ffffff
        tango
        color light
}
grayterm(){
        setfg bebebe
        setbg 333333
        tango
        color dark
}

setcolors(){
        local i=0
        #printf "\033]4;"
        #for arg; do
        #        printf "%s;#%s" "$i" "$arg"
        #        ((++i))
        #done
	#printf "\a"
        #i=0
        for arg; do
                printf "\033]P%x%s\a" "$i" "$arg"
        	printf "\033]4;%s;#%s\a" "$i" "$arg"
                ((++i))
        done
}

tango(){
        setcolors \
                1e1e1e \
                cc0000 \
                4e9a06 \
                c4a000 \
                3464a4 \
                75507b \
                0b939b \
                d3d7cf \
                555753 \
                ef2929 \
                8ae234 \
                fce94f \
                729fcf \
                ad7fa8 \
                00f5e9 \
                eeeeec
}

setopt(){
        printf "\033]%s;#%s\a" "$1" "$2"
}

setfg(){
	setopt 10 "$1"
	setopt 12 "$1"
	setopt 13 "$1"
	setopt 17 "$1"
        printf "\033]Pg%s\a" "$1"
}

setbg(){
	setopt 11 "$1"
	setopt 14 "$1"
        printf "\033]Ph%s\a" "$1"
}

preexec(){
#set -x
        if [ "$SHOW_COMMAND" ]; then
		local command
		if [ -n "$BASH_COMMAND" ]; then
			command=$BASH_COMMAND
		elif [ -n "$ZSH_VERSION" ]; then
			command=$1
		else
			eval 'command=${.sh.command}'
			#echo XXX $command
		fi
#echo "FOOOBAR $command"
                #local short=${command:0:50}
                local short
                eval 'short=${command:0:50}'
                pretty_title "$short"
                case "$TERM" in screen)
                        printf "\033%.20s\033\\" "$short"
                esac
        fi
}

LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:';
export LS_COLORS

set -o emacs
#alias grep='grep --color=auto'
#alias grpe='grep --color=auto'
#alias gerp='grep --color=auto'
#alias ls='ls -F --color=auto'
alias ls='ls -F'
alias sc='du -schx -- *|sort -h'
#alias vi='vim "+sy on" "+colo elflord" "+se nu" "+se ai" "+se si" "+se hls"'
alias vi='vim "+sy on" "+colo elflord" "+se nu ai si hls"'

: "${EUID:=$(id -u)}"
CSI=$(printf '\033[')
if [ -n "$YASH_VERSION$BASH_VERSION" ]; then
	START="\["
	SC="$START$CSI"
	END="\]"
	EC="m$END"
else
	SC="$CSI"
	EC="m"
	START=
	END=
fi

RED="${SC}1;31$EC"
GREEN="${SC}1;32$EC"
BLUE="${SC}1;34$EC"
DBLUE="${SC}34$EC"
STD="${SC}$EC"

BGCOLOR="48;5;52"
BGCOLOR=""
if [ -n "$BGCOLOR" ]; then
	STD="${SC}0;$BGCOLOR$EC"
	PREFIX="$STD$PREFIX"
	printf '%s' "$STD${CSI}J"
fi

: "${HOSTNAME:=$(hostname)}"
DISPLAYHOST=${HOSTNAME%%.*}

CHECKWINSIZE=1
SHOW_COMMAND=1

if [ "$EUID" = 0 ]; then
        UCOLOR=$RED
	SYMBOL='#'
else
        UCOLOR=$GREEN
	SYMBOL='$'
fi

case "$TERM" in
dumb)
        PS1="${DISPLAYHOST}.\w$SYMBOL "
        ;;
*)
	RPROMPT=
	RPROMPT="$(basename "$SHELL")"
	PS1="$PREFIX${UCOLOR}${DISPLAYHOST}${STD}.${BLUE}$START\$(prompt_helper;printf %s \"\$PWEXT\$RET\")$END${STD}$SYMBOL "
	#PS1="$PREFIX${UCOLOR}${DISPLAYHOST}${STD}.${BLUE}\$(prompt_helper)\$PWEXT\$RET${STD}$SYMBOL "
        ;;
esac


echo "siprof"

# LASTARG: save and restore the value of $_
#if [ -z "$ZSH_VERSION" ]; then
#	trap 'LASTARG="$_";$PREEXEC_COMMAND;: $LASTARG' DEBUG
#fi
#PREEXEC_COMMAND="preexec"