summarylogtreecommitdiffstats
path: root/bok
diff options
context:
space:
mode:
Diffstat (limited to 'bok')
-rwxr-xr-xbok108
1 files changed, 108 insertions, 0 deletions
diff --git a/bok b/bok
new file mode 100755
index 000000000000..4d280504e089
--- /dev/null
+++ b/bok
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+edit() {
+ # if -d is the empty string, date will just
+ # get today's date
+ file=$BOK_DIR/$(date -d "$1" '+%Y/%m/%d')
+
+ # confirm that date format is valid
+ [ "$?" -ne "0" ] && exit 1
+
+ # initialize journal file and necessary directories,
+ # being sure not to overwrite an existing file
+ [ ! -f $file ] &&
+ echo $(date -d "$1" '+%a %D') | install -D /dev/stdin $file
+ $EDITOR $file
+}
+
+view() {
+ # get list of all journal files
+ journal=$(find $BOK_DIR |
+ grep -E "[0-9]{4}/[0-9]{2}/[0-9]{2}" |
+ sort)
+
+ # get index of target entry
+ n=$(expr -1 + $( # line numbers start at 1, but index starts at 0
+ grep -n $(date -d "$1" '+%Y/%m/%d') <<< $journal |
+ cut -f1 -d:))
+
+ lesskey $BOK_KEYS # set custom keybindings
+ less +"$n"l $journal
+ echo ' ' | lesskey /dev/stdin # restore default keybindings
+}
+
+search () {
+ grep -rw $BOK_DIR -e "$1" |
+ # This regex looks super complicated, but all it does
+ # is match the date plus everything before it
+ # and replace it with just the date.
+ # tl;dr It removes the full path.
+ sed -E "s/.*([0-9]{4}\/[0-9]{2}\/[0-9]{2}:)/\1 /" |
+ sort
+}
+
+tagsearch () {
+ grep -rnw $BOK_DIR -e "$1" |
+ grep ":2:" |
+ sed -E "s/:[0-9]*:/:/
+ s/.*([0-9]{4}\/[0-9]{2}\/[0-9]{2}:)/\1 /" |
+ sort
+}
+
+sview () {
+ match=$(grep -rnw $BOK_DIR -e "$1" |
+ sed "s/:[0-9]*:.*//")
+ lesskey $BOK_KEYS
+ less $match
+ echo ' ' | lesskey /dev/sdtin
+}
+
+tview () {
+ match=$(grep -rnw $BOK_DIR -e "$1" |
+ grep ":2:" |
+ sed "s/:2:.*//")
+ lesskey $BOK_KEYS
+ less $match
+ echo ' ' | lesskey /dev/sdtin
+}
+
+showhelp () {
+ echo "Usage: bok <command> [arg]"
+ echo ""
+ echo "Arguments:"
+ echo " new Create/edit journal entry for today"
+ echo " edit [date] Create/edit journal entry for specified date"
+ echo " view [date] Open journal entry for specified date in less"
+ echo " search <key> Search the journal for entries containing"
+ echo " the specified keyword"
+ echo " searcht <tag> Search the journal for entries containing"
+ echo " the specified tag, looking only in the second"
+ echo " line of the file"
+ echo " searchv <key> Open the journal as with the view option,"
+ echo " but only show search matches"
+ echo " searchtv <tag> Open the journal as with the view option,"
+ echo " but only show files with the specified tag"
+ echo " Alias: searchvt"
+ echo ""
+ echo "When the view function opens less, it uses the following keybindings:"
+ echo " h/l Previous/next entry"
+ echo " j/k Scroll down/scroll up"
+ echo " q Quit"
+}
+
+[ -z ${BOK_DIR+x} ] && BOK_DIR="$HOME/journal"
+[ -z ${BOK_KEYS+x} ] && BOK_KEYS="$HOME/.bokrc"
+[ ! -f $BOK_KEYS ] && echo ".bokrc file does not exist, creating" &&
+ cp /usr/share/bok/bokrc.default $HOME/.bokrc
+[ -z ${1+x} ] && showhelp && exit 1
+[ $1 = "new" ] && edit "$(date)" && exit 0
+[ $1 = "edit" ] && edit "$2" && exit 0
+[ $1 = "view" ] && view "$2" && exit 0
+[ $1 = "search" ] && search "$2" && exit 0
+[ $1 = "searcht" ] && tagsearch "$2" && exit 0
+[ $1 = "searchv" ] && sview "$2" && exit 0
+[ $1 = "searchtv" ] && tview "$2" && exit 0
+[ $1 = "searchvt" ] && tview "$2" && exit 0
+
+showhelp && exit 1
+