-
Notifications
You must be signed in to change notification settings - Fork 4
/
hamster-dmenu
executable file
·80 lines (71 loc) · 2.07 KB
/
hamster-dmenu
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
#!/bin/sh
#
# Script to start, stop & continue hamster time tracker activities from dmenu.
#
# - To cancel, type escape
# - If you enter a single minus sign ("-") at the prompt, time tracking is stopped
# - If you enter a single forward slash ("/") at the prompt, hamster-time-tracker is launched
# - Anything else will start a new task. Format is the hamster format: `activity`@`category`
# - Completion list shows all entries, with the entries most recently entered via
# hamster-dmenu first. So pressing 'enter' will continue the last activity.
#
# Requirements: hamster-cli, dmenu, notify-send, sed and awk
# The script can be invoked with any dmenu settings (eg. -i for case insensitivity)
#
# Paths to utilities
hamster='/usr/bin/hamster-time-tracker'
hamstercli='/usr/bin/hamster-cli'
dmenu='/usr/bin/dmenu'
notify='/usr/bin/notify-send hamster'
notifycrit='/usr/bin/notify-send -u critical hamster'
# Shell shorthands
echo_with_nl='printf %b'
make_unique='awk !a[$0]++'
remove_empty_lines='sed /^\s*$/d'
# Caching
cachedir=${XDG_CACHE_HOME:-'$HOME/.cache'}
cache_content=''
if [ -d "${cachedir}" ]; then
cache=${cachedir}/hamster-dmenu
else
cache=${HOME}/.hamster-dmenu-cache
fi
# Build list of entries
if [ -f "${cache}" ]
then
cache_content=`cat ${cache}`
entries=${cache_content}\\n`${hamstercli} list-activities`
# Ensure each entry is unique and remove blank lines
entries=`${echo_with_nl} "${entries}" | ${make_unique} | ${remove_empty_lines}`
else
entries=`${hamstercli} list-activities`
fi
# Get prompt
activity=`${echo_with_nl} "${entries}" | ${dmenu} "$@"`
# Do nothing on escape
if [ $? -eq 1 ]
then
exit 0
fi
# Stop on single -
if [ "${activity}" = "-" ]
then
${hamstercli} stop
exit 0
fi
# Run hamster on single '/'
if [ "${activity}" = "/" ]
then
exec ${hamster}
exit 0
fi
# Otherwise start a new activity
$hamstercli start "$activity"
if [ $? -eq 0 ]
then
# Update the mru cache
${echo_with_nl} "${activity}\n${cache_content}" | ${make_unique} > ${cache}
else
$notifycrit "Hamster failed to start task"
exit 1
fi