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.

60 lines
2.3 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # It runs given command, which in general will be a git command,
  5. # automatically looking at cache first (a field named *-cache,
  6. # in FAST_HIGHLIGHT), which is valid for 5 seconds, and in case
  7. # of outdated or not existing cache, runs the command, splitting
  8. # on new-lines, first checking if PWD is inside git repository.
  9. #
  10. # FAST_HIGHLIGHT hash serves as container for variables that
  11. # prevents creating them in global scope. (P) flag is not used,
  12. # output array is fixed (__lines_list).
  13. #
  14. # $1 - the command, e.g. "git remote"; 2>/dev/null redirection is
  15. # added automatically
  16. # $2 - FAST_HIGHLIGHT field name, e.g. "chroma-git-branches"; two
  17. # additional fields will be used, $2-cache, $2-cache-born-at
  18. # $3 - what to remove from beginning of the lines returned by the
  19. # command
  20. # $4 - cache validity time, default 5 (seconds)
  21. #
  22. # Output: array __lines_list, with output of the (git) command ran
  23. # User should not forget to define this array, the below code
  24. # will only ensure that it's array (can also define a global)
  25. typeset -ga __lines_list
  26. local -a __response
  27. if [[ $1 == --status ]] {
  28. integer __status=1
  29. shift
  30. }
  31. if [[ -z ${FAST_HIGHLIGHT[$2-cache]} || $(( EPOCHSECONDS - FAST_HIGHLIGHT[$2-cache-born-at] )) -gt ${4:-5} ]]; then
  32. FAST_HIGHLIGHT[$2-cache-born-at]=$EPOCHSECONDS
  33. if [[ "$(command git rev-parse --is-inside-work-tree 2>/dev/null)" = true ]]; then
  34. __response=( ${${(f)"$(command ${(Qz)${1#+}} 2>/dev/null)"}#$3} )
  35. integer retval=$?
  36. if (( __status )) {
  37. __response=( $retval )
  38. __lines_list=( $retval )
  39. } else {
  40. [[ "$1" = "+"* ]] && \
  41. __lines_list+=( "${__response[@]}" ) || \
  42. __lines_list=( "${__response[@]}" )
  43. }
  44. else
  45. __lines_list=()
  46. fi
  47. FAST_HIGHLIGHT[$2-cache]="${(j:;:)__response}"
  48. else
  49. # Quoted (s:;:) flag without @ will skip empty elements. It
  50. # still produces array output, interesingly. All this is for
  51. # the trailing ";" above, to skip last, empty element.
  52. [[ "$1" = "+"* ]] && \
  53. __lines_list+=( "${(@s:;:)FAST_HIGHLIGHT[$2-cache]}" ) || \
  54. __lines_list=( "${(@s:;:)FAST_HIGHLIGHT[$2-cache]}" )
  55. fi
  56. # vim:ft=zsh:et:sw=4