~charles/dotfiles

f2b0d150e08f193450b7f61acd36aafa1aee8e04 — Charles Daniels 1 year, 25 days ago 7f8d204
add lstodo
1 files changed, 74 insertions(+), 0 deletions(-)

A overlay/bin/joplin-lstodo
A overlay/bin/joplin-lstodo => overlay/bin/joplin-lstodo +74 -0
@@ 0,0 1,74 @@
#!/bin/sh

set -e
set -u

if [ ! -x "$(which jq)" ] ; then
	echo "no jq in PATH" 1>&2
	exit 1
fi

if [ ! -x "$(which curl)" ] ; then
	echo "no curl in PATH" 1>&2
	exit 1
fi

TOKEN="$(cat "$HOME/.config/joplin-tools/token.txt")"
URL='http://localhost:41184'

if [ -t 1 ] ; then
	GREY='\033[0;37m\e[9m'
	RED='\033[0;31m'
	ORANGE='\033[0;33m'
	NC='\033[0m\e[0m'
else
	GREY=''
	RED=''
	ORANGE=''
	NC=''
fi

curl -s "$URL/notes?token=$TOKEN" | \
	jq -c -r '.[] | select(.is_todo == 1) | [.todo_completed,.title] | @tsv' | \
	( while read -r entry ; do
		complete="$(echo "$entry" | cut -f 1)"
		title="$(echo "$entry" | cut -f2)"
		set +e
		if due="$(echo "$title" | egrep -o '_[0-9]{4}-[0-9]{1,2}-[0-9]{0,2}[ ]+' | sed 's/^_//g')" ; then
			title="$(echo "$title" | sed 's/^_'"$due"'//g')"
		fi
		set -e

		COLOR="NC"
		if [ -z "$due" ] ; then
			due='N/A      '
		elif [ "$(date --date "$due" --iso)" = "$(date --iso)" ] ; then
			COLOR="RED"

		elif [ "$(date --date "$due" +%s)" -lt "$(date --date "7 days" +%s)" ] ; then
			COLOR="ORANGE"
		fi

		if [ "$complete" -gt 0 ] ; then
			due="X        "
			COLOR="GREY"
		fi
		printf "$due\t$title\t$COLOR\n"
	done ) | \
	sort -s | while read -r entry ; do
		COLOR="$(echo "$entry" | cut -f 3)"
		REST="$(echo "$entry" | cut -f 1,2)"
		if [ "$COLOR" = "NC" ] ; then
			COLOR="$NC"
		elif [ "$COLOR" = "GREY" ] ; then
			COLOR="$GREY"
		elif [ "$COLOR" = "RED" ] ; then
			COLOR="$RED"
		elif [ "$COLOR" = "ORANGE" ] ; then
			COLOR="$ORANGE"
		else
			COLOR=""
		fi
		printf "$COLOR$REST$NC\n"
	done