zsh-users
 help / color / mirror / code / Atom feed
* Dynamic colored command in zsh
@ 2010-09-12 10:04 Michel
  2010-09-13 13:03 ` Roy Zuo
  0 siblings, 1 reply; 14+ messages in thread
From: Michel @ 2010-09-12 10:04 UTC (permalink / raw)
  To: zsh-users

Hello,

Is it possible to have the dynamic colored command in zsh as in fish ?

By example : if I type "grep", for each caracter before the "p" the
word is colored in red but when I type the last caractere the word is
colored in green. The coloring should be appled on parameters, string
(between 2 " or '), the color should be different if the word is
alias, function or command.

Thanks for your answer

Nice day
_____________________
Michel BARRET


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-12 10:04 Dynamic colored command in zsh Michel
@ 2010-09-13 13:03 ` Roy Zuo
  2010-09-13 13:25   ` Julien Nicoulaud
  0 siblings, 1 reply; 14+ messages in thread
From: Roy Zuo @ 2010-09-13 13:03 UTC (permalink / raw)
  To: Michel; +Cc: zsh-users

Yes, I tried today and made some progress. I can make the color for the command
to change whenever I type a new character or delete one. However my ZLE skill is
quite limited and someone else might be able to make this also work when TAB is
pressed.

    recolor-cmd() {
        args=(${(z)BUFFER})
        cmd=$args[1]
        res=$(builtin type $cmd 2>/dev/null)
        [ -z $res ]  && return
        if [ $res =~ 'reserved word' ]; then
            color="magenta"
        elif [ $res =~ 'an alias' ]; then

            color="cyan"
        elif [ $res =~ 'shell builtin' ]; then
            color="yellow"
        elif [ $res =~ 'shell function' ]; then
            color='green'
        elif [ $res =~ "$cmd is" ]; then
            color="blue"
        else
            color="red"
        fi
        region_highlight=("0 ${#cmd} fg=${color},bold")
    }

    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

Regards,

Roy

On Sun, Sep 12, 2010 at 12:04:08PM +0200, Michel wrote:
> Hello,
> 
> Is it possible to have the dynamic colored command in zsh as in fish ?
> 
> By example : if I type "grep", for each caracter before the "p" the
> word is colored in red but when I type the last caractere the word is
> colored in green. The coloring should be appled on parameters, string
> (between 2 " or '), the color should be different if the word is
> alias, function or command.
> 
> Thanks for your answer
> 
> Nice day
> _____________________
> Michel BARRET
> 

-- 
 _________________________ 
< Murphy was an optimist. >
 ------------------------- 
       \   ,__,
        \  (oo)____
           (__)    )\
              ||--|| *


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-13 13:03 ` Roy Zuo
@ 2010-09-13 13:25   ` Julien Nicoulaud
  2010-09-13 13:44     ` Roy Zuo
                       ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Julien Nicoulaud @ 2010-09-13 13:25 UTC (permalink / raw)
  To: Michel, zsh-users

