#!/bin/zsh -f



# prompt_simple_setup  

# Create a simple minimal prompt (eg: zsh-%), and put the other usual stuff
# into the title bar (and if availble) the $PWD and penultimate directory 
# in the tab. Also enable transient display of a running command (eg vim).
# Currently works with iTerm (OSX) and konsole (KDE) tabs, and should work
# with any standard X-like term (and Apple's Terminal.app).

# Issue "prompt_simple_help" for customization of colors.

version="2.0.5"

# Bugs: prompt_simple_preview function doesn't work with "setopt globsubst".

###############################################################################
 
#  Created by William G. Scott.
#  Copyright (c) 2007. All rights reserved.


#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#    USA
#    
#    cf. URL:   http://www.fsf.org/licensing/licenses/gpl.html
#
###############################################################################



# Use a minimal prompt, together with an informative title bar and (iTerm) tab
 
autoload -U colors
colors


function prompt_simple_help {

print " 
A simple non-dynamic prompt accompanied by dynamically updated title bar and tab labels.


\e[1m prompt simple\e[0m  [\e[1m<\e[0mcolor1\e[1m>\e[0m[\e[1m<\e[0mcolor2\e[1m>\e[0m[\e[1m<\e[0mcolor3\e[1m>\e[0m[\e[1m<\e[0mstring1\e[1m>\e[0m[\e[1m<\e[0mstring2\e[1m>\e[0m[\e[1m<\e[0mstring3\e[1m>\e[0m]]]]]]

  Supply up to three \e[1mcolors\e[0m and then up to three alternate static \e[1mprompt strings \e[0m.

	\e[1mcolor1\e[0m is the local prompt color: \e[1mzsh-% \e[0m   		color1=default
	\e[1mcolor2\e[0m is the remote prompt color: \e[31m$HOST:r:r-%\e[0m  		color2=red
 	\e[1mcolor3\e[0m is the screensession prompt color: \e[1mScreen-% \e[0m  	color3=default

  Allowed \e[1mcolorN\e[0m options include:           
    
    \e[1mdefault\e[0m, \e[0m black\e[0m, \e[31m red\e[0m, \e[32m green\e[0m, \e[33m yellow\e[0m, \e[34m blue\e[0m, \e[35m magenta\e[0m, \e[36m cyan\e[0m, \e[0m white\e[0m  

  \e[1mstringN\e[0m can be any (non-dynamic) text string or prompt special characters (eg: %m)
" 
}

function prompt_simple_setup {  
	
	# ---------- Input ---------------------------- 
	# Local prompt color and (static) string:

		if [[ -z "$1"  ]];then
			1="default"
		fi
	
		if [[ -z $4 ]]; then
			4="zsh"
		fi
	

	# Remote prompt color and (static) string:

		if [[ -z "$2"  ]];then
			2="red"
		fi
	
		if [[ -z "$5"  ]];then
			5="%m"
		fi
	
	 
	# Screen prompt color and (static) string:

		if [[ -z "$3"  ]];then
			3="default"
		fi
		
		if [[ -z "$6"  ]];then
			if [[ $STY == *$HOST:r:r ]];then
				6="Screen-$STY:r:r" 
		    else
		        6="Screen-$STY:e"
		    fi
		fi
 

	# --------------------------------------------- 
 
	if [[ -n $WINDOW && -n $STY  ]];then

	    # If this is a screen session, define prompt as follows:
	    # If screen is invoked without a name for the session, use
	    # the session number in the prompt.
	    # If screen is invoked with "screen -S foo", use
	    # the title "foo" in the prompt. The default is boldface.
	
		PS1="%B%{${fg[$3]}%}$6-%# %{${fg[default]}%}%b"
	    export PS1
   
	elif [[ -z $SSH_CONNECTION ]]; then
 
	    # If this is a local interactive session
	    # the default is a boldface zsh-%  prompt
	
		PS1="%B%{${fg[$1]}%}$4-%# %{${fg[default]}%}%b"
		export PS1


	else

	    # Define a prompt for ssh sessions on remote computer:
	    # The default is red boldface with hostname-% prompt

	    PS1="%B%{${fg[$2]}%}$5-%# %{${fg[default]}%}%b"
	    export PS1 
 
	fi  
}

function prompt_simple_preview {
	setopt noglobsubst
	function preexec { }
	function precmd { }
	if (( ! $#* )); then
		prompt_preview_theme simple
		print
		prompt_preview_theme simple local-prompt-color remote-prompt-color screensession-prompt-color
	else
		prompt_preview_theme simple "$@"
	fi
	setopt globsubst
	unfunction preexec  
	unfunction precmd  
}

function set_tabs_and_title {

    function settab   {
	
		    # file settab  -- invoked only if iTerm or Konsole is running 

		    #  Set iterm window tab to current directory and penultimate directory if the
		    #  shell process is running.  Truncate to leave the rightmost $rlength characters.
		    #
		    #  Use with functions settitle (to set iterm title bar to current directory)
		    #  and chpwd


		if [[ $TERM_PROGRAM == iTerm.app && -z "$KONSOLE_DCOP_SESSION" ]];then

			# The $rlength variable prints only the 20 rightmost characters. Otherwise iTerm truncates
			# what appears in the tab from the left.
	     

				# Chage the following to change the string that actually appears in the tab:
						 
					tab_label="$PWD:h:t/$PWD:t"
		 

					rlength="20"   # number of characters to appear before truncation from the left

		            echo -ne "\e]1;${(l:rlength:)tab_label}\a"
	     

		else  

				# For KDE konsole tabs 

				# Chage the following to change the string that actually appears in the tab:

					tab_label="$PWD:h:t/$PWD:t"

					rlength="20"   # number of characters to appear before truncation from the left

		        # If we have a functioning KDE console, set the tab in the same way
		        if [[ -n "$KONSOLE_DCOP_SESSION" && ( -x $(which dcop)  )  ]];then
		                dcop "$KONSOLE_DCOP_SESSION" renameSession "${(l:rlength:)tab_label}"
		        else
		            : # do nothing if tabs don't exist
		        fi    
	
		fi
	}

    function settitle   {
		# Function "settitle"  --  set the title of the iterm title bar. use with chpwd and settab

		# Change the following string to change what appears in the Title Bar label:


			title_lab=$HOST:r:r::$PWD  
				# Prints the host name, two colons, absolute path for current directory

			# Change the title bar label dynamically:

			echo -ne "\e]2;[zsh]   $title_lab\a"
	}

	# Set tab and title bar dynamically using above-defined functions

		function prompt_simple_chpwd { settab ; settitle }
		
		# Now we need to run it:
	    prompt_simple_chpwd

	# Set tab or title bar label transiently to the currently running command
	
	if [[ "$TERM_PROGRAM" == "iTerm.app" ]];then		
		function prompt_simple_preexec {  echo -ne "\e]1; $(history $HISTCMD | cut -b7- ) \a"  } 
		function prompt_simple_precmd  { settab }	
	else
		function prompt_simple_preexec {  echo -ne "\e]2; $(history $HISTCMD | cut -b7- ) \a"  } 
		function prompt_simple_precmd  { settitle }      
	fi

 	# Use reserved named arrays instead of special functions if the ZSH version is 4.3.4 or above
 
		typeset -ga preexec_functions
		preexec_functions+=prompt_simple_preexec

		typeset -ga precmd_functions
		precmd_functions+=prompt_simple_precmd

		typeset -ga chpwd_functions
		chpwd_functions+=prompt_simple_chpwd
		
	# Otherwise we need to do this for older versions of zsh:
	
	if [[ $ZSH_VERSION < 4.3.4 ]];then
		function preexec { $preexec_functions }
		function precmd  { $precmd_functions  }
		function  chpwd  { $chpwd_functions   }
	fi
 
}

####################

set_tabs_and_title
 
prompt_simple_setup "$@"


