summarylogtreecommitdiffstats
path: root/bok
blob: 4d280504e089be74ba6b0ead698d9425bb11ef99 (plain)
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
#!/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