[-- Attachment #1: Type: text/plain, Size: 2087 bytes --]

Wow, that would be great !

Roy, can you give us the options you use ? The "=~" equalities throw errors
for me.

2010/9/13 Roy Zuo <roylzuo@gmail.com>

> Yes, I tried today and made some progress. I can make the color for the
> command
> to change whenever I type a new character or delete one. However my ZLE
> skill is
> quite limited and someone else might be able to make this also work when
> TAB is
> pressed.
>
>    recolor-cmd() {
>        args=(${(z)BUFFER})
>        cmd=$args[1]
>        res=$(builtin type $cmd 2>/dev/null)
>        [ -z $res ]  && return
>        if [ $res =~ 'reserved word' ]; then
>            color="magenta"
>        elif [ $res =~ 'an alias' ]; then
>
>            color="cyan"
>        elif [ $res =~ 'shell builtin' ]; then
>            color="yellow"
>        elif [ $res =~ 'shell function' ]; then
>            color='green'
>        elif [ $res =~ "$cmd is" ]; then
>            color="blue"
>        else
>            color="red"
>        fi
>        region_highlight=("0 ${#cmd} fg=${color},bold")
>    }
>
>    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
>
> Regards,
>
> Roy
>
> On Sun, Sep 12, 2010 at 12:04:08PM +0200, Michel wrote:
> > Hello,
> >
> > Is it possible to have the dynamic colored command in zsh as in fish ?
> >
> > By example : if I type "grep", for each caracter before the "p" the
> > word is colored in red but when I type the last caractere the word is
> > colored in green. The coloring should be appled on parameters, string
> > (between 2 " or '), the color should be different if the word is
> > alias, function or command.
> >
> > Thanks for your answer
> >
> > Nice day
> > _____________________
> > Michel BARRET
> >
>
> --
>  _________________________
> < Murphy was an optimist. >
>  -------------------------
>       \   ,__,
>        \  (oo)____
>           (__)    )\
>              ||--|| *
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-13 13:25   ` Julien Nicoulaud
@ 2010-09-13 13:44     ` Roy Zuo
  2010-09-13 14:14     ` Sebastian Stark
  2010-09-13 14:16     ` Frank Terbeck
  2 siblings, 0 replies; 14+ messages in thread
From: Roy Zuo @ 2010-09-13 13:44 UTC (permalink / raw)
  To: Julien Nicoulaud; +Cc: Michel, zsh-users

Hi Julien,

Which version of zsh are you using? If you are using 4.2.x, i.e. the default
version under Centos 5, you would have this problem because operator "=~" is not
defined. Even if you are able to get around this, you still cannot use my script
because "region_highlight" is not defined in 4.2.x too.

Roy

On Mon, Sep 13, 2010 at 03:25:09PM +0200, Julien Nicoulaud wrote:
> Wow, that would be great !
> 
> Roy, can you give us the options you use ? The "=~" equalities throw errors
> for me.
> 
> 2010/9/13 Roy Zuo <roylzuo@gmail.com>
> 
> > Yes, I tried today and made some progress. I can make the color for the
> > command
> > to change whenever I type a new character or delete one. However my ZLE
> > skill is
> > quite limited and someone else might be able to make this also work when
> > TAB is
> > pressed.
> >
> >    recolor-cmd() {
> >        args=(${(z)BUFFER})
> >        cmd=$args[1]
> >        res=$(builtin type $cmd 2>/dev/null)
> >        [ -z $res ]  && return
> >        if [ $res =~ 'reserved word' ]; then
> >            color="magenta"
> >        elif [ $res =~ 'an alias' ]; then
> >
> >            color="cyan"
> >        elif [ $res =~ 'shell builtin' ]; then
> >            color="yellow"
> >        elif [ $res =~ 'shell function' ]; then
> >            color='green'
> >        elif [ $res =~ "$cmd is" ]; then
> >            color="blue"
> >        else
> >            color="red"
> >        fi
> >        region_highlight=("0 ${#cmd} fg=${color},bold")
> >    }
> >
> >    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
> >
> > Regards,
> >
> > Roy
> >
> > On Sun, Sep 12, 2010 at 12:04:08PM +0200, Michel wrote:
> > > Hello,
> > >
> > > Is it possible to have the dynamic colored command in zsh as in fish ?
> > >
> > > By example : if I type "grep", for each caracter before the "p" the
> > > word is colored in red but when I type the last caractere the word is
> > > colored in green. The coloring should be appled on parameters, string
> > > (between 2 " or '), the color should be different if the word is
> > > alias, function or command.
> > >
> > > Thanks for your answer
> > >
> > > Nice day
> > > _____________________
> > > Michel BARRET
> > >
> >
> > --
> >  _________________________
> > < Murphy was an optimist. >
> >  -------------------------
> >       \   ,__,
> >        \  (oo)____
> >           (__)    )\
> >              ||--|| *
> >

-- 
 ______________________________________________________________________ 
/ MSDOS didn't get as bad as it is overnight -- it took over ten years \
| of careful development.                                              |
|                                                                      |
\ -- dmeggins@aix1.uottawa.ca                                          /
 ---------------------------------------------------------------------- 
       \   ,__,
        \  (oo)____
           (__)    )\
              ||--|| *


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-13 13:25   ` Julien Nicoulaud
  2010-09-13 13:44     ` Roy Zuo
