-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclout.zsh
executable file
·107 lines (95 loc) · 5.28 KB
/
clout.zsh
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
#!/bin/zsh
## Clock Out script
# In collaboration with clin.zsh, clnote.zsh, and clp_state.zsh, it maintains/uses/parses these files:
# ~/.clock_log/clin_time --> ASCII one line
# if clocked in: the output of `date +"%F %H:%M:%S"` recording when you clocked in
# if clocked out: OUT
# acts as CLOCK STATUS (am I in or out?)
# ~/.clock_log/YYYY-MM-DD.log --> log of that day's in/outs
# ~/.clock_log/state --> short, human-readable current status
# "IN HH:MM" w/ HH:MM the time clocked in , or
# "OUT"
# ~/.clock_log/clin_notes --> A log of timestamped, one-line notes
# describing actions, notes, ideas that you want to log while clocked in
# It is an error to try to modify this
# while not clocked in.
# This file only ever contains a log for
# the current session. This is then
# written out to a .log file and deleted.
# ~/.clock_log/clout_notes --> A log of timestamped, one-line notes
# describing actions, notes, ideas that occur when
# you're not focusing on a particular project/outcome
# It is an error to try to modify this
# while clocked in (this is for focused or
# potentially billable time).
# This file only ever contains a log for
# the current session. This is then
# written out to a .log file and deleted.
#User has tried to clock out, so check state
# TODO: Add error checks
# file should exist
# file should always have just one line with specifically formatted data
# TODO: make directories variables instead of hard-coded
#Use matching regex (date) sentinel to get state
cur_state=`cat ~/.clock_log/clin_time | awk '$1 ~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/ {$1=$1;print}'` #awk magic ($1=$1 has side-effect of re-evaluating $0 and rebuilding with default separators) trims outer spaces and squeezes internal spaces to 1
#Use matching regex (three capital letter code) sentinel to get context (optional! if blank, ignore)
cur_context=`cat ~/.clock_log/clin_time | awk '$1 ~ /[A-Z]{3}:/ {$1=$1;print}'`
cur_context_code=`cat ~/.clock_log/clin_time | awk '$1 ~ /[A-Z]{3}:/ {$1=$1;printf "%.3s", $1}'`
cur_context_desc=`cat ~/.clock_log/clin_time | awk '$1 ~ /[A-Z]{3}:/ {$1="";print $0}'`
if [[ cur_state == 'OUT' ]]; then
#We're here, so state was clocked out.
echo "You've already clocked out!"
else
#We're here, so state should be clock-in time YYYY-MM-DD HH:MM:SS
#calculate time clocked in (requires dateutils linux package)
cl_start=$cur_state
cl_end=`date +"%F %H:%M:%S"`
time_elapsed_record=`datediff --format="%d %H:%M:%S" $cl_start $cl_end`
time_elapsed_human=`datediff --format="%H:%M" $cl_start $cl_end`
#get any user note
# TODO: error checking, make this a proper script
if [[ $# -gt 0 ]]; then
user_note=$1
fi
#If the optional context is provided, prepare vars
with_slash=""
if [[ -d ~/.clock_log/$cur_context_code ]]; then
with_slash=$cur_context_code/
fi
#store record in ~/.clock_log/YYYY-MM-DD.log
echo ${cl_start} -- ${cl_end} >> ~/.clock_log/`date +"%F"`.log
if [[ -n ${with_slash} ]]; then
echo "Context code: $cur_context_code" >> ~/.clock_log/`date +"%F"`.log
fi
if [[ -f ~/.clock_log/clin_notes ]]; then
cat ~/.clock_log/clin_notes >> ~/.clock_log/`date +"%F"`.log
fi
echo " " $user_note >> ~/.clock_log/`date +"%F"`.log
echo " Days H:M:S - " $time_elapsed_record >> ~/.clock_log/`date +"%F"`.log
#also store a context log if there's a context
if [[ -n ${with_slash} ]]; then
echo ${cl_start} -- ${cl_end} >> ~/.clock_log/${with_slash}`date +"%F"`.log
if [[ -f ~/.clock_log/clin_notes ]]; then
cat ~/.clock_log/clin_notes >> ~/.clock_log/${with_slash}`date +"%F"`.log
fi
echo " " $user_note >> ~/.clock_log/${with_slash}`date +"%F"`.log
echo " Days H:M:S - " $time_elapsed_record >> ~/.clock_log/${with_slash}`date +"%F"`.log
fi
#tell user gist of what happened
echo "clocked out!"
if [[ -f ~/.clock_log/clin_notes ]]; then
echo " log:"
cat ~/.clock_log/clin_notes
fi
echo " note: " $user_note
echo " focus duration: " $time_elapsed_human
#update state variables and reset state for clock in
echo "OUT" > ~/.clock_log/clin_time
echo "OUT" > ~/.clock_log/state
if [[ -f ~/.clock_log/clin_notes ]]; then
rm ~/.clock_log/clin_notes
fi
#The state's updated,
#my zsh prompt configuration has a custom segment that reads the state
#file, so this is all we need to do to update the prompt
fi