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

Re: Fw: Phil's prompt is not working when LANG is set to UTF-8



On Saturday 16 February 2008, Peter Stephenson wrote:
> How about the following tweak to prompts to support this?  The upshot is
> that you include any funny characters in %{...%G%} where the %G for
> `glitch' (which may be repeated or take a numeric argument) indicates a
> screen cell taken up by the sequence. 

Yes, I was about to try the same today morning (you have to sleep on it);
but I likely would not end up with such elegant solution.

> I like this because it uses 
> facilities that have been present in the shell for a long time and hence
> was trivial to implement and might work.
> 
> I played with this in simple cases, but would anybody like to confirm
> this works in the cases that matter (and maybe produce an updated Phil's
> Prompt)? 

I confirm that this works and updated prompt is attached. Wael, does it work
for you with this patch applied?

Now downside of course is, it works only with patched shell ...

> To put it another way:  I am happy to support this fix but 
> have no interest in doing anything with it myself.
> 
> I think this is clean and useful enough that I will commit it anyway.
# vim:ft=zsh:fenc=UTF-8:ts=4:sts=4:sw=4:expandtab:
# $Id$

function precmd {

    local TERMWIDTH
    (( TERMWIDTH = ${COLUMNS} - 1 ))


    ###
    # Truncate the path if it's too long.

    PR_FILLBAR=""
    PR_FILLBAR_PRE=""
    PR_FILLBAR_POST=""
    PR_PWDLEN=""

    local promptsize=${#${(%):---(%n@%m:%l)---()--}}
    local pwdsize=${#${(%):-%~}}

    if [[ "$promptsize + $pwdsize" -gt $TERMWIDTH ]]; then
	    ((PR_PWDLEN=$TERMWIDTH - $promptsize))
    else
	PR_FILLBAR="\${(l.(($TERMWIDTH - ($promptsize + $pwdsize)))..${PR_HBAR_PURE}.)}"
    PR_FILLBAR_PRE="%{"
    PR_FILLBAR_POST="%${#PR_FILLBAR}G%}"
    fi


    ###
    # Get APM info.

#    if which ibam &> /dev/null; then
#	PR_APM_RESULT=`ibam --percentbattery 2> /dev/null`
#    elif which apm &> /dev/null; then
#	PR_APM_RESULT=`apm 2> /dev/null`
#    fi
}


setopt extended_glob
preexec () {
    if [[ "$TERM" == "screen" ]]; then
	local CMD=${1[(wr)^(*=*|sudo|-*)]}
	echo -n "\ek$CMD\e\\"
    fi
}


setprompt () {
    ###
    # Need this so the prompt will work.

    setopt prompt_subst


    ###
    # See if we can use colors.

    autoload colors zsh/terminfo
    if [[ "$terminfo[colors]" -ge 8 ]]; then
	colors
    fi
    for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
	eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
	eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
	(( count = $count + 1 ))
    done
    PR_NO_COLOUR="%{$terminfo[sgr0]%}"


    ###
    # See if we can use extended characters to look nicer.

    typeset -A altchar
    set -A altchar ${(s..)terminfo[acsc]}
    PR_SET_CHARSET="%{$terminfo[enacs]%}"
    PR_SHIFT_IN="%{$terminfo[smacs]%}"
    PR_SHIFT_OUT="%{$terminfo[rmacs]%}"
    PR_HBAR_PURE="${altchar[q]:--}"
    PR_HBAR="%{${altchar[q]:--}%G%}"
    PR_ULCORNER="%{${altchar[l]:--}%G%}"
    PR_LLCORNER="%{${altchar[m]:--}%G%}"
    PR_LRCORNER="%{${altchar[j]:--}%G%}"
    PR_URCORNER="%{${altchar[k]:--}%G%}"


    ###
    # Decide if we need to set titlebar text.

    case $TERM in
	xterm*)
	    PR_TITLEBAR=$'%{\e]0;%(!.-=*[ROOT]*=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\a%}'
	    ;;
	screen*)
	    PR_TITLEBAR=$'%{\e_screen \005 (\005t) | %(!.-=[ROOT]=- | .)%n@%m:%~ | ${COLUMNS}x${LINES} | %y\e\\%}'
	    ;;
	*)
	    PR_TITLEBAR=''
	    ;;
    esac


    ###
    # Decide whether to set a screen title
    if echo "${TERM}" | grep -q "^screen"; then
	PR_STITLE=$'%{\ekzsh\e\\%}'
    else
	PR_STITLE=''
    fi


    ###
    # APM detection

#    if which ibam > /dev/null; then
#	PR_APM='$PR_RED${${PR_APM_RESULT[(f)1]}[(w)-2]}%%(${${PR_APM_RESULT[(f)3]}[(w)-1]})$PR_LIGHT_BLUE:'
#    elif which apm > /dev/null; then
#	PR_APM='$PR_RED${PR_APM_RESULT[(w)5,(w)6]/\% /%%}$PR_LIGHT_BLUE:'
#    else
#	PR_APM=''
#    fi


    ###
    # Finally, the prompt.

    PROMPT='$PR_SET_CHARSET$PR_STITLE${(e)PR_TITLEBAR}\
$PR_CYAN$PR_SHIFT_IN$PR_ULCORNER$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\
$PR_CYAN%(!.%SROOT%s.%n)$PR_YELLOW@$PR_GREEN%m$PR_YELLOW:$PR_BLUE%l\
$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_CYAN$PR_HBAR$PR_FILLBAR_PRE${(e)PR_FILLBAR}$PR_FILLBAR_POST$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\
$PR_MAGENTA%$PR_PWDLEN<...<%~%<<\
$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_CYAN$PR_URCORNER$PR_SHIFT_OUT\

$PR_CYAN$PR_SHIFT_IN$PR_LLCORNER$PR_BLUE$PR_HBAR$PR_SHIFT_OUT(\
%(?..$PR_LIGHT_RED%?$PR_BLUE:)\
${(e)PR_APM}$PR_YELLOW%D{%H:%M}\
$PR_LIGHT_BLUE:%(!.$PR_RED.$PR_WHITE)%#$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_CYAN$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_NO_COLOUR '

    RPROMPT=' $PR_CYAN$PR_SHIFT_IN$PR_HBAR$PR_BLUE$PR_HBAR$PR_SHIFT_OUT\
($PR_YELLOW%D{%a,%b%d}$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_CYAN$PR_LRCORNER$PR_SHIFT_OUT$PR_NO_COLOUR'

    PS2='$PR_CYAN$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_BLUE$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT(\
$PR_LIGHT_GREEN%_$PR_BLUE)$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT\
$PR_CYAN$PR_SHIFT_IN$PR_HBAR$PR_SHIFT_OUT$PR_NO_COLOUR '
}

setprompt
print -rnP -- "$PROMPT" > /tmp/PROMPT
print -rnP -- "$RPROMPT" > /tmp/RPROMPT

Attachment: signature.asc
Description: This is a digitally signed message part.



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