Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Dynamic colored command in zsh
- X-seq: zsh-users 15411
- From: Julien Nicoulaud <julien.nicoulaud@xxxxxxxxx>
- To: Michel <michel.barret@xxxxxxxxx>
- Subject: Re: Dynamic colored command in zsh
- Date: Sun, 19 Sep 2010 13:50:59 +0200
- Cc: zsh-users@xxxxxxx
- 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:cc:content-type; bh=uDfG1XB471wsrk/gJCkWbKGwFQnfX1xaLuJWXMskCmA=; b=Z+OZ505JU+TLMP7HTDp6MPiL/R1JI0YiJj8bhrU1BCqWFDa/5zwfZ/XxQUtO0vZ4jg 17NBXkkyfIxAJSL3Xc5l0muLskBvsmAfmVzymQGDIEp64AOnxh5jSK2+BgbYos/EsW++ lfa1ofN05c5vTcjB8i6ykDudTZuS2S7sRWum8=
- 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 :cc:content-type; b=mvwmJPzkGn9EJNnsQJjLg9O+xADCW/wtCrCQIAgkYmnHgZa0nh04oi5jn2hDsfEL/q fYXz7PGLelV31jNKuaeknLmMMS+nAgGdu1YGOy03ZqClz9o1uGiqAokouWi9X24gq1Zq DvoXHv+26Gky3oufGtfym8mD6yBp0bOrb8NT0=
- In-reply-to: <AANLkTin4ufgJ-5WHPxojvuukJiksMaqywFJtU0-So1Mo@xxxxxxxxxxxxxx>
- 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> <AANLkTin4ufgJ-5WHPxojvuukJiksMaqywFJtU0-So1Mo@xxxxxxxxxxxxxx>
My version merged with Roy's last one that handles inconsistent spacing in
the command: http://gist.github.com/586698
Nobody found how to trigger it whenever the buffer changes ? I tried with
_refresh without success.
2010/9/19 Michel <michel.barret@xxxxxxxxx>
> 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)____
> > (__) )\
> > ||--|| *
> >
>
-- Julien Nicoulaud
Messages sorted by:
Reverse Date,
Date,
Thread,
Author