Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Dynamic colored command in zsh
- X-seq: zsh-users 15410
- From: Michel <michel.barret@xxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Dynamic colored command in zsh
- Date: Sun, 19 Sep 2010 01:58:04 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:content-type :content-transfer-encoding; bh=i0fbpTf2V5VF87GIdh+J6pJiBVrT0fehYNsS+3tGiqk=; b=dZUM1Tqb/xmZAmMFFDy26C3ZRR/rK9BmLbp80v6behLdxwpPoBgyD9LAwQZiHFp/ZW ZAhuS6YXCHkh11gn0hKKCUF0ioYlW3hWJ5rHIypMbVEPCdVhrIUXng+Yy6XQmzDP5Cri Ec8oezaL+0a/ahBD/IKu97uaY882akcBOLoGI=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=RTvqPPRQgrnw1gCWQzGhSiXOV+EJrNUczEIV0Tgb699t5Kb1AlK24qyGk8rj30jE2z LgkkK6ukNMkOzF/pZ+hxPQ6pLOpHMoH9XkJ+TOeEuBM0Yq9s/Wv4/EQYrAXP34A1ueCg r1PlBdC0IeTxaxE+bxfypvQCEcZdp2Ypj1xF0=
- In-reply-to: <20100918230945.GA4530@Lancelot>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <AANLkTinNJXay4pibEiQmKR4PSRcNiwCJ3w==Scc9rco0@xxxxxxxxxxxxxx> <20100913130345.GA9240@Lancelot> <AANLkTikq=h-awW+-HAB8-QWdg0dWTuY+uydMu-Ezmt-0@xxxxxxxxxxxxxx> <6227C736-CCD3-4E09-A922-106573F3D70D@xxxxxxxxxxx> <AANLkTimj6n_qSd3_5Egk6RN=v47GwWBb+W3_MjWpMs4t@xxxxxxxxxxxxxx> <AANLkTi=28cEq+XhxCy7pYKtu+bmSNG3QSKMfBUckJ7Tc@xxxxxxxxxxxxxx> <20100918230945.GA4530@Lancelot>
Thanks everybody for your help.
I have work with the first code give by Roy and I have this :
color_cmd() {
res=$(builtin type $1 2>/dev/null)
[ -z $res ] && return
case $res in
*'reserved word'*) color='magenta' ;;
*'an alias'*) color='cyan' ;;
*'shell builtin'*) color='yellow' ;;
*'shell function'*) color='green' ;;
*"$cms is"*) color='blue' ;;
*) color='red'
esac
case $cmd in
'sudo') state=1 ;;
'start') state=1 ;;
'time') state=1 ;;
'strace') state=1 ;;
*) state=2
esac
}
color_arg() {
case $1 in
'--'*) color='magenta' ;;
'-'*) color='cyan' ;;
*) color='red'
esac
}
color_string() {
case $1 in
'"'*) color='yellow' ;;
"'"*) color='yellow' ;;
*) color=''
esac
}
recolor-cmd() {
args=(${(z)BUFFER})
offset=0
state=1
region_highlight=()
for cmd in $args; do
if [ $state -eq 1 ]; then
color_cmd $cmd
elif [ $state -eq 2 ]; then
color_arg $cmd
if [[ "$color" =~ 'red' ]]; then
color_string $cmd
fi
fi
if [ -n "$color" ]; then
region_highlight=($region_highlight "$offset $((${#cmd}+offset))
fg=${color},bold")
fi
offset=$((offset+${#cmd}+1))
case $cmd in
*'|') state=1 ;;
*'&') state=1 ;;
*';') state=1 ;;
esac
done
}
check-cmd-self-insert() { zle .self-insert && recolor-cmd }
check-cmd-backward-delete-char() { zle .backward-delete-char && recolor-cmd }
#check-cmd-expand-or-complete() { zle .expand-or-complete } # && recolor-cmd }
zle -N self-insert check-cmd-self-insert
#zle -N expand-or-complete check-cmd-expand-or-complete
zle -N backward-delete-char check-cmd-backward-delete-char
_____________________
Michel BARRET
2010/9/19 Roy Zuo <roylzuo@xxxxxxxxx>:
> Not exactly the same code, but the same feature and less bugs.
>
> Â Ârecolor-cmd() {
> Â Â Â Âregion_highlight=()
> Â Â Â Âcolorize=true
> Â Â Â Âstart_pos=0
> Â Â Â Âfor arg in ${(z)BUFFER}; do
> Â Â Â Â Â Â((start_pos+=${#BUFFER[$start_pos+1,-1]}-${#${BUFFER[$start_pos+1,-1]## #}}))
> Â Â Â Â Â Â((end_pos=$start_pos+${#arg}))
> Â Â Â Â Â Âif $colorize; then
> Â Â Â Â Â Â Â Âcolorize=false
> Â Â Â Â Â Â Â Âres=$(LC_ALL=C builtin type $arg 2>/dev/null)
> Â Â Â Â Â Â Â Âcase $res in
> Â Â Â Â Â Â Â Â Â Â*'reserved word'*) Â style="fg=magenta,bold";;
> Â Â Â Â Â Â Â Â Â Â*'alias for'*) Â Â Â style="fg=cyan,bold";;
> Â Â Â Â Â Â Â Â Â Â*'shell builtin'*) Â style="fg=yellow,bold";;
> Â Â Â Â Â Â Â Â Â Â*'shell function'*) Âstyle='fg=green,bold';;
> Â Â Â Â Â Â Â Â Â Â*"$arg is"*)
> Â Â Â Â Â Â Â Â Â Â Â Â[[ $arg = 'sudo' ]] && style="fg=red,bold" || style="fg=blue,bold";;
> Â Â Â Â Â Â Â Â Â Â*) Â Â Â Â Â Â Â Â Â style='none,bold';;
> Â Â Â Â Â Â Â Âesac
> Â Â Â Â Â Â Â Âregion_highlight+=("$start_pos $end_pos $style")
> Â Â Â Â Â Âfi
> Â Â Â Â Â Âif [[ $arg = '|' ]] || [[ $arg = 'sudo' ]]; then
> Â Â Â Â Â Â Â Â Âcolorize=true
> Â Â Â Â Â Âfi
> Â Â Â Â Â Âstart_pos=$end_pos
> Â Â Â Âdone
> Â Â}
>
> Â Âcheck-cmd-self-insert() { zle .self-insert && recolor-cmd }
> Â Âcheck-cmd-backward-delete-char() { zle .backward-delete-char && recolor-cmd }
>
> Â Âzle -N self-insert check-cmd-self-insert
> Â Âzle -N backward-delete-char check-cmd-backward-delete-char
>
> Roy
>
> On Sat, Sep 18, 2010 at 06:03:42PM +0200, Richard Hartmann wrote:
>> On Mon, Sep 13, 2010 at 16:34, Julien Nicoulaud
>> <julien.nicoulaud@xxxxxxxxx> wrote:
>>
>> > I created a collaborative notepad, feel free to improve it:
>> > http://ethercodes.com/zDUmDAuWiV
>>
>> That link does not seem to work, any more.
>>
>>
>> Richard
>>
>
> --
> Â____________________________________________________________
> / Nothing will dispel enthusiasm like a small admission fee. \
> | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â|
> \ -- Kim Hubbard                       /
> Â------------------------------------------------------------
> Â Â Â \ Â ,__,
> Â Â Â Â\ Â(oo)____
> Â Â Â Â Â (__) Â Â)\
> Â Â Â Â Â Â Â||--|| *
>
Messages sorted by:
Reverse Date,
Date,
Thread,
Author