task_funcs.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/bin/bash
  2. source libs/color_vars.sh
  3. declare -a desc_files
  4. declare -a todays_tasks
  5. function load_tasks
  6. {
  7. if [[ -n $(ls Tasks/) ]]; then
  8. desc_files=($(ls Tasks/*.description*))
  9. fi
  10. }
  11. function load_todays_tasks
  12. {
  13. if [[ -n $(ls Today/) ]]; then
  14. todays_tasks=($(ls Today/*.description*))
  15. fi
  16. }
  17. # Test if filename end in .done
  18. # Args: $1 = taskfile
  19. function is_done
  20. {
  21. if [[ "${1##*.}" == "done" ]]; then
  22. return 0
  23. else
  24. return 1
  25. fi
  26. }
  27. # Print task title
  28. # Args: $1 = taskfile
  29. function print_task_title
  30. {
  31. color_task $1
  32. head -n 1 "$1"
  33. echo -ne "$RESET_COLOR"
  34. }
  35. # Set colors on tasks that are done and active.
  36. # Args: $1 = filename
  37. function color_task
  38. {
  39. local ret
  40. if is_done $1; then
  41. echo -ne "$FG_GREEN$FG_BOLD"
  42. else
  43. echo -ne "$FG_CYAN"
  44. fi
  45. }
  46. # echo the filename of the logfile for a taskfile
  47. # Args: $1 = taskfile
  48. function get_logfile_name
  49. {
  50. if is_done $1; then
  51. echo -ne "${1%.description.done}.log"
  52. else
  53. echo -ne "${1%.description}.log"
  54. fi
  55. }
  56. # echo logfile date
  57. # Args: $1 = taskfile
  58. function print_logfile_date
  59. {
  60. stat -c %y "$(get_logfile_name $1)" | sed -n -e 's/\(.*\)\..*/\1/p'
  61. }
  62. function show_tasks
  63. {
  64. if [[ -v $desc_files ]]; then
  65. echo "No tasks."
  66. return 1
  67. fi
  68. local n_elements=${#desc_files[@]}
  69. for ((i=0; i < n_elements; i++)); do
  70. echo -ne "${FG_BOLD}[$i] "
  71. print_task_title ${desc_files[$i]};
  72. echo -ne "\t${FG_BOLD}Log Change: ${RESET_COLOR}"
  73. print_logfile_date ${desc_files[$i]}
  74. done
  75. return 0
  76. }
  77. function todays_tasks_show
  78. {
  79. if [[ -v $todays_tasks ]]; then
  80. echo "No tasks."
  81. return 1
  82. fi
  83. local n_elements=${#todays_tasks[@]}
  84. for ((i=0; i < n_elements; i++)); do
  85. echo -ne "${FG_BOLD}[$i] "
  86. print_task_title ${todays_tasks[$i]}
  87. echo -ne "\t${FG_BOLD}Log Change: ${RESET_COLOR}"
  88. local tasklog_file="Tasks/${todays_tasks[$i]##*/}"
  89. print_logfile_date $tasklog_file
  90. done
  91. return 0
  92. }
  93. function open_log
  94. {
  95. if [[ $1 == "-tasks" ]]; then
  96. load_tasks
  97. if(($2 < ${#desc_files[@]})); then
  98. nano "${desc_files[$2]%.description}.log"
  99. return 0
  100. fi
  101. else
  102. load_todays_tasks
  103. if(($1 < ${#todays_tasks[@]})); then
  104. local tasklog_file="${todays_tasks[$1]##*/}"
  105. nano "Tasks/${tasklog_file%.description}.log"
  106. return 0
  107. fi
  108. fi
  109. return 1
  110. }
  111. function show_description
  112. {
  113. if [[ $1 == "-today" ]]; then
  114. load_todays_tasks
  115. if(($2 < ${#todays_tasks[@]})); then
  116. echo -n "Log updated: "
  117. local tasklog_file="Tasks/${todays_tasks[$i]##*/}"
  118. print_logfile_date $tasklog_file
  119. echo ""
  120. tail -n +3 ${todays_tasks[$2]}
  121. return 0
  122. fi
  123. else
  124. load_tasks
  125. if(($1 < ${#desc_files[@]})); then
  126. echo -n "Log updated: "
  127. print_logfile_date ${desc_files[$1]}
  128. echo ""
  129. tail -n +3 ${desc_files[$1]}
  130. return 0
  131. fi
  132. fi
  133. return 1
  134. }
  135. # Works on today tasks
  136. # Input task number
  137. function task_done
  138. {
  139. task_file=${todays_tasks[$1]##*/}
  140. unlink Today/$task_file
  141. mv Tasks/$task_file Tasks/$task_file.done
  142. ln -r -s Tasks/$task_file.done Today/
  143. }
  144. function task_undone
  145. {
  146. task_file=${todays_tasks[$1]##*/}
  147. unlink Today/$task_file
  148. mv Tasks/$task_file Tasks/${task_file%.done}
  149. ln -r -s Tasks/${task_file%.done} Today/
  150. }
  151. function get_new_bottom_number
  152. {
  153. filenum=${#desc_files[@]}
  154. while((4 > ${#filenum})); do
  155. filenum="0${filenum}";
  156. done
  157. echo $filenum
  158. }
  159. # Low Priority
  160. # Add a new task through script instead of manual file creation
  161. # If we delete a file. The leading numbers and order will be fucked.
  162. function new_task
  163. {
  164. echo "New Task Title (only spaces, dots, alphabetical and numbers): "
  165. read task_title
  166. echo "New Task Description (end input with Ctrl+D): "
  167. task_description=$(cat)
  168. local num
  169. if [[ $desc_files != 0 ]]; then
  170. num=$(get_new_bottom_number)
  171. if((4 < ${#num})); then
  172. echo "Error: Too many tasks?"
  173. exit
  174. fi
  175. else
  176. num="0000"
  177. fi
  178. local filename=$(echo -ne "$task_title" | sed 's/ /_/g')
  179. filename=${filename,,}
  180. echo -e "$task_title" >> "Tasks/${num}-${filename}.description"
  181. echo -e "\n$task_description" >> "Tasks/${num}-${filename}.description"
  182. touch "Tasks/${num}-${filename}.log"
  183. }
  184. # High Priority
  185. # Makes the appropriate symlinking to the Today folder
  186. function take_task
  187. {
  188. ln -r -s "${desc_files[$1]}" Today/
  189. }