@ 2010-09-13 14:14     ` Sebastian Stark
  2010-09-13 14:34       ` Julien Nicoulaud
  2010-09-13 14:16     ` Frank Terbeck
  2 siblings, 1 reply; 14+ messages in thread
From: Sebastian Stark @ 2010-09-13 14:14 UTC (permalink / raw)
  To: Julien Nicoulaud; +Cc: Michel, zsh-users



Am 13.09.2010 um 15:25 schrieb Julien Nicoulaud:
> 
> Roy, can you give us the options you use ? The "=~" equalities throw errors
> for me.

It works if you replace [ ] with [[ ]]. I don't see how this could work with [ ].


Sebastian

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-13 13:25   ` Julien Nicoulaud
  2010-09-13 13:44     ` Roy Zuo
  2010-09-13 14:14     ` Sebastian Stark
@ 2010-09-13 14:16     ` Frank Terbeck
  2010-09-13 23:33       ` Roy Zuo
  2 siblings, 1 reply; 14+ messages in thread
From: Frank Terbeck @ 2010-09-13 14:16 UTC (permalink / raw)
  To: zsh-users

Julien Nicoulaud <julien.nicoulaud@gmail.com>:
> Wow, that would be great !
> 
> Roy, can you give us the options you use ? The "=~" equalities throw errors
> for me.
> 
> 2010/9/13 Roy Zuo <roylzuo@gmail.com>
[...]
> >        if [ $res =~ 'reserved word' ]; then
[...]

There is no need for regex use here. You can just use

case $res in
    *'reserved word'*)
        # code
        ;;
    ...
esac

...or use [[ $res == *'reserved word'* ]] instead of the above test.

Regards, Frank


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-13 14:14     ` Sebastian Stark
@ 2010-09-13 14:34       ` Julien Nicoulaud
  2010-09-18 16:03         ` Richard Hartmann
  0 siblings, 1 reply; 14+ messages in thread
From: Julien Nicoulaud @ 2010-09-13 14:34 UTC (permalink / raw)
  To: Sebastian Stark; +Cc: Michel, zsh-users

[-- Attachment #1: Type: text/plain, Size: 515 bytes --]

Yes, Sebastien is right (using zsh 4.3.10 on Ubuntu here). Works great.

I created a collaborative notepad, feel free to improve it:
http://ethercodes.com/zDUmDAuWiV

<http://ethercodes.com/zDUmDAuWiV>Julien

2010/9/13 Sebastian Stark <seb-zsh@biskalar.de>

>
>
> Am 13.09.2010 um 15:25 schrieb Julien Nicoulaud:
> >
> > Roy, can you give us the options you use ? The "=~" equalities throw
> errors
> > for me.
>
> It works if you replace [ ] with [[ ]]. I don't see how this could work
> with [ ].
>
>
> Sebastian

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-13 14:16     ` Frank Terbeck
@ 2010-09-13 23:33       ` Roy Zuo
  0 siblings, 0 replies; 14+ messages in thread
From: Roy Zuo @ 2010-09-13 23:33 UTC (permalink / raw)
  To: zsh-users

Thanks Sebastin and Frank, I myself am never a decent shell programmer :).

Roy

On Mon, Sep 13, 2010 at 04:16:27PM +0200, Frank Terbeck wrote:
> Julien Nicoulaud <julien.nicoulaud@gmail.com>:
> > Wow, that would be great !
> > 
> > Roy, can you give us the options you use ? The "=~" equalities throw errors
> > for me.
> > 
> > 2010/9/13 Roy Zuo <roylzuo@gmail.com>
> [...]
> > >        if [ $res =~ 'reserved word' ]; then
> [...]
> 
> There is no need for regex use here. You can just use
> 
> case $res in
>     *'reserved word'*)
>         # code
>         ;;
>     ...
> esac
> 
> ...or use [[ $res == *'reserved word'* ]] instead of the above test.
> 
> Regards, Frank
> 

-- 
 _________________________________________ 
/ Fain would I climb, yet fear I to fall. \
|                                         |
\ -- Sir Walter Raleigh                   /
 ----------------------------------------- 
       \   ,__,
        \  (oo)____
           (__)    )\
              ||--|| *


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-13 14:34       ` Julien Nicoulaud
@ 2010-09-18 16:03         ` Richard Hartmann
  2010-09-18 23:09           ` Roy Zuo
  0 siblings, 1 reply; 14+ messages in thread
