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

Re: Dynamic colored command in zsh



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