This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
forked from tashima42/pomo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpd
executable file
·157 lines (140 loc) · 5.91 KB
/
pd
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
#!/bin/bash
### Support for MacOS and Linux
kernel=$(uname -s)
### Send desktop notifications
## Params: header {String} - Notification highlighted text
## Params: body {String} - Notification complementary text
## Returns: {Void}
function notify() {
header=$1
body=$2
if [[ $kernel == "Darwin" ]]; then
osascript -e "display notification \"${body:?}\" with title \"${header:?}\" sound name \"~/.config/pomodoro-cli/sound.wav\""
else
notify-send -u critical -t 0 -a pomodoro-cli "${header:?}" "${body:?}"
aplay ~/.config/pomodoro-cli/sound.wav &> /dev/null
fi
}
### Show a countdown timer and a message and updates without
### cleaning the whole screen. Also kills apps if needed
## Params: seconds {Number} - Countdown time in seconds
## Params: message {String} - Timer description
## Item: IFS=',' is (Internal Field Separator) set to comma
## Returns: {String} - "hh:mm:ss - $message"
function countdown(){
secs=$1
IFS=','
read -ra arr <<< "$apps_to_kill"
shift
message=$*
while [ "$secs" -gt -1 ]
do
if [[ $message == "FOCUS TIME" ]]; then
for app_name in "${arr[@]}"; do
if pgrep -x "$app_name" > /dev/null
then
pkill "$app_name"
fi
done
fi
sleep 1 &
printf "\r%s - %02d:%02d:%02d" "$message" $((secs/3600)) $(((secs/60)%60)) $((secs%60))
secs=$(( secs - 1 ))
wait
done
echo
}
### Transforms minutes into seconds
## Params: minutes {Number} - Ammount of minutes to be transformed to seconds
## Returns: {Number} - Seconds
function minutes_to_seconds() {
minutes=$1
echo $((minutes * 60))
}
### Add minutes to current time
## Params: minutes {Number} - Ammounts of minutes to be added
## Returns: {Date} - Current date plus minutes
function current_time_plus_minutes() {
minutes=$1
if [[ $kernel == "Darwin" ]]; then
date -v +"${minutes}"M +%R
else
date -d "$minutes minutes" +'%H:%M'
fi
}
### Display a summary of the settings defined by the user
## Params: focus_minutes {Number} - Ammount of minutes to last a focus period
## Params: break_minutes {Number} - Ammount of minutes to last a break period
## Params: long_break_minutes {Number} - Ammount of minutes to last a long break period
## Params: breaks_until_long {Number} - Ammount of breaks until a long break period starts
## Params: apps_to_kill {String} - Comma separated list of apps to kill
## Returns: {String} - Formatted summary of the settings
function display_summary() {
focus_minutes=$1
break_minutes=$2
long_break_minutes=$3
breaks_until_long=$4
apps_to_kill=$5
echo "╔════════════════╦════════╗"
echo "║ FOCUS ║ $(printf "%03d\n" "$focus_minutes") ║"
echo "║ BREAK ║ $(printf "%03d\n" "$break_minutes") ║"
echo "║ LONG BREAK ║ $(printf "%03d\n" "$long_break_minutes") ║"
echo "║ BREAKS TL LONG ║ $(printf "%03d\n" "$breaks_until_long") ║"
echo "║ APPS TO AVOID ║$(printf "$apps_to_kill")"
echo "╚════════════════╩════════╝"
}
### Display a help message
## Returns: {String} - Formatted help message with all available settings and options
function display_help() {
echo "Usage: $(basename "$0") [options] [focus] [break] [long_break] [breaks_until_long]"
echo " options -h: display help message"
echo " focus Minutes of focus until break | Default = 25"
echo " break Minutes of break until focus | Default = 5"
echo " long_break Minutes of long break until focus | Default = 15"
echo " breaks_until_long Number of breaks until long break | Default = 4"
echo " apps_to_kill ',' separated list of apps to kill| Default = none"
}
### Controls the application flow, parse arguments, show the countdown and notifications
## Params: focus_minutes {Number} - Ammount of minutes to last a focus period
## Params: break_minutes {Number} - Ammount of minutes to last a break period
## Params: long_break_minutes {Number} - Ammount of minutes to last a long break period
## Params: breaks_until_long {Number} - Ammount of breaks until a long break period starts
## Params: apps_to_kill {String} - Comma separated list of apps to kill
## Returns: {Void}
function main() {
focus_minutes=$1
break_minutes=$2
long_break_minutes=$3
breaks_until_long=$4
apps_to_kill=$5
focus_seconds=$(minutes_to_seconds "$focus_minutes")
break_seconds=$(minutes_to_seconds "$break_minutes")
long_break_seconds=$(minutes_to_seconds "$long_break_minutes")
#Creating alert and asking for reiwing its inputs
zenity --error --title 'Review your break!!' \
--text " $focus_minutes minutes to last a focus period and $break_minutes minutes to last a break period $long_break_minutes minutes to last a long break period
$breaks_until_long breaks until a long break period starts"\
--ok-label Accept \
--extra-button Dont_Accept
display_summary "$focus_minutes" "$break_minutes" "$long_break_minutes" "$breaks_until_long" "$apps_to_kill"
while true; do
for (( i=1; i<=breaks_until_long; i++ )); do
countdown "$focus_seconds" "FOCUS TIME"
notify "BREAK: $break_minutes MINUTES" "Focus time at $(current_time_plus_minutes "$break_minutes")"
if [ $((i)) -ne "$breaks_until_long" ]; then
countdown "$break_seconds" "BREAK TIME"
notify "FOCUS: $focus_minutes MINUTES" "Break time at $(current_time_plus_minutes "$focus_minutes")"
else
notify "LONG BREAK: $long_break_minutes MINUTES" "Focus time at $(current_time_plus_minutes "$long_break_minutes")"
fi
done
countdown "$long_break_seconds" "LONG BREAK TIME"
notify "FOCUS: $focus_minutes MINUTES" "Break time at $(current_time_plus_minutes "$focus_minutes")"
done
}
## Help message
if [ "$1" == "-h" ]; then
display_help
exit 0
fi
main "${1:-25}" "${2:-5}" "${3:-15}" "${4:-4}" "${5:-none}"