-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomo-time.sh
executable file
·138 lines (97 loc) · 2.34 KB
/
pomo-time.sh
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
#!/bin/dash
# this is ridiculously slow
scriptName="$0"
elapsedSecondsLastPomo()
{
ps -C pomo -o etimes | tail -n 1 | tr -d '[:blank:]'
}
isPomoRunning ()
{
pgrep -a pomo | grep -qv "$scriptName"
}
pomoTime ()
{
# only works for minutes
secondArgNum="$(ps -C pomo -o cmd | tail -n 1 | cut -f3 -d' ' | grep -o '[0-9]*')"
if [ -n "$secondArgNum" ]
then
echo "$secondArgNum"
else
# there was no time specified when pomo was run
isPomoRunning && # but pomo *was* run
echo 25
fi
# unnecessary redundancy and echo but probably clearer though probably not idiomatic
}
minutesToSeconds ()
{
[ -n "$1" ] && echo "$(( $1 * 60))"
}
pomoTimeSeconds()
{
timeMinutes="$(pomoTime)"
[ -n "$timeMinutes" ] && minutesToSeconds "$timeMinutes"
# likewise
}
timeLeftLastPomo ()
{
# figure out how long it was for and use elapsedSecondsLastPomo to subtract
totalTime="$(pomoTimeSeconds)"
elapsedTime="$(elapsedSecondsLastPomo)"
[ -n "$totalTime" -a -n "$elapsedTime" ] &&
echo "$(( totalTime - elapsedTime))"
}
zeroPad()
{
# first argument is the int to pad
# second argument is number of digits
if [ $(echo -n "$1" | wc -c) -lt "$2" ] # if you need more padding than your int is long, pad
then
zeroPad "0$1" "$2"
# pad one zero then recurse
else
echo $1
fi
}
secondsToMinutesAndSeconds()
{
[ -z "$1" ] && echo -n "" && exit 1
minutes="$(( $1 / 60 ))"
[ -z "$minutes" ] && minutes="00"
seconds="$(( $1 % 60 ))"
echo $(zeroPad $minutes 2):$(zeroPad $seconds 2)
}
formatTimeLeftLastPomo ()
{
secondsToMinutesAndSeconds $(timeLeftLastPomo)
}
pomoCmd ()
{
ps -C pomo -o cmd | tail -n 1 | cut -f3- -d' '
}
pomoName ()
{
isPomoRunning || exit 1
cmdFirstNum="$(pomoCmd | sed -E 's/^[0-9]+[a-zA-Z]* *//')"
name="$(pomoCmd | sed -E 's/[0-9]+[a-zA-Z]* *//' | grep '[a-zA-Z0-9]')" # it has any kind of name
if pomoCmd | grep -qE -e '(25|25m)' || pomoCmd | grep -qv -E '^[0-9]+[a-zA-Z]*' # either it's got 25 minutes explicitly or it's got no number in $1
then
if [ -n "$name" ]; then
echo "Pomo: $name"
else
echo Pomodoro
fi
else
if [ -n "$name" ]; then
echo "Timer: $name"
else
echo Timer
fi
fi
}
if [ "$1" = name ]
then
pomoName
else
isPomoRunning && formatTimeLeftLastPomo
fi