Skip to content

Commit

Permalink
added and executed shfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWillner committed Dec 28, 2017
1 parent ac630d0 commit 87a20b9
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 89 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
help:
@echo "Nothing really to make, but there are some available commands:"
@echo " * test : run some tests"
@echo " * style : style bash script"
@echo " * feedback : create a GitHub issue"

feedback:
Expand All @@ -13,6 +14,10 @@ test: check
@bashcov shunit2 test/thingsTest.sh
@cat coverage/index.html||true

style:
@type shfmt >/dev/null 2>&1 || (echo "Run 'go get -u mvdan.cc/sh/cmd/shfmt' first." >&2 ; exit 1)
@shfmt -i 2 -w things.sh

check:
@type shellcheck >/dev/null 2>&1 || (echo "Run 'brew install shellcheck' first." >&2 ; exit 1)
@type shunit2 >/dev/null 2>&1 || (echo "Run 'brew install shunit2' first." >&2 ; exit 1)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ Days/Task : 39.0
* https://dev.to/thiht/shell-scripts-matter
* https://google.github.io/styleguide/shell.xml
* https://kvz.io/blog/2013/11/21/bash-best-practices/
* https://github.com/progrium/bashstyle
* https://github.com/progrium/bashstyle
* https://github.com/mvdan/sh/
62 changes: 40 additions & 22 deletions test/thingsTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,75 @@ testWaiting() {

testStat() {
command="stat"
output=$(eval "$CLI" "$command"|sed -e 's/[ ]//g')
output=$(eval "$CLI" "$command" | sed -e 's/[ ]//g')

expected="Inbox:1"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Today:1"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Upcoming:3"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Next:6"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Someday:4"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Completed:1"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Cancelled:1"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Trashed:3"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Tasks:11"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Subtasks:2"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Waiting:1"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Projects:4"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Repeating:2"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Nextish:6"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Headings:2"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Oldest:2017-12-27|TodayProject|TodayTodo"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
expected="Farest:2045-05-13|(NoContext)|NextTodo"
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
}

Expand All @@ -96,9 +113,10 @@ testDue() {
}

runSimpleTest() {
command="$1"
expected="$2"
command="$1"
expected="$2"
output=$(eval "$CLI" "$command")
(echo "$output"|grep "$expected" > /dev/null 2>&1); result=$?
(echo "$output" | grep "$expected" >/dev/null 2>&1)
result=$?
assertTrue "Command '$command' should contain '$expected' in '\n$output'" $result
}
}
159 changes: 93 additions & 66 deletions things.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
#
#
# DESCRIPTION
#
# Simple read-only comand-line interface to your Things 3 database. Since
Expand Down Expand Up @@ -292,7 +292,6 @@ LIMIT $limitBy;
SQL
}


old() {
sqlite3 "$THINGSDB" <<-SQL
SELECT
Expand All @@ -314,7 +313,6 @@ LIMIT $limitBy;
SQL
}


due() {
sqlite3 "$THINGSDB" <<-SQL
SELECT
Expand Down Expand Up @@ -439,9 +437,9 @@ SQL
}

csv() {
echo 'Title;"Creation Date";"Modification Date";"Due Date";"Start Date";"Completion Date";Project;Area;Subtask;Notes'
echo 'Title;"Creation Date";"Modification Date";"Due Date";"Start Date";"Completion Date";Project;Area;Subtask;Notes'

sqlite3 "$THINGSDB" <<-SQL
sqlite3 "$THINGSDB" <<-SQL
.mode csv
.separator ";"
SELECT
Expand All @@ -461,7 +459,7 @@ LEFT OUTER JOIN $AREATABLE T3 ON T1.area = T3.uuid
WHERE T1.$ISNOTTRASHED AND (T1.$ISOPEN OR T1.status = 3) AND T1.$ISTASK;
SQL

sqlite3 "$THINGSDB" <<-SQL
sqlite3 "$THINGSDB" <<-SQL
.mode csv
.separator ";"
SELECT
Expand All @@ -481,30 +479,47 @@ WHERE (T2.$ISOPEN OR T2.status = 3) AND T2.$ISNOTTRASHED;
SQL
}


