New chroma -docker.ch' that verifies image ID passed to image rm'

This commit is contained in:
Sebastian Gniazdowski 2018-06-09 20:06:57 +02:00
parent 868812a77b
commit 17a28baad4
4 changed files with 118 additions and 2 deletions

30
-fast-run-command Normal file
View file

@ -0,0 +1,30 @@
# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# Copyright (c) 2018 Sebastian Gniazdowski
#
# FAST_HIGHLIGHT hash serves as container for variables that
# prevents creating them in global scope. (P) flag is not used,
# output array is fixed (__lines_list).
#
# $1 - the command, e.g. "git remote"; 2>/dev/null redirection is
# added automatically
# $2 - FAST_HIGHLIGHT field name, e.g. "chroma-git-branches"; two
# additional fields will be used, $2-cache, $2-cache-born-at
# $3 - what to remove from beginning of the lines returned by the
# command
# $4 - cache validity time, default 5 (seconds)
#
# Output: array __lines_list, with output of the command ran
# User should not forget to define this array, the below code
# will only ensure that it's array (can also define a global)
typeset -ga __lines_list
if [[ -z "${FAST_HIGHLIGHT[$2-cache]}" || $(( EPOCHSECONDS - FAST_HIGHLIGHT[$2-cache-born-at] )) -gt ${4:-5} ]]; then
FAST_HIGHLIGHT[$2-cache-born-at]="$EPOCHSECONDS"
__lines_list=( ${${(f)"$(${(Qz)1} 2>/dev/null)"}#$3} )
FAST_HIGHLIGHT[$2-cache]="${(j:;:)__lines_list};"
else
__lines_list=( "${(s:;:)FAST_HIGHLIGHT[$2-cache]}" )
fi
# vim:ft=zsh:et:sw=4

84
chroma/-docker.ch Normal file
View file

@ -0,0 +1,84 @@
# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# Copyright (c) 2018 Sebastian Gniazdowski
#
# Chroma function for command `docker'. It verifies command line, by denoting
# wrong and good arguments by color. Currently implemented: verification of
# image IDs passed to: docker image rm <ID>.
#
# $1 - 0 or 1, denoting if it's first call to the chroma, or following one
# $2 - the current token, also accessible by $__arg from the above scope -
# basically a private copy of $__arg
# $3 - a private copy of $_start_pos, i.e. the position of the token in the
# command line buffer, used to add region_highlight entry (see man),
# because Zsh colorizes by *ranges* in command line buffer
# $4 - a private copy of $_end_pos from the above scope
#
(( next_word = 2 | 8192 ))
local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
local __style
integer __idx1 __idx2
local -a __lines_list
(( __first_call )) && {
# Called for the first time - new command
# FAST_HIGHLIGHT is used because it survives between calls, and
# allows to use a single global hash only, instead of multiple
# global variables
FAST_HIGHLIGHT[chroma-docker-counter]=0
FAST_HIGHLIGHT[chroma-docker-got-subcommand]=0
FAST_HIGHLIGHT[chroma-docker-subcommand]=""
FAST_HIGHLIGHT[chrome-docker-got-msg1]=0
__style=${FAST_THEME_NAME}command
} || {
# Following call, i.e. not the first one
# Check if chroma should end test if token is of type
# "starts new command", if so pass-through chroma ends
[[ "$__arg_type" = 3 ]] && return 2
if [[ "$__wrd" = -* && ${FAST_HIGHLIGHT[chroma-docker-got-subcommand]} -eq 0 ]]; then
__style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option}
else
if (( FAST_HIGHLIGHT[chroma-docker-got-subcommand] == 0 )); then
FAST_HIGHLIGHT[chroma-docker-got-subcommand]=1
FAST_HIGHLIGHT[chroma-docker-subcommand]="$__wrd"
__style=${FAST_THEME_NAME}builtin
(( FAST_HIGHLIGHT[chroma-docker-counter] += 1 ))
else
__wrd="${__wrd//\`/x}"
__arg="${__arg//\`/x}"
__wrd="${(Q)__wrd}"
if [[ "${FAST_HIGHLIGHT[chroma-docker-subcommand]}" = "image" ]]; then
[[ "$__wrd" != -* ]] && {
(( FAST_HIGHLIGHT[chroma-docker-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-docker-counter] ))
if (( __idx1 == 2 )); then
__style=${FAST_THEME_NAME}reserved-word
elif (( __idx1 == 3 )); then
-fast-run-command "docker images -q" chroma-docker-list ""
[[ -n "${__lines_list[(r)$__wrd]}" ]] && {
(( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}")
} || {
(( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
}
fi
} || __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option}
fi
fi
fi
}
# Add region_highlight entry (via `reply' array)
[[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
# We aren't passing-through, do obligatory things ourselves
(( this_word = next_word ))
_start_pos=$_end_pos
return 0
# vim:ft=zsh:et:sw=4

View file

@ -156,6 +156,7 @@ FAST_HIGHLIGHT+=(
chroma-perl chroma/-perl.ch
chroma-git chroma/-git.ch
chroma-example chroma/-example.ch
chroma-docker chroma/-docker.ch
)
# Assignments seen, to know if math parameter exists

View file

@ -272,9 +272,10 @@ ZSH_HIGHLIGHT_MAXLENGTH=10000
# Load zsh/parameter module if available
zmodload zsh/parameter 2>/dev/null
autoload -Uz -- is-at-least fast-theme fast-read-ini-file -fast-run-git-command -fast-make-targets
autoload -Uz -- is-at-least fast-theme fast-read-ini-file -fast-run-git-command -fast-make-targets \
-fast-run-command
autoload -Uz -- chroma/-git.ch chroma/-example.ch chroma/-grep.ch chroma/-perl.ch chroma/-make.ch \
chroma/-awk.ch chroma/-vim.ch chroma/-source.ch chroma/-sh.ch
chroma/-awk.ch chroma/-vim.ch chroma/-source.ch chroma/-sh.ch chroma/-docker.ch
source "${ZERO:h}/fast-highlight"
local __fsyh_theme