summaryrefslogtreecommitdiff
path: root/scripts/sh/wv
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/sh/wv')
-rwxr-xr-xscripts/sh/wv100
1 files changed, 100 insertions, 0 deletions
diff --git a/scripts/sh/wv b/scripts/sh/wv
new file mode 100755
index 0000000..83bda1d
--- /dev/null
+++ b/scripts/sh/wv
@@ -0,0 +1,100 @@
+#!/bin/sh
+# wv: write a word-vomit.
+# TODO: add -w flag for setting website.
+
+usage() {
+ echo "wv [-h | ideas | ls | stats | per-day | per-month | NUMBER]"
+}
+
+stats() {
+ total_ent="$(exec ls "$WWW_DEFAULT_PATH/src/wv" | sed 's/\([0-9]\+\).*/\1/g' | sort -n | tail -1)"
+ total_wc="$(wc -w $WWW_DEFAULT_PATH/src/wv/* | tail -n 1 | awk '{ print $1 }')"
+ avg_wc="$(($total_wc / $(echo $total_ent | sed 's/^0*//') ))"
+
+ echo "Total entries: $total_ent"
+ echo "Total wordcount: $total_wc"
+ echo "Avg wordcount: $avg_wc"
+}
+
+stats_html() {
+ total_ent="$(exec ls "$WWW_DEFAULT_PATH/src/wv" | sed 's/\([0-9]\+\).*/\1/g' | sort -n | tail -1)"
+ total_wc="$(wc -w $WWW_DEFAULT_PATH/src/wv/* | tail -n 1 | awk '{ print $1 }')"
+ avg_wc="$(($total_wc / $(echo $total_ent | sed 's/^0*//') ))"
+
+ printf "<ul>\n<li>Total entries: $total_ent</li>\n<li>Total word count: $total_wc</li>\n<li>Average word count: $avg_wc</li>\n</ul>"
+
+}
+
+list() {
+ out="file date wc title\n"
+ for f in $(find $WWW_DEFAULT_PATH/src/wv -type f | sort -k 3) ; do
+ out="$out$(basename "$f") $(sed -n 2p $f) $(wc -w "$f" | awk '{print $1}') $(sed -n 1p $f)\n"
+ done
+
+ printf "$out"
+}
+
+list_html() {
+ out="<table>\n<tr><th>Index</th>\t<th>Date</th>\t<th>Word count</th>\t<th>Title</th></tr>\n"
+ for f in $(find $WWW_DEFAULT_PATH/src/wv -type f | sort -rk 3) ; do
+ out="$out\t<tr><td>$(basename "$f")</td> <td>$(sed -n 2p $f)</td> <td>$(wc -w "$f" | awk '{print $1}')</td> <td><a href=\"/wv/$(basename "$f")\">$(sed -n 1p $f)</a></td></tr>\n"
+ done
+ out="$out\n</table>"
+ printf "$out"
+}
+
+
+perday() {
+ list | awk '{print $2}' | grep -v 'date' | sort | uniq -c | sort -n
+}
+
+permonth() {
+ for f in $(find $WWW_DEFAULT_PATH/src/wv -type f | sort -k 3) ; do
+ out="$out$(sed -n 2p $f | cut -c1-7)\n"
+ done
+
+ printf "$(printf "$out" | uniq -c)\n"
+}
+
+current() {
+ total_ent="$(exec ls "$WWW_DEFAULT_PATH/src/wv" | sed 's/\([0-9]\+\).*/\1/g' | sort -n | tail -1)"
+ printf "$total_ent\n" | sed 's/^0*//'
+}
+
+latest_entry() {
+ latest_ent="$(exec ls "$WWW_DEFAULT_PATH/src/wv" | sed 's/\([0-9]\+\).*/\1/g' | sort -n | tail -1)"
+ echo "$(sed -n 2p "$WWW_DEFAULT_PATH/src/wv/$latest_ent")"
+}
+
+first_entry() {
+ first_ent="$(exec ls "$WWW_DEFAULT_PATH/src/wv" | sed 's/\([0-9]\+\).*/\1/g' | sort -n | head -n1)"
+ echo "$(sed -n 2p "$WWW_DEFAULT_PATH/src/wv/$first_ent")"
+}
+
+[ -z "$WWW_DEFAULT_PATH" ] && echo "WWW_DEFAULT_PATH not set" && exit 1
+[ "$1" = "-h" ] && usage && exit 0
+[ "$1" = "help" ] && usage && exit 0
+[ "$1" = "ls" ] && list && exit 0
+[ "$1" = "ls-html" ] && list_html && exit 0
+[ "$1" = "ideas" ] && spr "wv-ideas" && exit 0
+[ "$1" = "stats" ] && stats && exit 0
+[ "$1" = "stats-html" ] && stats_html && exit 0
+[ "$1" = "per-day" ] && perday && exit 0
+[ "$1" = "per-month" ] && permonth && exit 0
+[ "$1" = "latest-entry" ] && latest_entry && exit 0
+[ "$1" = "first-entry" ] && first_entry && exit 0
+[ "$1" = "current" ] && current && exit 0
+[ ! -z "$1" ] && nvim "$WWW_DEFAULT_PATH/src/wv/$(printf "%04d" "$1")" && exit 0
+
+number="na"
+for i in $(seq -w 1 1000) ; do
+ if [ ! -e "$WWW_DEFAULT_PATH/src/wv/$i" ] ; then
+ number="$i"
+ break
+ fi
+done
+
+[ "$number" = "na" ] && echo "exhausted range" && exit 1
+
+nvim "$WWW_DEFAULT_PATH/src/wv/$number"
+