stat() {
echo -n "Inbox :"; inbox|wc -l
echo -n "Inbox :"
inbox | wc -l
echo ""
echo -n "Today :"; today|wc -l
echo -n "Upcoming :"; upcoming|wc -l
echo -n "Next :"; next|wc -l
echo -n "Someday :"; someday|wc -l
echo ""
echo -n "Completed :"; completed|wc -l
echo -n "Cancelled :"; cancelled|wc -l
echo -n "Trashed :"; trashed|wc -l
echo ""
echo -n "Tasks :"; all|wc -l
echo -n "Subtasks :"; subtasks|wc -l
echo -n "Waiting :"; waiting|wc -l
echo -n "Projects :"; projects|wc -l
echo -n "Repeating :"; repeating|wc -l
echo -n "Nextish :"; nextish|wc -l
echo -n "Headings :"; headings|wc -l
echo ""
echo -n "Oldest : "; limitBy="1" old
echo -n "Farest : "; orderBy="startDate DESC" upcoming|tail -n1
echo -n "Days/Task : "; averageCompleteTime
echo -n "Today :"
today | wc -l
echo -n "Upcoming :"
upcoming | wc -l
echo -n "Next :"
next | wc -l
echo -n "Someday :"
someday | wc -l
echo ""
echo -n "Completed :"
completed | wc -l
echo -n "Cancelled :"
cancelled | wc -l
echo -n "Trashed :"
trashed | wc -l
echo ""
echo -n "Tasks :"
all | wc -l
echo -n "Subtasks :"
subtasks | wc -l
echo -n "Waiting :"
waiting | wc -l
echo -n "Projects :"
projects | wc -l
echo -n "Repeating :"
repeating | wc -l
echo -n "Nextish :"
nextish | wc -l
echo -n "Headings :"
headings | wc -l
echo ""
echo -n "Oldest : "
limitBy="1" old
echo -n "Farest : "
orderBy="startDate DESC" upcoming | tail -n1
echo -n "Days/Task : "
averageCompleteTime
}

search() {
Expand All @@ -527,7 +542,7 @@ WHERE T1.$ISNOTTRASHED AND T1.$ISTASK
AND (T1.title LIKE "%$string%" OR T2.title LIKE "%$string%");
SQL

sqlite3 "$THINGSDB" <<-SQL
sqlite3 "$THINGSDB" <<-SQL
.mode line
SELECT
T2.title as "Title",
Expand All @@ -543,17 +558,17 @@ SQL
}

require_sqlite3() {
command -v sqlite3 > /dev/null 2>&1 || {
command -v sqlite3 >/dev/null 2>&1 || {
echo >&2 "ERROR: SQLite3 is required but could not be found."
exit 1
}
}

require_db() {
test -r "$THINGSDB" -a -f "$THINGSDB" || {
echo >&2 "ERROR: Things database not found at '$THINGSDB'."
echo >&2 "HINT: You might need to install Things from https://culturedcode.com/things/"
exit 2
echo >&2 "ERROR: Things database not found at '$THINGSDB'."
echo >&2 "HINT: You might need to install Things from https://culturedcode.com/things/"
exit 2
}
}

Expand All @@ -564,52 +579,64 @@ main() {
while [[ $# -gt 1 ]]; do
local key="$1"
case $key in
-l|--limitBy) limitBy="$2";shift;;
-w|--waitingTag) waitingTag="$2";shift;;
-o|--orderBy) orderBy="$2";shift;;
-s|--string) string="$2";shift;;
*) ;;
-l | --limitBy)
limitBy="$2"
shift
;;
-w | --waitingTag)
waitingTag="$2"
shift
;;
-o | --orderBy)
orderBy="$2"
shift
;;
-s | --string)
string="$2"
shift
;;
*) ;;
esac
shift
done

local command=${1:-}

if [[ -n $command ]]; then
case $1 in
inbox) inbox;;
today) today;;
upcoming) upcoming;;
next) next;;
anytime) anytime;;
someday) someday;;
all) all;;
nextish) nextish;;
completed) completed;;
old) old;;
due) due;;
repeating) repeating;;
subtasks) subtasks;;
projects) projects;;
headings) headings;;
cancelled) cancelled;;
trashed) trashed;;
waiting) waiting;;
notes) notes;;
csv) csv|awk '{gsub("<[^>]*>", "")}1'|iconv -c -f UTF-8 -t WINDOWS-1252//TRANSLIT;;
stat) limitBy="999999" stat;;
search) search;;
feedback) open https://github.com/AlexanderWillner/things.sh/issues/;;
*) usage;;
inbox) inbox ;;
today) today ;;
upcoming) upcoming ;;
next) next ;;
anytime) anytime ;;
someday) someday ;;
all) all ;;
nextish) nextish ;;
completed) completed ;;
old) old ;;
due) due ;;
repeating) repeating ;;
subtasks) subtasks ;;
projects) projects ;;
headings) headings ;;
cancelled) cancelled ;;
trashed) trashed ;;
waiting) waiting ;;
notes) notes ;;
csv) csv | awk '{gsub("<[^>]*>", "")}1' | iconv -c -f UTF-8 -t WINDOWS-1252//TRANSLIT ;;
stat) limitBy="999999" stat ;;
search) search ;;
feedback) open https://github.com/AlexanderWillner/things.sh/issues/ ;;
*) usage ;;
esac
else
usage;
usage
fi
}

cleanup() {
: # nothing to clean up
# echo "$(date) $(hostname) $0: EXIT on line $2 (exit status $1)"
: # nothing to clean up
# echo "$(date) $(hostname) $0: EXIT on line $2 (exit status $1)"
}

[[ "${BASH_SOURCE[0]}" == "${0}" ]] && trap 'cleanup $? $LINENO' EXIT && main "$@"

0 comments on commit 87a20b9

Please sign in to comment.