You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.9 KiB

  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 `docker'. It verifies command line, by denoting
  5. # wrong and good arguments by color. Currently implemented: verification of
  6. # image IDs passed to: docker 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. (( next_word = 2 | 8192 ))
  17. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  18. local __style
  19. integer __idx1 __idx2
  20. local -a __lines_list
  21. (( __first_call )) && {
  22. # Called for the first time - new command
  23. # FAST_HIGHLIGHT is used because it survives between calls, and
  24. # allows to use a single global hash only, instead of multiple
  25. # global variables
  26. FAST_HIGHLIGHT[chroma-docker-counter]=0
  27. FAST_HIGHLIGHT[chroma-docker-got-subcommand]=0
  28. FAST_HIGHLIGHT[chroma-docker-subcommand]=""
  29. FAST_HIGHLIGHT[chrome-docker-got-msg1]=0
  30. return 1
  31. } || {
  32. # Following call, i.e. not the first one
  33. # Check if chroma should end – test if token is of type
  34. # "starts new command", if so pass-through – chroma ends
  35. [[ "$__arg_type" = 3 ]] && return 2
  36. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  37. return 1
  38. fi
  39. if [[ "$__wrd" = -* && ${FAST_HIGHLIGHT[chroma-docker-got-subcommand]} -eq 0 ]]; then
  40. __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option}
  41. else
  42. if (( FAST_HIGHLIGHT[chroma-docker-got-subcommand] == 0 )); then
  43. FAST_HIGHLIGHT[chroma-docker-got-subcommand]=1
  44. FAST_HIGHLIGHT[chroma-docker-subcommand]="$__wrd"
  45. __style=${FAST_THEME_NAME}subcommand
  46. (( FAST_HIGHLIGHT[chroma-docker-counter] += 1 ))
  47. else
  48. __wrd="${__wrd//\`/x}"
  49. __arg="${__arg//\`/x}"
  50. __wrd="${(Q)__wrd}"
  51. if [[ "${FAST_HIGHLIGHT[chroma-docker-subcommand]}" = "image" ]]; then
  52. [[ "$__wrd" != -* ]] && {
  53. (( FAST_HIGHLIGHT[chroma-docker-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-docker-counter] ))
  54. if (( __idx1 == 2 )); then
  55. __style=${FAST_THEME_NAME}subcommand
  56. elif (( __idx1 == 3 )); then
  57. .fast-run-command "docker images -q" chroma-docker-list ""
  58. [[ -n "${__lines_list[(r)$__wrd]}" ]] && {
  59. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
  60. reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}")
  61. } || {
  62. (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
  63. reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
  64. }
  65. fi
  66. } || __style=${FAST_THEME_NAME}${${${__wrd:#--*}:+single-hyphen-option}:-double-hyphen-option}
  67. else
  68. return 1
  69. fi
  70. fi
  71. fi
  72. }
  73. # Add region_highlight entry (via `reply' array)
  74. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  75. # We aren't passing-through, do obligatory things ourselves
  76. (( this_word = next_word ))
  77. _start_pos=$_end_pos
  78. return 0
  79. # vim:ft=zsh:et:sw=4