farend.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #!/bin/sh
  2. # --------------------------------------
  3. # name: farend
  4. # desc: a simple todo system in shell
  5. # main: jaidyn ann <jadedctrl@teknik.io>
  6. # --------------------------------------
  7. # Take in everything in stdin, then print
  8. # (Useful for piping something into a variable)
  9. function reade {
  10. local stack=""
  11. while read input; do
  12. stack="$(printf '%s\n%s' "$stack" "$input")"
  13. done
  14. echo "$stack"
  15. }
  16. # --------------------------------------
  17. # BASIC MATH
  18. # Add together two numbers
  19. function add {
  20. local a="$1"; local b="$2"
  21. echo "$a + $b" \
  22. | bc
  23. }
  24. # Subtract two numbers
  25. function subtract {
  26. local a="$1"; local b="$2"
  27. echo "$a - $b" \
  28. | bc
  29. }
  30. # Increment a number by one
  31. function inc {
  32. local a="$1"
  33. add 1 "$a"
  34. }
  35. # Decrement a number by one
  36. function dec {
  37. local a="$1"
  38. subtract "$a" 1
  39. }
  40. # Format a number to be a certain amount of digits long
  41. # (done by prepending zeroes)
  42. function digits {
  43. local number="$(reade)"
  44. local digits="$1"
  45. if test "$(dec "$(echo "$number" | wc -c )")" -lt "$(inc "$digits")"
  46. then
  47. local i=1;
  48. while test $i -lt "$digits"; do
  49. printf "0";
  50. i="$(inc "$i")"
  51. done
  52. printf %s $number
  53. else
  54. printf %s $number;
  55. fi
  56. }
  57. # --------------------------------------
  58. # DATE PRIMITIVES
  59. # Return today's date, YYYY-MM-DD
  60. function today {
  61. date +"%Y-%m-%d"
  62. }
  63. # Return the day of a given date
  64. function date_day {
  65. local date="$1"
  66. echo "$date" \
  67. | awk -F "-" '{print $3}'
  68. }
  69. # Return the month of a given date
  70. function date_month {
  71. local date="$1"
  72. echo "$date" \
  73. | awk -F "-" '{print $2}'
  74. }
  75. # Return the year of a given date
  76. function date_year {
  77. local date="$1"
  78. echo "$date" \
  79. | awk -F "-" '{print $1}'
  80. }
  81. # Return how many days ought to be in the given month
  82. function month_days {
  83. local month="$1"
  84. case "$month" in
  85. 09) echo 30;;
  86. 04) echo 30;;
  87. 06) echo 30;;
  88. 11) echo 30;;
  89. *) echo 31;;
  90. esac
  91. }
  92. # --------------------------------------
  93. # DATE ARITHMETIC
  94. # Add an amount of days to a given date
  95. function add_days {
  96. local date="$1"
  97. local days_added="$2"
  98. local day="$(date_day "$date")"; local month="$(date_month "$date")"
  99. local year="$(date_year "$date")"
  100. local new_day="$(add "$day" "$days_added" | digits 2)"
  101. carry_date "${year}-${month}-$new_day"
  102. }
  103. # Return whether or not a given date is valid
  104. # (I.E., not too month months or days)
  105. function is_balanced_date {
  106. local date="$1"
  107. local month="$(date_month "$date")"
  108. local day="$(date_day "$date")"
  109. local month_max="$(month_days "$month")"
  110. if test "$month" -gt 12; then
  111. return 1
  112. elif test "$day" -gt "$month_max" ; then
  113. return 1
  114. else
  115. return 0
  116. fi
  117. }
  118. # Correct an unbalanced date
  119. function carry_date {
  120. local date="$1"
  121. is_balanced_date "$date"
  122. while test "$?" -eq 1; do
  123. date="$(carry_months "$(carry_days "$date")")"
  124. done
  125. echo "$date"
  126. }
  127. # If too many days in a given month, carry them over
  128. function carry_days {
  129. local date="$1"
  130. local year="$(date_year "$date")"
  131. local month="$(date_month "$date")"
  132. local days="$(date_day "$date")"
  133. if test "$days" -gt "$(month_days "$month")"; then
  134. local new_days="$(subtract "$days" "$(month_days "$month")")"
  135. local new_month="$(add "$month" "1")"
  136. new_days="$(echo "$new_days" | digits 2)"
  137. new_month="$(echo "$new_month" | digits 2)"
  138. echo "${year}-${new_month}-${new_days}"
  139. else
  140. echo "$date"
  141. fi
  142. }
  143. # If too many months in a year, carry them over
  144. function carry_months {
  145. local date="$1"
  146. local year="$(date_year "$date")"
  147. local day="$(date_day "$date")"
  148. local month="$(date_month "$date")"
  149. if test "$month" -gt 12; then
  150. month="$(subtract "$month" 12 | digits 2)"
  151. year="$(inc "$year")"
  152. fi
  153. echo "${year}-${month}-${day}"
  154. }
  155. # --------------------------------------
  156. # TODO HANDLING
  157. # Clean up a todo file for use with the program
  158. function preprocess_todo {
  159. ignore_todo_blanks \
  160. | demystify_todo_times
  161. }
  162. # Replace piped todo's vague dates with current dates
  163. function demystify_todo_times {
  164. local year="$(date_year "$(today)")"
  165. local month="$(date_month "$(today)")"
  166. local day="$(date_day "$(today)")"
  167. sed 's%^\*-%'"$year"'-%g' \
  168. | sed 's%-\*-%-'"$month"'-%g' \
  169. | sed 's%-\*%-'"$day"'%g'
  170. }
  171. # Filter out comments and blank lines from piped todo
  172. function ignore_todo_blanks {
  173. grep -v "^#" \
  174. | grep -v "^$"
  175. }
  176. # Return all todo lines of the given date
  177. function date_todo_lines {
  178. local date="$1"
  179. grep "$date"
  180. }
  181. # Print all todo lines during the giving days following the start-date
  182. function upcoming_todo_lines {
  183. local todo_file="$1"
  184. local start_date="$2"
  185. local limit="$3"
  186. if test "$limit" -eq 0; then limit="$(inc "$limit")"; fi
  187. local i="0"
  188. while test "$i" -lt "$limit"; do
  189. cat "$todo_file" \
  190. | preprocess_todo \
  191. | date_todo_lines "$(add_days "$start_date" "$i")"
  192. if test "$?" -eq 0 \
  193. -a "$i" -ne "$(dec "$limit")" \
  194. -a "$QUIET_MODE" -ne 0
  195. then echo "---"; fi
  196. i="$(inc "$i")"
  197. done
  198. }
  199. # Print a user-friendly report of upcoming events <3
  200. function generate_report {
  201. local todo_file="$1"
  202. local limit="$2"
  203. local tomorrow="$(add_days "$(today)" 1)"
  204. local today_lines="$(upcoming_todo_lines "$todo_file" "$(today)" 0)"
  205. local later_lines="$(upcoming_todo_lines "$todo_file" "$tomorrow" "$limit")"
  206. if test -n "$today_lines"; then
  207. echo "$TODAY_MSG"
  208. echo "$DIVIDER"
  209. echo "$today_lines"
  210. if test -n "$later_lines"; then echo ''; fi
  211. fi
  212. if test -n "$later_lines"; then
  213. echo "$LATER_MSG"
  214. echo "$DIVIDER"
  215. echo "$later_lines"
  216. fi
  217. }
  218. # --------------------------------------
  219. # INVOCATION
  220. BIN="$(echo "$0" | sed 's%.*/%%')"
  221. function print_help {
  222. echo "usage: $BIN [-hq] [-l \$LIMIT] [-L | -D \$MSG] [\$TODO_PATH]"
  223. exit 2
  224. }
  225. # ------------------
  226. # OPTIONS
  227. DIVIDER="----------------------------------------"
  228. TODO_FILE="$HOME/.todo"
  229. LIMIT=7
  230. TODAY_MSG="TODAY"
  231. LATER_MSG="NEXT EPISODE..."
  232. PAST_MSG="FROM THE GRAVE"
  233. QUIET_MODE=1
  234. while getopts 'l:D:T:L:qh' c; do
  235. case "$c" in
  236. l) LIMIT="$OPTARG" ;;
  237. D) DIVIDER="$OPTARG" ;;
  238. T) TODAY_MSG="$OPTARG" ;;
  239. L) LATER_MSG="$OPTARG" ;;
  240. q) QUIET_MODE=0 ;;
  241. h) print_help ;;
  242. esac
  243. done
  244. shift "$(dec "$OPTIND" 1)"
  245. FREE_ARG="$1"
  246. if test -n "$FREE_ARG"; then
  247. TODO_FILE="$FREE_ARG"
  248. fi
  249. # ------------------
  250. # PROGRAM TYME
  251. if test ! -e "$TODO_FILE"; then
  252. echo "$TODO_FILE: No such file exists"
  253. exit 3
  254. fi
  255. if test $QUIET_MODE -eq 1; then
  256. generate_report "$TODO_FILE" "$LIMIT"
  257. else
  258. upcoming_todo_lines "$TODO_FILE" "$(today)" "$LIMIT"
  259. fi