|
| 1 | +# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*- |
| 2 | +# Copyright (c) 2018 Sebastian Gniazdowski |
| 3 | +# |
| 4 | +# Chroma function for command `podman'. It verifies command line, by denoting |
| 5 | +# wrong and good arguments by color. Currently implemented: verification of |
| 6 | +# image IDs passed to: podman image rm <ID>. |
| 7 | +# |
| 8 | +# $1 - 0 or 1, denoting if it's first call to the chroma, or following one |
| 9 | +# $2 - the current token, also accessible by $__arg from the above scope - |
| 10 | +# basically a private copy of $__arg |
| 11 | +# $3 - a private copy of $_start_pos, i.e. the position of the token in the |
| 12 | +# command line buffer, used to add region_highlight entry (see man), |
| 13 | +# because Zsh colorizes by *ranges* in command line buffer |
| 14 | +# $4 - a private copy of $_end_pos from the above scope |
| 15 | +# |
| 16 | + |
| 17 | +(( next_word = 2 | 8192 )) |
| 18 | + |
| 19 | +local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4" |
| 20 | +local __style |
| 21 | +integer __idx1 __idx2 |
| 22 | +local -a __lines_list |
| 23 | + |
| 24 | +(( __first_call )) && { |
| 25 | + # Called for the first time - new command |
| 26 | + # FAST_HIGHLIGHT is used because it survives between calls, and |
| 27 | + # allows to use a single global hash only, instead of multiple |
| 28 | + # global variables |
| 29 | + FAST_HIGHLIGHT[chroma-podman-counter]=0 |
| 30 | + FAST_HIGHLIGHT[chroma-podman-got-subcommand]=0 |
| 31 | + FAST_HIGHLIGHT[chroma-podman-subcommand]="" |
| 32 | + FAST_HIGHLIGHT[chrome-podman-got-msg1]=0 |
| 33 | + return 1 |
| 34 | +} || { |
| 35 | + # Following call, i.e. not the first one |
| 36 | + |
| 37 | + # Check if chroma should end – test if token is of type |
| 38 | + # "starts new command", if so pass-through – chroma ends |
| 39 | + [[ "$__arg_type" = 3 ]] && return 2 |
| 40 | + |
| 41 | + if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then |
| 42 | + return 1 |
| 43 | + fi |
| 44 | + |
| 45 | + if [[ "$__wrd" = -* && ${FAST_HIGHLIGHT[chroma-podman-got-subcommand]} -eq 0 ]]; then |
| 46 | + __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option} |
| 47 | + else |
| 48 | + if (( FAST_HIGHLIGHT[chroma-podman-got-subcommand] == 0 )); then |
| 49 | + FAST_HIGHLIGHT[chroma-podman-got-subcommand]=1 |
| 50 | + FAST_HIGHLIGHT[chroma-podman-subcommand]="$__wrd" |
| 51 | + __style=${FAST_THEME_NAME}subcommand |
| 52 | + (( FAST_HIGHLIGHT[chroma-podman-counter] += 1 )) |
| 53 | + else |
| 54 | + __wrd="${__wrd//\`/x}" |
| 55 | + __arg="${__arg//\`/x}" |
| 56 | + __wrd="${(Q)__wrd}" |
| 57 | + if [[ "${FAST_HIGHLIGHT[chroma-podman-subcommand]}" = "image" ]]; then |
| 58 | + [[ "$__wrd" != -* ]] && { |
| 59 | + (( FAST_HIGHLIGHT[chroma-podman-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-podman-counter] )) |
| 60 | + |
| 61 | + if (( __idx1 == 2 )); then |
| 62 | + __style=${FAST_THEME_NAME}subcommand |
| 63 | + elif (( __idx1 == 3 )); then |
| 64 | + .fast-run-command "podman images -q" chroma-podman-list "" |
| 65 | + [[ -n "${__lines_list[(r)$__wrd]}" ]] && { |
| 66 | + (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \ |
| 67 | + reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}") |
| 68 | + } || { |
| 69 | + (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \ |
| 70 | + reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}") |
| 71 | + } |
| 72 | + fi |
| 73 | + } || __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option} |
| 74 | + else |
| 75 | + return 1 |
| 76 | + fi |
| 77 | + fi |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | +# Add region_highlight entry (via `reply' array) |
| 82 | +[[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}") |
| 83 | + |
| 84 | +# We aren't passing-through, do obligatory things ourselves |
| 85 | +(( this_word = next_word )) |
| 86 | +_start_pos=$_end_pos |
| 87 | + |
| 88 | +return 0 |
| 89 | + |
| 90 | +# vim:ft=zsh:et:sw=4 |
0 commit comments