Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Submitting vcs_info function



On 2008-09-21 at 09:11 +0000, Clint Adams wrote:
> On Wed, Sep 17, 2008 at 10:32:42PM +0200, Frank Terbeck wrote:
> > +If you plan to use the information from var(vcs_info) in your prompt (which
> > +is its primary use), you need to enable the tt(PROMPT_SUBST) option.
> 
> I don't think that this claim is accurate.

Ditto.  Although it would be nice if vcs_info made all the different
variables available in a $Z_VCS hash or somesuch, so that I didn't need
to zstyle from my prompt.

FWIW, here's a lightly modified version of the prompt I'm using now,
built up over years, originally with help from zefram@ and then later
with some attention to Bart's setup and generally munged about.  I
finally updated it to use the new colour codes, since the vcs_info stuff
led to me using more elements of $psvar, deciding to add %(V..)
conditional to check if set and then seeing that CVS had already done
just that.  Convenient.  :)

Reports screen WINDOW, SHLVL, previous command exit status (including
signal name if sig-exit), count of backgrounded jobs, repository
information plus the usual bits.  Has some automatic screen titling
stuff in the prompt too.

----------------------------8< cut here >8------------------------------
# Phil's prompt setup for zsh
#
# $HeadURL: https://svn.spodhuis.org/ksvn/spodhuis-tools/zsh/site-functions/prompt_pdp-vcs_setup $
# $Id: prompt_pdp-vcs_setup 239 2008-09-20 21:37:30Z xxx@xxxxxxxxxxxx $
#
# Installs somewhere in fpath; typically one of:
#  /usr/globnix/share/zsh/site-functions/prompt_pdp-vcs_setup
#  ~/bin/zsh-functions/
#
# Requires: zsh 4.3.7 or newer (for %(V..) prompt conditional)

zmodload -i zsh/parameter || return 1

function prompt_pdp-vcs_help {
	<<EOF
This prompt shows some information always and some when necessary.
The information depends in part upon \$TERM, as does emission of a
title-changing escape sequence (for xterms, etc).
OnDemand: '[num]' showing the SHLVL if not 1.
OnDemand: '{errcode}' or '{SIGNAME}' if previous command not successful.
Backgrounded jobs indication; '-' for none or '<+num>' showing count.
Then: usercode@xxxxxxxxxxxxxxxx:tty[time]
OnDemand: VCS repository information
Then: (history-num)
A left-truncated section of the pwd.
The %/# prompt character and a space.
Right-hand prompt: VCS type or absent
TERM=screen: prefix '<WINDOW>' and a screen-magic escape sequence for titling.
Optional prompt setup parameters:
 update -- do not reset any colour variables
EOF
}

function prompt_pdp-vcs_precmd {
	local exitstatus=$?
	emulate -L zsh	# ksh_arrays breaks us badly
	psvar=()
	psvar[1]=SIG
	[[ $exitstatus -ge 128 ]] && psvar[1]=SIG$signals[$exitstatus-127]
	[[ $psvar[1] == SIG ]] && psvar[1]=$exitstatus
	vcs_info
	psvar[2]=$#jobstates; [[ $psvar[2] -eq 0 ]] && psvar[2]=''
	psvar[3]=$VCS_INFO_message_1_
	psvar[4]=$VCS_INFO_message_0_
	return $exitstatus
}

function prompt_pdp-vcs_setup__colour {
	local endc
	typeset -gA prompt_pdp_vcs__hl prompt_pdp_vcs__ehl

	prompt_pdp_vcs__hl=()
	prompt_pdp_vcs__ehl=()

	prompt_pdp_vcs__hl[name]='%F{green}'
	prompt_pdp_vcs__hl[mach]='%F{yellow}'
	#prompt_pdp_vcs__hl[time]='%F{yellow}'
	prompt_pdp_vcs__hl[level]='%U'
	prompt_pdp_vcs__ehl[level]='%u'
	prompt_pdp_vcs__hl[error]='%B%F{red}'
	prompt_pdp_vcs__ehl[error]='%f%b'
	prompt_pdp_vcs__hl[jobcount]='%B'
	prompt_pdp_vcs__ehl[jobcount]='%b'
	# commandcount here
	prompt_pdp_vcs__hl[path]='%F{cyan}'
	prompt_pdp_vcs__hl[window]='%B%F{magenta}'
	prompt_pdp_vcs__ehl[window]='%f%b'
	prompt_pdp_vcs__hl[root]='%K{red}'
	prompt_pdp_vcs__ehl[root]='%k'
	prompt_pdp_vcs__hl[vcstype]='%F{green}'
	prompt_pdp_vcs__hl[vcsbranch]='%F{magenta}'
	return 1
}

