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

Re: Dynamic colored command in zsh



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