bug.sh (5714B)
1 #!/bin/sh 2 # (encoding: UTF-8) 3 # 4 # bug-fork, regexghost's fork/re-write of bug by Lluis Batlle i Rossell 5 # Simple ToDo terminal cli tool 6 # LICENSE: GPL 2 (see LICENSE file) 7 8 BOLD="\033[1m" 9 RED="\033[31m" 10 GREEN="\033[32m" 11 YELLOW="\033[33m" 12 BLUE="\033[34m" 13 MAGENTA="\033[35m" 14 CYAN="\033[36m" 15 RESET_COLOUR="\033[0m" 16 17 BUG_PROJECT_DEL="$(dirname "$BUG_PROJECT")/.$(basename "$BUG_PROJECT")-del" 18 19 [ -f /tmp/bug_todo_temp ] && rm /tmp/bug_todo_temp 20 21 get_next_id () { 22 echo $(($(head -n 1 "$BUG_PROJECT")+1)) 23 } 24 25 unNL () { 26 sed 's/ /\\t/g; s/$/\\/g' | tr "\\n" n 27 } 28 29 reNL () { 30 sed 's/\\t/ /g; s/\\n/\n/g' 31 } 32 33 priorNumToName () { 34 sed 's/ 1 / URGENT /g; s/ 2 / High /g; s/ 3 / Medium /g; s/ 4 / Low /g; s/ 5 / Anytime /g' 35 } 36 37 priorNameToNum () { 38 sed 's/ URGENT / 1 /g; s/ High / 2 /g; s/ Medium / 3 /g; s/ Low / 4 /g; s/ Anytime / 5 /' 39 } 40 41 trim () { 42 sed 's/^ //g; s/ $//g' | sed 's/\(\\n\)*$//g' 43 } 44 45 updateCurID () { 46 sed "s/^[0-9][0-9]*\$/${1}/g" "$BUG_PROJECT" > /tmp/bugproj 47 mv /tmp/bugproj "$BUG_PROJECT" 48 } 49 50 fileToLine () { 51 file="$1" 52 id="$(grep "^ID:" "$file" | head -n 1 | cut -d ":" -f 2- | trim)" 53 prior="$(grep "^Priority:" "$file" | head -n 1 | cut -d ":" -f 2- | trim)" 54 state="$(grep "^State:" "$file" | head -n 1 | cut -d ":" -f 2- | trim)" 55 subj="$(grep "^Subject:" "$file" | head -n 1 | cut -d ":" -f 2- | trim)" 56 desc="$(tail -n +6 "$file" | unNL | trim)" 57 58 printf '%s\n' "${id} ${prior} ${state} ${subj} ${desc}" 59 } 60 61 add () { 62 id=$(get_next_id) 63 64 read -p "Enter Priority (1,2,3.. 1=highest): " priority 65 priority=$(echo "\t${priority}\t" | priorNumToName | sed 's/ //g') # This isn't very elegant 66 read -p "Enter State (NS,IP): " state 67 read -p "Enter Subject: " subject 68 69 touch /tmp/bug_todo_temp 70 echo "ID: ${id}" >> /tmp/bug_todo_temp 71 echo "Priority: ${priority}" >> /tmp/bug_todo_temp 72 echo "State: ${state}" >> /tmp/bug_todo_temp 73 echo "Subject: ${subject}" >> /tmp/bug_todo_temp 74 echo "-- Description Below --" >> /tmp/bug_todo_temp 75 "${VISUAL:-${EDITOR:-vi}}" /tmp/bug_todo_temp 76 77 fileToLine /tmp/bug_todo_temp >> "$BUG_PROJECT" 78 updateCurID "$id" 79 } 80 81 lineToFile () { 82 line="$(awk "/^${1}\t/" "$BUG_PROJECT")" 83 id="$(printf '%s' "$line" | cut -f 1)" 84 prior="$(printf '%s' "$line" | cut -f 2)" 85 state="$(printf '%s' "$line" | cut -f 3)" 86 subject="$(printf '%s' "$line" | cut -f 4)" 87 desc="$(printf '%s' "$line" | cut -f 5)" 88 89 touch /tmp/bug_todo_temp 90 echo "ID: ${id}" >> /tmp/bug_todo_temp 91 echo "Priority: ${prior}" >> /tmp/bug_todo_temp 92 echo "State: ${state}" >> /tmp/bug_todo_temp 93 echo "Subject: ${subject}" >> /tmp/bug_todo_temp 94 echo "-- Description Below --" >> /tmp/bug_todo_temp 95 echo "$desc" | reNL >> /tmp/bug_todo_temp 96 } 97 98 selectEntry () { 99 file="${1:-${BUG_PROJECT}}" 100 result="$(tail -n +2 "$file" | cut -f 1,4 | fzf -i | cut -f 1)" 101 [ "$result" = "" ] && return 1 102 echo "$result" 103 } 104 105 delLine () { 106 id="$1" 107 file="$2" 108 sed "/^${id} .*/d" "$file" > /tmp/bug_temp 109 mv /tmp/bug_temp "$file" 110 } 111 112 edit () { 113 id=$(selectEntry) 114 [ "$?" = "0" ] || exit 115 lineToFile $id 116 "${VISUAL:-${EDITOR:-vi}}" /tmp/bug_todo_temp 117 updatedLine="$(fileToLine /tmp/bug_todo_temp)" 118 delLine "$id" "$BUG_PROJECT" 119 printf '%s\n' "$updatedLine" >> "$BUG_PROJECT" 120 } 121 122 view () { 123 id="$1" 124 [ "$id" = "" ] && { id=$(selectEntry); { [ "$?" = "0" ] || exit; }; } 125 lineToFile $id 126 output="$(cat /tmp/bug_todo_temp | sed "\ 127 s/^ID:/\\${MAGENTA}\\${BOLD}ID:\\${RESET_COLOUR}/g; \ 128 s/^Priority:/\\${MAGENTA}\\${BOLD}Priority:\\${RESET_COLOUR}/g; \ 129 s/^State:/\\${MAGENTA}\\${BOLD}State:\\${RESET_COLOUR}/g; \ 130 s/^Subject:/\\${MAGENTA}\\${BOLD}Subject:\\${RESET_COLOUR}/g; \ 131 s/^-- Description Below --/\\${GREEN}\\${BOLD}-- Description Below --\\${RESET_COLOUR}/g")" 132 echo "$output" 133 } 134 135 list () { 136 output="$(tail -n +2 "$BUG_PROJECT" | cut -f 1-4 | priorNameToNum | sort -r -n -k 2 | priorNumToName | sed "\ 137 s/ URGENT / \\${RED}\\${BOLD}URGENT\\${RESET_COLOUR} /g; \ 138 s/ High / \\${MAGENTA}\\${BOLD}High\\${RESET_COLOUR} /g; \ 139 s/ Medium / \\${CYAN}\\${BOLD}Medium\\${RESET_COLOUR} /g; \ 140 s/ Low / \\${GREEN}\\${BOLD}Low\\${RESET_COLOUR} /g; \ 141 s/ Anytime / \\${BOLD}Anytime\\${RESET_COLOUR} /g; \ 142 s/ NS / \\${BLUE}\\${BOLD}NS\\${RESET_COLOUR} /g; \ 143 s/ IP / \\${YELLOW}\\${BOLD}IP\\${RESET_COLOUR} /g")" 144 echo "$output" 145 } 146 147 delete () { 148 id=$(selectEntry) 149 [ "$?" = "0" ] || exit 150 view "$id" 151 echo "" 152 read -p "Delete? (y/N) " yesOrNo 153 if [ "$yesOrNo" = "y" ] || [ "$yesOrNo" = "Y" ]; then 154 awk "/^${id}\t/" "$BUG_PROJECT" >> "$BUG_PROJECT_DEL" 155 delLine "$id" "$BUG_PROJECT" 156 fi 157 } 158 159 restore () { 160 id=$(selectEntry "$BUG_PROJECT_DEL") 161 [ "$?" = "0" ] || exit 162 awk "/^${id}\t/" "$BUG_PROJECT_DEL" >> "$BUG_PROJECT" 163 delLine "$id" "$BUG_PROJECT_DEL" 164 } 165 166 version () { 167 echo "bug-fork 0.6 - Simple ToDo cli manager" 168 echo "Copyright (C) 2006 Lluis Batlle i Rossell" 169 echo "With modifications bvy regexghost" 170 echo "License: GPL 2" 171 } 172 173 project () { 174 echo "Project file: $BUG_PROJECT" 175 echo "Deleted file: $BUG_PROJECT_DEL" 176 } 177 178 PNAME="$(basename "$0")" 179 180 printHelp () { 181 echo "Usage:" 182 echo " ${PNAME} list - List todos" 183 echo " ${PNAME} view - View todo details" 184 echo " ${PNAME} add - Add new todo" 185 echo " ${PNAME} edit - Edit todo" 186 echo " ${PNAME} delete - Delete todo" 187 echo " ${PNAME} restore - Restore deleted toto" 188 echo " ${PNAME} version - Version information" 189 echo " ${PNAME} create - Create project files" 190 echo " ${PNAME} project - Print project file" 191 } 192 193 CMD="$1" 194 195 case "$CMD" in 196 -h) 197 printHelp 198 exit 1 199 ;; 200 a*) 201 add || exit 1 202 ;; 203 l*) 204 list || exit 1 205 ;; 206 ver*) 207 version || exit 1 208 ;; 209 v*) 210 view || exit 1 211 ;; 212 p*) 213 project 214 ;; 215 del*|rm) 216 delete || exit 1 217 ;; 218 create) 219 create || exit 1 220 ;; 221 e*) 222 edit || exit 1 223 ;; 224 r*) 225 restore || exit 1 226 ;; 227 *) 228 printHelp 229 exit 1 230 ;; 231 esac