function prompt_pdp-vcs_hl {
	local what="$1"; shift
	local text="$1"; shift
	local start end
	
	start="${prompt_pdp_vcs__hl[$what]}"
	if [[ -z $start ]]; then
		end=''
	else
		end="${prompt_pdp_vcs__ehl[$what]}"
		: ${end:='%f'}
	fi
	print -r -- "${start}${text}${end}"
}

function prompt_pdp-vcs_setup {
	local -h name root host mach line time level error jobc pathdisp vcs
	local -h p1 prefix='' trunclen=40
	local -h employer
	local -h docolour=:
	[[ $1 == update ]] && docolour=false

# this should be set by a style:
	local -h hostlabelcount=2
# foo.local stet; foo.example.tld => foo; foo.bar.example.tld => foo.bar
# foo.bar.example.co.cc => foo.bar; BUT foo.example.co.cc => foo.example
# (the last accepted reluctantly as the best that can be done without
# encoding per-TLD hierarchy information)
	[[ ${#${HOST//[^\.]/}} -eq 2 ]] && hostlabelcount=1

	[[ -n $COLORTERM ]] && $docolour && prompt_pdp-vcs_setup__colour "$@"

	case ${TERM} in
	(putty|xterm*)
		[[ -z $COLORTERM ]] && export COLORTERM="$TERM"
		function chpwd { [[ -t 0 ]] || return; print -Pn "\e]2;%n@%${hostlabelcount}m:%~\a" }
		$docolour && prompt_pdp-vcs_setup__colour
		chpwd
		;;
	(screen|screen.*)
		unfunction chpwd >/dev/null 2>&1 || true
		# \x5c is backslash '\'; screen(1) §TITLES
		prefix="$(prompt_pdp-vcs_hl window "<$WINDOW>")%{$(print '\ek\e\x5c')%}"
		trunclen=33
		;;
	(cons*|linux)
		[[ -z $COLORTERM ]] && export COLORTERM="$TERM"
		unfunction chpwd >/dev/null 2>&1 || true
		$docolour && prompt_pdp-vcs_setup__colour
		;;
	(*)
		unfunction chpwd >/dev/null 2>&1 || true
		;;
	esac

	zstyle ':vcs_info:*' branchformat %b:%r
	zstyle ':vcs_info:*' formats %b %s
	zstyle ':vcs_info:*' actionformats "%b|%a" %s

# Employer-dependent $HOST check elided from post
	employer=''

	root="$(prompt_pdp-vcs_hl root %n)"
	name="$(prompt_pdp-vcs_hl name "%(#.${root}.%n)")"
	host="$(prompt_pdp-vcs_hl mach "%${hostlabelcount}m")"
	line="$(prompt_pdp-vcs_hl line '%l')"
	time="$(prompt_pdp-vcs_hl time '[%T]')"
	level="%2(L.$(prompt_pdp-vcs_hl level '[%L]').)"
	error="%(?..$(prompt_pdp-vcs_hl error '{%v}'))"
	jobc="%(2V:<$(prompt_pdp-vcs_hl jobcount '+%2v')>:-)"
	cmdc="($(prompt_pdp-vcs_hl commandcount '%!'))"
	pathdisp="$(prompt_pdp-vcs_hl path "%${trunclen}<..<%~%<<")"
	vcs="%(3V:$(prompt_pdp-vcs_hl vcsbranch '<%4v>'):)"

	PS1="$prefix$level$error$jobc$name@$host$employer:$line$time$vcs$cmdc$pathdisp%# "
	PS2='%(4_:... :)%3_> '
	RPS1="%(3V:$(prompt_pdp-vcs_hl vcstype '(%3v)'):)"
	psvar=()
	function precmd { prompt_pdp-vcs_precmd }
	function preexec { }
}

prompt_pdp-vcs_setup "$@"
----------------------------8< cut here >8------------------------------



Messages sorted by: Reverse Date, Date, Thread, Author