~psic4t/nntd

ref: 626e995cbc7d546f3cb92f332505686caafd8757 nntd/nntd -rwxr-xr-x 4.7 KiB
626e995cpsic4t README instpirations 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
# nntd - Simple todo  app for Nextcloud Notes
# CC by-nc-nd psic4t 2019

# check jq
[ $(command -v jq) ] || { echo "jq not found. exiting..." && exit 1; }

# check config
if [ -r ~/.config/$(basename "$0").conf ]; then
	. ~/.config/$(basename "$0").conf
else 
	echo -e "Config not found.\nGenerating a new TODO note and ~/.config/$(basename "$0").conf."
	#echo -e "Config not found.\nGenerating an empty one - please edit ~/.config/$(basename "$0").conf."
        CONFIG_SKEL="LOGIN=username\nPASSWORD=secret\nHOSTNAME=myhost.tld\nAPPPATH=\"/apps/notes/api/v0.2/notes\""
        echo -e "$CONFIG_SKEL" > ~/.config/$(basename "$0").conf
	STRING="{\"content\": \"# TODO\n\"}"
	RESULT=$(curl -s -H "Content-Type: application/json" -XPOST -d "$STRING" "https://$LOGIN:$PASSWORD@$HOSTNAME$APPPATH")
	if [ "$(echo $RESULT | jq '.error')" = false ]; then
		echo -e "\nNew TODO note created."
		echo "TODO_NOTEID=$(echo $RESULT | jq '.id')" >> ~/.config/$(basename "$0").conf
	else
		echo "An error occurred."
	fi
	exit 1
fi

# color map
BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BROWN='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHT_GREY='\033[0;37m'
DARK_GREY='\033[1;30m'
LIGHT_RED='\033[1;31m'
LIGHT_GREEN='\033[1;32m'
YELLOW='\033[1;33m'
LIGHT_BLUE='\033[1;34m'
LIGHT_PURPLE='\033[1;35m'
LIGHT_CYAN='\033[1;36m'
WHITE='\033[1;37m'
DEFAULT='\033[0m'

todo_get_file() {
	TODO_DATA=$(curl --connect-timeout 5 -s "https://$LOGIN:$PASSWORD@$HOSTNAME$APPPATH/$TODO_NOTEID" | jq -r '.content')
	readarray -t TODO_ARRAY <<< $(echo "$TODO_DATA")
}

todo_sort() {
	# set LC_ALL on C so sort works :P
	readarray -td '' TODO_ARRAY < <(printf '%s\0' "${TODO_ARRAY[@]}" | LC_ALL=C sort -fz)
}


todo_colorize() {
	case "$1" in
		\(A\)*)
			echo -e "$LIGHT_RED$taskid $i"
			;;
		\(B\)*)
			echo -e "$YELLOW$taskid $i"
			;;
		\(C\)*)
			echo -e "$LIGHT_BLUE$taskid $i"
			;;
		\(D\)*)
			echo -e "$LIGHT_PURPLE$taskid $i"
			;;
		*)
			echo -e "$DEFAULT$taskid $i"
	esac
}

todo_show() {
	# remove note name
	TODO_ARRAY=("${TODO_ARRAY[@]:1}")

	taskid=0
	for i in "${TODO_ARRAY[@]}"; do
		case "$1" in
			active)
				if ! [[ $i =~ ^x\ * ]]; then
					((taskid++))
					todo_colorize $i
				fi
				;;
			pri)
				if [[ $i =~ ^\(${2^^}\)* ]]; then
					((taskid++))
					todo_colorize $i
				fi
				;;
			done)
				if [[ $i =~ ^x\ * ]]; then
					((taskid++))
					todo_colorize $i
				fi
				;;
		esac
	done

	echo -e "$DEFAULT--\n$taskid out of ${#TODO_ARRAY[@]} tasks"
}

todo_write() {
	TODO_ARRAY=("${TODO_ARRAY[@]:1}")
	todo_sort

	# Not easier? TODO
	for i in "${TODO_ARRAY[@]}"; do
		MERGE="$MERGE$i\n"
	done
	MERGE=${MERGE%??} # remove trailing newline
	# Put Header before uploading
	STRING="{\"content\": \"# TODO\n"$MERGE"\"}"
	#STRING="{\"content\": \""$MERGE"\"}"
	RESULT=$(curl -s -H "Content-Type: application/json" -XPUT -d "$STRING" "https://$LOGIN:$PASSWORD@$HOSTNAME$APPPATH/$TODO_NOTEID")
	#echo -e "$RESULT"
	#todo_show
	echo "TODO updated"
}

todo_add() {
	TODO_ARRAY+=("$1")
	todo_write
}

todo_pri() {
	TODO_ARRAY[$1]="(${2^^}) $(echo ${TODO_ARRAY[$1]} | sed 's/^(.*)\ //g')"
	todo_write
}

todo_depri() {
	TODO_ARRAY[$1]=$(echo ${TODO_ARRAY[$1]} | sed 's/^(.*)\ //g')
	todo_write
}

todo_do() {
	TODO_ARRAY[$1]="x $(echo ${TODO_ARRAY[$1]} | sed 's/^x\ //g')"
	todo_write
}

todo_del() {
	read -p "Delete TODO \"${TODO_ARRAY[$1]}\"? " -n 1 -r
	echo
        if [[ $REPLY =~ ^[Yyj]$ ]]; then
		unset 'TODO_ARRAY[$1]'
		todo_write
	fi
}

todo_purge() {
	read -p "Purge all done TODOs? " -n 1 -r
	echo
        if [[ $REPLY =~ ^[Yyj]$ ]]; then
		for i in "${!TODO_ARRAY[@]}"; do
			#[[ ${TODO_ARRAY[i]} =~ ^x\ * ]] && echo "${TODO_ARRAY[$i]}"
			[[ ${TODO_ARRAY[i]} =~ ^x\ * ]] && unset "TODO_ARRAY[$i]"
		done
		todo_write
	fi
}


case "$1" in
	a|add)
		todo_get_file
		todo_add "$2"
		;;
	del)
		todo_get_file
		todo_del "$2"
		;;
	do)
		todo_get_file
		todo_do "$2"
		;;
	p|pri)
		todo_get_file
		todo_pri "$2" "$3"
		;;
	dp|depri)
		todo_get_file
		todo_depri "$2"
		;;
	lsp|listpri)
		todo_get_file
		todo_show pri "$2"
		;;
	lsd|listdone)
		todo_get_file
		todo_show done
		;;
	purge)
		todo_get_file
		todo_purge
		;;
	"")
		todo_get_file
		todo_show active 
		;;
	help|-help|-h)
		echo -e "Usage:\n
$(basename $0) a|add \"new todo\"\t\t- Creates a new TODO.
$(basename $0) \t\t\t\t- Shows all active TODOs.
$(basename $0) p|pri <number> <a-z>\t- Prioritizes the given TODO A-Z. $(basename $0) pri 4 a prioritizes TODO 4 with priority A.
$(basename $0) depri <number>\t\t- Removes priority from TODO.
$(basename $0) lsp|listpri <priority>\t- Lists all TODOs with a given priority.
$(basename $0) do <number>\t\t- Marks TODO as done.
$(basename $0) lsd|listdone\t\t- Lists all done TODOs.
$(basename $0) purge\t\t\t- Purges all done TODOs.
$(basename $0) del <number>\t\t- Deletes a TODO. 
"
		;;
esac