summaryrefslogtreecommitdiff
path: root/note
blob: eae8cf094e97577a73d2ba1875465015ea00ed1c (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
#!/bin/sh

set -e
notesdir="$HOME/.local/share/notes"

print_usage() {
	cat << EOF
usage: note [option]
  -h    show this help message
  -a    append note to the main file
  -d    append using dmenu
  -e    edit the main file
  -s    show last 10 notes
  -n    create a new file
  -l    open file in pager
EOF
}

append_note() {
	echo -n "note: " && read -r note
	printf "[$(date +'%d/%m/%Y, %H:%M')] %s\n\n" "$note" >> $notesdir/notes.txt
}

edit_file() {
	$EDITOR $notesdir/notes.txt
}

show_last() {
	tail -n 20 $notesdir/notes.txt
}

new_file() {
	echo -n "title: " && read -r title
	$EDITOR $notesdir/$title.txt
}

open_pager() {
	find $notesdir -type f -printf "%p\n" |
	fzf -e +s --reverse --with-nth -1 -d '/' --preview "cat {}" \
	--preview-window=right:75%:sharp | xargs -ro less
}

dmenu_note() {
	note="$(echo "\c" | dmenu -c -bw 3 -p 'Note:')"
	printf "[$(date +'%d/%m/%Y, %H:%M')] %s\n\n" "$note" >> $notesdir/notes.txt
}

[ $# -eq 0 ] && print_usage

while getopts "hadesnl" o; do 
  case "${o}" in 
	h) print_usage ;;
	a) append_note ;;
	d) dmenu_note ;;
	e) edit_file ;;
	s) show_last ;;
	n) new_file ;;
	l) open_pager ;;
	*) print_usage ; exit 1 ;;
  esac
done