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.

37 lines
1.6 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # FAST_HIGHLIGHT hash serves as container for variables that
  5. # prevents creating them in global scope. (P) flag is not used,
  6. # output array is fixed (__lines_list).
  7. #
  8. # $1 - the command, e.g. "git remote"; 2>/dev/null redirection is
  9. # added automatically
  10. # $2 - FAST_HIGHLIGHT field name, e.g. "chroma-git-branches"; two
  11. # additional fields will be used, $2-cache, $2-cache-born-at
  12. # $3 - what to remove from beginning of the lines returned by the
  13. # command
  14. # $4 - cache validity time, default 5 (seconds)
  15. #
  16. # Output: array __lines_list, with output of the command ran
  17. # User should not forget to define this array, the below code
  18. # will only ensure that it's array (can also define a global)
  19. typeset -ga __lines_list
  20. local -a __response
  21. if [[ -z "${FAST_HIGHLIGHT[$2-cache]}" || $(( EPOCHSECONDS - FAST_HIGHLIGHT[$2-cache-born-at] )) -gt ${4:-5} ]]; then
  22. FAST_HIGHLIGHT[$2-cache-born-at]="$EPOCHSECONDS"
  23. __response=( ${${(f)"$(command ${(Qz)1#+} 2>/dev/null)"}#${~3}} )
  24. [[ "$1" = "+"* ]] && __lines_list+=( "${__response[@]}" ) || __lines_list=( "${__response[@]}" )
  25. FAST_HIGHLIGHT[$2-cache]="${(j:;:)__response}"
  26. else
  27. # Quoted (s:;:) flag without @ will skip empty elements. It
  28. # still produces array output, interesingly. All this is for
  29. # the trailing ";" above, to skip last, empty element.
  30. [[ "$1" = "+"* ]] && \
  31. __lines_list+=( "${(@s:;:)FAST_HIGHLIGHT[$2-cache]}" ) || \
  32. __lines_list=( "${(@s:;:)FAST_HIGHLIGHT[$2-cache]}" )
  33. fi
  34. # vim:ft=zsh:et:sw=4