custom 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/sh -e
  2. #
  3. # custom: run the user's custom status scripts
  4. #
  5. # Copyright (C) 2009 Canonical Ltd.
  6. # Copyright (C) 2011-2014 Dustin Kirkland
  7. #
  8. # Authors: Dustin Kirkland <kirkland@byobu.org>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, version 3 of the License.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. __custom_detail() {
  22. return
  23. }
  24. __custom() {
  25. local cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/custom"
  26. local i= output=
  27. for i in "$BYOBU_CONFIG_DIR/bin/"[0-9]*_*; do
  28. # Loop over custom scripts
  29. # Ensure executable
  30. [ -x "$i" ] || continue
  31. # Ignore vim backup files ending in ~
  32. case "$i" in
  33. *~) continue ;;
  34. esac
  35. local now="$(date +%s)"
  36. local script=${i##*/}
  37. local freq=${script%%_*}
  38. freq=${freq#0}
  39. local lastrun=
  40. [ -r "$cache.$script.last" ] && read lastrun < "$cache.$script.last" || lastrun=0
  41. local expiration=$(($lastrun+$freq))
  42. if [ $now -ge $expiration ]; then
  43. "$i" > "$cache.$script" 2>/dev/null
  44. printf "%s\n" "${now}" > "$cache.$script.last"
  45. fi
  46. readfile < "$cache.$script"
  47. local str="${_RET}"
  48. case "$str" in
  49. *"$ESC{"*)
  50. # User has formatted the colors
  51. output="$output$(printf "$str")"
  52. ;;
  53. *)
  54. # Default coloring
  55. output="$output$str "
  56. ;;
  57. esac
  58. done
  59. if [ -z "$output" ]; then
  60. rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/custom"*
  61. return
  62. fi
  63. printf "$output" | $BYOBU_SED ':a;N;$!ba;s/\n//g'
  64. }
  65. # vi: syntax=sh ts=4 noexpandtab