forked from SIDSSIDS/dzen-cal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dzen-cal
executable file
·353 lines (309 loc) · 10.4 KB
/
dzen-cal
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env bash
function repeatAnnually() {
repeatMonth=$(printf %02d $1)
repeatDay=$(printf %02d $2)
currentYear=`date +%Y`
repeatYearsBefore=$3
repeatYearsAfter=$4
new_days=()
# Current year date
new_days+=("$currentYear-$repeatMonth-$repeatDay")
# Previous years dates
for ((n=0; n < repeatYearsBefore; n++))
do
currentYear=$(($currentYear-1))
new_days+=("$currentYear-$repeatMonth-$repeatDay")
done
# Reset year to current after repeatYearsBefore loop
currentYear=$(($currentYear+$repeatYearsBefore))
# Next years dates
for ((n=0; n < repeatYearsAfter; n++))
do
currentYear=$(($currentYear+1))
new_days+=("$currentYear-$repeatMonth-$repeatDay")
done
}
function repeat_highlighted_days() {
# Number of years before to include
repeatYearsBefore=1
# Number of years after to include
repeatYearsAfter=3
repeatAnnually $1 $2 $repeatYearsBefore $repeatYearsAfter
highlighted_days=("${highlighted_days[@]}" "${new_days[@]}")
}
function repeat_holidays() {
# Number of years before to include
repeatYearsBefore=0
# Number of years after to include
repeatYearsAfter=3
repeatAnnually $1 $2 $repeatYearsBefore $repeatYearsAfter
holidays=("${holidays[@]}" "${new_days[@]}")
}
highlighted_days=(
)
# Birthdays
repeat_highlighted_days 5 4
holidays=(
# Good Friday
"2018-03-30"
"2019-04-19"
"2020-04-10"
"2021-04-02"
)
# New Year's Day
repeat_holidays 1 1
# Rememberance Day
repeat_holidays 11 11
# Christmas Day
repeat_holidays 12 25
# Boxing Day
repeat_holidays 12 26
declare -a aligns=( "tl" "tm" "tr"
"ml" "mm" "mr"
"bl" "bm" "br" )
declare -A aligns_map
for key in "${!aligns[@]}"; do
aligns_map[${aligns[$key]}]=1;
done
function test_args() {
if [[ -z "${aligns_map[$align]}" ]]; then
echo "ERROR: unknown align: $align"
exit 1
fi
if [[ "$view_type" != "month" ]] && [[ "$view_type" != "year" ]]; then
echo "ERROR: unknown view type: $view_type"
exit 1
fi
}
function read_args() {
OPTS=`getopt -o a:,m:,y:,v:,s: -l x-indent:,y-indent: -- "$@"`
eval set -- "$OPTS"
while true; do
case "$1" in
-v ) view_type="$2"; shift 2 ;;
-m ) month="$2"; shift 2 ;;
-y ) year="$2"; shift 2 ;;
-a ) align="$2"; shift 2 ;;
-s ) xs="$2"; shift 2;;
--x-indent) x_indent="$2"; shift 2;;
--y-indent) y_indent="$2"; shift 2;;
-- ) shift; break ;;
* ) break ;;
esac
done
x_indent="${x_indent:-0}"
y_indent="${y_indent:-0}"
day=$(date +%d)
month=${month:-$current_month}
year=${year:-$current_year}
if (( month > 12 )); then
month=1
((year++))
fi
if (( month < 1 )); then
month=12
((year--))
fi
}
# Import colors from .Xresources
source $HOME/.config/colors.sh
defaultBg=${color[0]}
defaultFg=${fg1}
curDayBg=${color[4]}
curDayFg=${color[0]}
holidayFg=${color[1]}
holidayBg=""
highlightedFg=${color[2]}
highlightedBg=""
titleName="dzen-calendar"
font="Iosevka:pixelsize=16:antialias=true:autohint=true"
font_height=24
width_month=$(( 8 * 24 ))
width_year=$(( 8 * 68 ))
screenW=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f1)
screenH=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f2)
script_path="$(cd "$(dirname "$0")"; pwd -P)/$(basename "$0")"
current_month=$(date +%-m)
current_year=$(date +%-Y)
view_type="month"
first_day="Sunday"
align="tr"
xs=1
fd=$(echo "$first_day" | awk '{print tolower($0)}')
read_args "$@"
test_args
next_cmd="$script_path -s $xs -a $align --x-indent $x_indent --y-indent $y_indent -m $((month + 1)) -y $year"
prev_cmd="$script_path -s $xs -a $align --x-indent $x_indent --y-indent $y_indent -m $((month - 1)) -y $year"
next="^bg($defaultFg)^fg($defaultBg)^ca(1,$next_cmd)>>^ca()^fg()^bg()"
prev="^bg($defaultFg)^fg($defaultBg)^ca(1,$prev_cmd)<<^ca()^fg()^bg()"
year_cmd="$script_path -s $xs -a $align --x-indent $x_indent --y-indent $y_indent -y $year -v year"
year_btn="^bg($defaultFg)^fg($defaultBg)^ca(1,$year_cmd)$year^ca()^fg()^bg()"
month_cmd="$script_path -s $xs -a $align --x-indent $x_indent --y-indent $y_indent -y $year -v month"
month_btn="^bg($defaultFg)^fg($defaultBg)^ca(1,$month_cmd)$year^ca()^fg()^bg()"
next_year_cmd="$script_path -s $xs -a $align --x-indent $x_indent --y-indent $y_indent -y $((year + 1)) -v year"
prev_year_cmd="$script_path -s $xs -a $align --x-indent $x_indent --y-indent $y_indent -y $((year - 1)) -v year"
next_year="^bg($defaultFg)^fg($defaultBg)^ca(1,$next_year_cmd)>>^ca()^fg()^bg()"
prev_year="^bg($defaultFg)^fg($defaultBg)^ca(1,$prev_year_cmd)<<^ca()^fg()^bg()"
generate_colored_month_output() {
cal --$fd $month $year | awk -v hd="${highlighted_days[*]}" -v hds="${holidays[*]}" '
function isHighlighted(d,m,y,cur_d,cur_m,cur_y) {
return int(d) == int(cur_d) && int(m) == int(cur_m) && int(y) == int(cur_y)
}
function isHoliday(d,m,y,weekend) {
for (ii in holidays) {
if (int(holidays[ii][3]) == int(d) && int(holidays[ii][2]) == int(m) && int(holidays[ii][1]) == int(y)) {
return 1
}
}
return 0
}
BEGIN {
split(hd, tmp, " ")
for (i in tmp) {
h_days[i][0]=""
split(tmp[i],h_days[i],"-")
}
split(hds, tmp2, " ")
for (i in tmp2) {
holidays[i][0]=""
split(tmp2[i],holidays[i],"-")
}
}
{
# skip title line
if (NR > 1) {
for (i = 1; i <= length($0); i+=3) {
day=substr($0,i,2)
prefix = ""
postfix = ""
if (isHoliday(day,"'"$month"'","'"$year"'", int(i/3) >= 5)) {
prefix = "^bg('"$holidayBg"')^fg('"$holidayFg"')"
postfix = "^fg()^bg()"
}
for (j in h_days) {
if (isHighlighted(h_days[j][3],h_days[j][2],h_days[j][1],day,"'"$month"'","'"$year"'")) {
prefix = "^bg('"$highlightedBg"')^fg('"$highlightedFg"')"
postfix = "^fg()^bg()"
break;
}
}
if (isHighlighted("'"$day"'", "'"$month"'", "'"$year"'",
day,"'"$current_month"'","'"$current_year"'")) {
prefix = "^bg('"$curDayBg"')^fg('"$curDayFg"')"
postfix = "^fg()^bg()"
}
printf "%s%s%s%s",prefix,day,postfix,substr($0,i+2,1)
}
printf "\n"
}
else {
print $0
printf "\n"
}
}' | sed "1s|$year|$year_btn|; 1s|^ |$prev|; 1s| $|$next|"
}
generate_colored_year_output() {
cal --$fd -y $year | awk -v hd="${highlighted_days[*]}" -v hds="${holidays[*]}" '
function monthEquals(m, cur_line, cur_col) {
cur_row = int((cur_line-3)/8)
row = int((int(m)-1)/3)
col = (int(m)-1) - row*3
return col == cur_col && row == cur_row
}
function isHighlighted(d,m,y,cur_d, cur_line, cur_col, cur_y) {
return int(d) == int(cur_d) && int(y) == int(cur_y) && monthEquals(m, cur_line, cur_col)
}
function formatMonthLine(line,start,col) {
for(i = start; i < start + 21; i+=3) {
day=substr(line,i,2)
prefix = ""
postfix = ""
if (isHoliday(day,NR,col,"'"$year"'", int((i-start)/3) >= 5)) {
prefix = "^bg('"$holidayBg"')^fg('"$holidayFg"')"
postfix = "^fg()^bg()"
}
for (j in h_days) {
if (isHighlighted(h_days[j][3],h_days[j][2],h_days[j][1],day,NR,col,"'"$year"'")) {
prefix = "^bg('"$highlightedBg"')^fg('"$highlightedFg"')"
postfix = "^fg()^bg()"
break;
}
}
if (isHighlighted("'"$day"'","'"$month"'","'"$year"'",day,NR,col,"'"$current_year"'")) {
prefix = "^bg('"$curDayBg"')^fg('"$curDayFg"')"
postfix = "^fg()^bg()"
}
printf "%s%s%s%s",prefix,day,postfix,substr(line,i+2,1)
}
}
function isHoliday(d,cur_line,cur_col,y,weekend) {
for (ii in holidays) {
if (int(holidays[ii][3]) == int(d) && monthEquals(int(holidays[ii][2]), cur_line, cur_col) && int(holidays[ii][1]) == int(y)) {
return 1
}
}
return 0
}
BEGIN {
split(hd, tmp, " ")
for (i in tmp) {
h_days[i][0]=""
split(tmp[i],h_days[i],"-")
}
split(hds, tmp2, " ")
for (i in tmp2) {
holidays[i][0]=""
split(tmp2[i],holidays[i],"-")
}
}
{
# skip title lines
if (NR == 1 || NR == 2 || NR == 3 || NR == 11 || NR == 19 || NR == 27) {
print $0
}
else {
formatMonthLine($0,1,0)
printf "%s",substr($0,22,1)
formatMonthLine($0,24,1)
printf "%s",substr($0,45,1)
formatMonthLine($0,47,2)
printf "\n"
}
}' | sed "1s|$year|$month_btn|; 1s|^ |$prev_year|; 1s| $|$next_year|"
}
# kill running dzen-calendar instances
pkill -f "dzen2 -title-name $titleName"
if [[ "$view_type" == "year" ]]; then
out=$(generate_colored_year_output)
width=$width_year
else
out=$(generate_colored_month_output)
width=$width_month
fi
lines=$(echo "$out" | wc -l)
height=$(( $lines * $font_height ))
x_align=${align:1:1}
y_align=${align:0:1}
case "$x_align" in
"l" ) xpos=$(( $x_indent + 32 )) ;;
"m" ) xpos=$(( $screenW / 2 - $width / 2 )) ;;
"r" ) xpos=$(( - $width - $x_indent - 32 )) ;;
esac
case "$y_align" in
"t" ) ypos=$(( $y_indent + 71 )) ;;
"m" ) ypos=$(( $screenH / 2 - $height / 2 )) ;;
"b" ) ypos=$(( - $height - $y_indent + 4 )) ;;
esac
echo "$out" | dzen2 -title-name "$titleName" \
-fn "$font" \
-fg "$defaultFg" \
-bg "$defaultBg" \
-p \
-xs "$xs" \
-x "$xpos" \
-y "$ypos" \
-w "$width" \
-l "$((lines))" \
-sa 'c' \
-ta 'c' \
-e 'onstart=uncollapse;button1=exit;button3=exit' &