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

Re: Dynamic colored command in zsh



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