-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomodoro
executable file
·205 lines (165 loc) · 5.07 KB
/
pomodoro
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
`msh src lib/x11`
pomodoro_settings="$HOME/.pomodoro.rc"
[ -e $pomodoro_settings ] && source $pomodoro_settings
pomodoro_logs_dir="$(realpath "${pomodoro_logs_dir:-$HOME/pomodoro/logs}")"
pomodoro_log_editor="${pomodoro_log_editor:-${EDITOR:-xdg-open}}"
today_link="$pomodoro_logs_dir/today.md"
before_link="$pomodoro_logs_dir/before.md"
start_timestamp="$(date)"
num_pomos_this_session=0
mkdir -p "$pomodoro_logs_dir"
CURRENT_VIMTASK_SCOPE=
CURRENT_POMO=
function read_log_metadata() {
local VIMTASK_SCOPE=$(tac "$today_link" | grep -m1 '^#.*|')
if [ -n "$VIMTASK_SCOPE" ]; then
CURRENT_VIMTASK_SCOPE="|${VIMTASK_SCOPE#*|}"
fi
local pomo=$(tac "$today_link" | grep -E --only-matching -m1 '^### \[.*\] Pomodoro [0-9]+')
pomo=${pomo#\###*Pomodoro}
pomo=${pomo%%|*}
pomo=${pomo/[^0-9]}
CURRENT_POMO=${pomo:-0}
}
function write_log() {
local VIMTASK_SCOPE=
local OPTIND=1
while getopts ":t" opt; do
case $opt in
t) VIMTASK_SCOPE=" $CURRENT_VIMTASK_SCOPE" ;;
esac
done
shift $(($OPTIND-1))
local DEPTH="####"
if [ $# -gt 1 ]; then
DEPTH=${1}
shift
fi
local MSG="$DEPTH [$(date +%H:%M)] ${1:-}$VIMTASK_SCOPE"
echo -e "$MSG"
echo -e "$MSG" >> "$today_link"
}
function write_log_raw() {
echo -e "$1"
echo -e "$1" >> "$today_link"
}
function write_log_horiz_sep() {
write_log_raw "--------------------------------------------------------------------------------\n"
}
function edit_pomo_file_cmd() {
echo "$pomodoro_log_editor $(realpath "$today_link")"
}
function show_log_editor() {
_msh_focus_or_exec "$(edit_pomo_file_cmd)"
read_log_metadata
}
function xok {
xmessage -default okay "$@"
}
function xyesno {
xmessage -default yes -buttons yes:0,no:1 "$@"
return
}
function _start_day() {
write_log_raw "# :pomodoro: || +pomodoro status:pending"
write_log_raw
local LAST_POMO_RELATIVE=$(realpath --relative-to="$pomodoro_logs_dir" "$before_link")
# TODO calculate LAST POMO date and use that in the vim-task filter instead of
# "today" and "tomorrow"
write_log_raw "## Recall and Plan | due:$(date --date=today +%Y-%m-%d)\n"
write_log_raw "Last Pomodoro: [$(basename "$LAST_POMO_RELATIVE")]($LAST_POMO_RELATIVE)"
write_log_raw "> TODO bring in some stats from last pomo, like projects and tasks\n\n"
write_log_raw "## Today $(date) | due:$(date --date=tomorrow +%Y-%m-%d)\n"
}
function _end_day() {
local end_timestamp="$(date)"
[ "$start_timestamp" == "$end_timestamp" ] && return
local bye_stats="\n## Summary\n- started: $start_timestamp\n- ended: $(date)\n- pomodoros: $num_pomos_this_session"
write_log "End Day\n$bye_stats\n--------------------------------------------------------------------------------"
echo -e "$bye_stats" | xok -nearmouse -file -
}
# cases
# - today entry deleted, want to start over
# - new day, current today link is old
# - day is over and want to continue pomodoro in new file
# ensure today link is correct
# ensure today's file is ready for logging
function _todays_pomolog_file() {
local pomolog_now_file="$pomodoro_logs_dir/$(date +%Y.%m.%d).md"
local today_link_file="$(realpath "$today_link")"
if [[ -e "$today_link_file" && "$pomolog_now_file" != "$today_link_file" ]]; then
# today_link points to a valid file, and we need to change it
_end_day
if [ "$pomolog_now_file" != "$today_link_file" ]; then
ln -srf "$today_link_file" "$before_link"
fi
ln -srf "$pomolog_now_file" "$today_link"
fi
if [ ! -e "$pomolog_now_file" ]; then
_start_day
fi
}
POMO_TIME_LEFT=
function _pomodoro() {
_todays_pomolog_file
read_log_metadata
CURRENT_POMO=$((CURRENT_POMO+1))
write_log -t "###" "Pomodoro $CURRENT_POMO"
local POMO_END=$((EPOCHSECONDS + 25*60))
while [ $EPOCHSECONDS -lt $POMO_END ]; do
POMO_TIME_LEFT=$(( (POMO_END - EPOCHSECONDS) / 60 ))m
_update_process_name "$POMO_TIME_LEFT"
sleep 5m
done
_update_process_name "END"
write_log "End Pomodoro $CURRENT_POMO"
# TAKE NOTES
if xyesno -nearmouse -file - <<-EOF
End of pomodoro $CURRENT_POMO
Take notes?
EOF
then
write_log "Take notes"
show_log_editor
fi
num_pomos_this_session=$((num_pomos_this_session+1))
# TAKE A BREAK heh?
xyesno -nearmouse "Take a break?"
# POMODORO?
xyesno -nearmouse "Pomodoro $((CURRENT_POMO+1))?"
return
}
# Update the process_name to see remaining time
function _update_process_name() {
local STATUS="$1"
local my_name_is=`basename $0`
if [ -n "$STATUS" ]; then
my_name_is+="[$STATUS]"
fi
# tmux support
if [ -n "$TMUX" ]; then
# update tmux pane title
printf '\033]2;%s\033\\' "$my_name_is"
fi
echo -n "$my_name_is" > /proc/$$/comm
}
OPTIND=1
while getopts "t" opt; do
case "$opt" in
t) realpath "$today_link"; exit;;
esac
done
# if there's a pomodoro already running, just show the log
THIS_PID=$$
if pgrep "$(basename "$0")" | grep -v $THIS_PID &>/dev/null; then
write_log
show_log_editor
exit 0
fi
if xyesno -nearmouse "Plan session? Take notes?"; then
show_log_editor
fi
( while read; do show_log_editor & done ) </dev/stdin &
while _pomodoro; do true; done
_end_day