Skip to content

Commit d3e1c2a

Browse files
committedMay 29, 2021
Add customized pomodoro-plus plugin
1 parent 7e843a7 commit d3e1c2a

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed
 

‎tmux-pomodoro-plus/LICENSE.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2021 Oli Morris
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the "Software"),
5+
to deal in the Software without restriction, including without limitation
6+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
and/or sell copies of the Software, and to permit persons to whom the
8+
Software is furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included
11+
in all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎tmux-pomodoro-plus/pomodoro.tmux

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
3+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
5+
default_start_pomodoro="a"
6+
start_pomodoro="@pomodoro_start"
7+
default_cancel_pomodoro="A"
8+
cancel_pomodoro="@pomodoro_cancel"
9+
10+
pomodoro_st="#($CURRENT_DIR/scripts/pomodoro.sh)"
11+
pomodoro_st_interpolation_string="\#{pomodoro_st}"
12+
13+
source $CURRENT_DIR/scripts/helpers.sh
14+
15+
set_start_binding() {
16+
local key_bindings=$(get_tmux_option "$start_pomodoro" "$default_start_pomodoro")
17+
local key
18+
for key in $key_bindings; do
19+
tmux bind-key "$key" run-shell "$CURRENT_DIR/scripts/pomodoro.sh start"
20+
done
21+
}
22+
23+
set_cancel_binding() {
24+
local key_bindings=$(get_tmux_option "$cancel_pomodoro" "$default_cancel_pomodoro")
25+
local key
26+
for key in $key_bindings; do
27+
tmux bind-key "$key" run-shell "$CURRENT_DIR/scripts/pomodoro.sh cancel"
28+
done
29+
}
30+
31+
do_interpolation() {
32+
local string="$1"
33+
local interpolated="${string/$pomodoro_st_interpolation_string/$pomodoro_st}"
34+
echo "$interpolated"
35+
}
36+
37+
update_tmux_option() {
38+
local option="$1"
39+
local option_value="$(get_tmux_option "$option")"
40+
local new_option_value="$(do_interpolation "$option_value")"
41+
set_tmux_option "$option" "$new_option_value"
42+
}
43+
44+
main() {
45+
set_start_binding
46+
set_cancel_binding
47+
update_tmux_option "status-right"
48+
update_tmux_option "status-left"
49+
}
50+
main

‎tmux-pomodoro-plus/scripts/helpers.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
get_tmux_option() {
2+
local option=$1
3+
local default_value=$2
4+
local option_value=$(tmux show-option -gqv "$option")
5+
if [ -z "$option_value" ]; then
6+
echo "$default_value"
7+
else
8+
echo "$option_value"
9+
fi
10+
}
11+
12+
set_tmux_option() {
13+
local option="$1"
14+
local value="$2"
15+
tmux set-option -gq "$option" "$value"
16+
}
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env bash
2+
3+
################################# SET VARIABLES ################################
4+
5+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
7+
pomodoro_duration_minutes="@pomodoro_mins"
8+
pomodoro_break_minutes="@pomodoro_break_mins"
9+
10+
pomodoro_on="@pomodoro_on"
11+
pomodoro_complete="@pomodoro_complete"
12+
pomodoro_on_default="P:"
13+
pomodoro_complete_default=""
14+
15+
SESSION_NAME="$( tmux display-message -p "#S")"
16+
POMODORO_DIR="/tmp/pomodoro/$SESSION_NAME"
17+
POMODORO_FILE="$POMODORO_DIR/pomodoro.txt"
18+
19+
source $CURRENT_DIR/helpers.sh
20+
21+
################################# FUNCTIONALITY ################################
22+
23+
get_pomodoro_duration () {
24+
get_tmux_option "$pomodoro_duration_minutes" "25"
25+
}
26+
27+
get_pomodoro_break () {
28+
get_tmux_option "$pomodoro_break_minutes" "5"
29+
}
30+
31+
get_seconds () {
32+
date +%s
33+
}
34+
35+
write_pomodoro_start_time () {
36+
mkdir -p $POMODORO_DIR
37+
echo $(get_seconds) > $POMODORO_FILE
38+
}
39+
40+
read_pomodoro_start_time () {
41+
if [ -f $POMODORO_FILE ]
42+
then
43+
cat $POMODORO_FILE
44+
else
45+
echo -1
46+
fi
47+
}
48+
49+
remove_pomodoro_start_time () {
50+
if [ -f $POMODORO_FILE ]
51+
then
52+
rm $POMODORO_FILE
53+
fi
54+
}
55+
56+
if_inside_tmux () {
57+
test -n "${TMUX}"
58+
}
59+
60+
pomodoro_start () {
61+
write_pomodoro_start_time
62+
if_inside_tmux && tmux refresh-client -S
63+
return 0
64+
}
65+
66+
pomodoro_cancel () {
67+
remove_pomodoro_start_time
68+
if_inside_tmux && tmux refresh-client -S
69+
return 0
70+
}
71+
72+
pomodoro_status () {
73+
local pomodoro_start_time=$(read_pomodoro_start_time)
74+
local current_time=$(get_seconds)
75+
local difference=$(( ($current_time - $pomodoro_start_time) / 60 ))
76+
77+
if [ $pomodoro_start_time -eq -1 ]
78+
then
79+
echo ""
80+
elif [ $difference -ge $(( $(get_pomodoro_duration) + $(get_pomodoro_break) )) ]
81+
then
82+
pomodoro_start_time=-1
83+
echo ""
84+
elif [ $difference -ge $(get_pomodoro_duration) ]
85+
then
86+
printf "$(get_tmux_option "$pomodoro_complete" "$pomodoro_complete_default")$(( -($difference - $(get_pomodoro_duration) - $(get_pomodoro_break)) ))#[default]"
87+
else
88+
printf "$(get_tmux_option "$pomodoro_on" "$pomodoro_on_default")$(( $(get_pomodoro_duration) - $difference ))#[default]"
89+
fi
90+
}
91+
92+
main () {
93+
cmd=$1
94+
shift
95+
96+
if [ "$cmd" = "start" ]
97+
then
98+
pomodoro_start
99+
elif [ "$cmd" = "cancel" ]
100+
then
101+
pomodoro_cancel
102+
else
103+
pomodoro_status
104+
fi
105+
}
106+
107+
main $@

0 commit comments

Comments
 (0)
Please sign in to comment.