bug-fork

fork/re-write of bug cli todo manager
git clone https://git.regexghost.com/bug-fork.git
Log | Files | Refs | README | LICENSE

bug.sh (5749B)


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