logo

mood

A mood-tracker in a POSIX Shell Script. git clone https://hacktivis.me/git/mood.git

mood (1223B)


  1. #!/bin/sh
  2. # Copyright 2018 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
  3. # Distributed under the terms of the ISC license
  4. moods_dir="$HOME/moods"
  5. git_auto_push="true"
  6. use_git="true"
  7. die() {
  8. if [ -n "$*" ]
  9. then echo "${*}, exiting…" >&2
  10. else echo "Failed, exiting…" >&2
  11. fi
  12. exit
  13. }
  14. mood_new() {
  15. date="$(date +%F)"
  16. cd "${moods_dir}"
  17. printf \
  18. "%s %s\n" \
  19. "$(date --rfc-3339=seconds)" \
  20. "$(echo -n "$*" | sed -e 's/^new //')" \
  21. >> "${date}.log" || die
  22. if $use_git
  23. then
  24. git add "${date}.log" || die
  25. git commit -m "${date}.log: Updated with mood new" || die
  26. if $git_auto_push
  27. then
  28. git push
  29. fi
  30. fi
  31. }
  32. mood_check() {
  33. date="$(date +%F)"
  34. if [ ! -e "${moods_dir}/${date}.log" ]
  35. then
  36. echo "mood: Please input your mood of the day. :3"
  37. fi
  38. }
  39. mood_usage() {
  40. cat >&2 <<EOF
  41. Usage: ${1} [new mood_description|check|show|list]
  42. Subcommands:
  43. new: inserts a new mood
  44. check: Reminds you to input your daily mood if not done
  45. show: show the mood(s) of the day
  46. list: list the mood(s) files
  47. EOF
  48. }
  49. case $1 in
  50. 'new')
  51. mood_new $*
  52. ;;
  53. 'check')
  54. mood_check
  55. ;;
  56. 'show')
  57. ${PAGER:-more} "${moods_dir}/${date}.log"
  58. ;;
  59. 'list')
  60. ls "${moods_dir}"
  61. ;;
  62. *)
  63. mood_usage $0
  64. exit 1
  65. ;;
  66. esac