From: Richard Hartmann @ 2010-09-18 16:03 UTC (permalink / raw)
  To: Julien Nicoulaud; +Cc: Sebastian Stark, Michel, zsh-users

On Mon, Sep 13, 2010 at 16:34, Julien Nicoulaud
<julien.nicoulaud@gmail.com> 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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-18 16:03         ` Richard Hartmann
@ 2010-09-18 23:09           ` Roy Zuo
  2010-09-18 23:58             ` Michel
  0 siblings, 1 reply; 14+ messages in thread
From: Roy Zuo @ 2010-09-18 23:09 UTC (permalink / raw)
  To: Richard Hartmann; +Cc: Julien Nicoulaud, Sebastian Stark, Michel, zsh-users

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@gmail.com> 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)____
           (__)    )\
              ||--|| *


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-18 23:09           ` Roy Zuo
@ 2010-09-18 23:58             ` Michel
  2010-09-19 11:50               ` Julien Nicoulaud
  0 siblings, 1 reply; 14+ messages in thread
From: Michel @ 2010-09-18 23:58 UTC (permalink / raw)
  To: zsh-users

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@gmail.com>:
> 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@gmail.com> 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)____
>           (__)    )\
>              ||--|| *
>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-18 23:58             ` Michel
@ 2010-09-19 11:50               ` Julien Nicoulaud
  2010-09-19 14:39                 ` Mikael Magnusson
  0 siblings, 1 reply; 14+ messages in thread
From: Julien Nicoulaud @ 2010-09-19 11:50 UTC (permalink / raw)
  To: Michel; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 4588 bytes --]

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@gmail.com>

> 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@gmail.com>:
> > 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@gmail.com> 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

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-19 11:50               ` Julien Nicoulaud
@ 2010-09-19 14:39                 ` Mikael Magnusson
  2010-09-19 14:55                   ` Mikael Magnusson
  0 siblings, 1 reply; 14+ messages in thread
From: Mikael Magnusson @ 2010-09-19 14:39 UTC (permalink / raw)
  To: Julien Nicoulaud; +Cc: Michel, zsh-users

On 19 September 2010 13:50, Julien Nicoulaud <julien.nicoulaud@gmail.com> wrote:
> 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.

I posted a crazy thing a while back, see
http://www.zsh.org/mla/workers/2009/msg01225.html , that function is
called every time the line is redrawn _including on cursor movement_,
so it is probably very slow, but it should at least look correct. It
requires patching zsh though, so it's not very portable.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Dynamic colored command in zsh
  2010-09-19 14:39                 ` Mikael Magnusson
@ 2010-09-19 14:55                   ` Mikael Magnusson
  0 siblings, 0 replies; 14+ messages in thread
From: Mikael Magnusson @ 2010-09-19 14:55 UTC (permalink / raw)
  To: Julien Nicoulaud; +Cc: Michel, zsh-users

On 19 September 2010 16:39, Mikael Magnusson <mikachu@gmail.com> wrote:
> On 19 September 2010 13:50, Julien Nicoulaud <julien.nicoulaud@gmail.com> wrote:
>> 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.
>
> I posted a crazy thing a while back, see
> http://www.zsh.org/mla/workers/2009/msg01225.html , that function is
> called every time the line is redrawn _including on cursor movement_,
> so it is probably very slow, but it should at least look correct. It
> requires patching zsh though, so it's not very portable.

I should perhaps also note that this patch breaks some things like ^r
history search in some subtle ways. (and possibly other things).

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2010-09-19 14:56 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-12 10:04 Dynamic colored command in zsh Michel
2010-09-13 13:03 ` Roy Zuo
2010-09-13 13:25   ` Julien Nicoulaud
2010-09-13 13:44     ` Roy Zuo
2010-09-13 14:14     ` Sebastian Stark
2010-09-13 14:34       ` Julien Nicoulaud
2010-09-18 16:03         ` Richard Hartmann
2010-09-18 23:09           ` Roy Zuo
2010-09-18 23:58             ` Michel
2010-09-19 11:50               ` Julien Nicoulaud
2010-09-19 14:39                 ` Mikael Magnusson
2010-09-19 14:55                   ` Mikael Magnusson
2010-09-13 14:16     ` Frank Terbeck
2010-09-13 23:33       ` Roy Zuo

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).