-
Notifications
You must be signed in to change notification settings - Fork 0
/
π²
executable file
Β·605 lines (505 loc) Β· 12.6 KB
/
π²
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env bash
# text colors
RED="\033[0;31m" #red
GREEN="\033[0;32m" #green
YELLOW="\033[0;33m" #yellow
BLUE="\033[0;34m" #blue
CYAN="\033[0;36m" #cyan
PURPLE="\033[0;35m" #purple
# text craziness
BLINK="\033[05m" #blink
RED_BLINK="\033[0;31;5m" #red blink
# text format reset
COLOR_RESET="\033[0m"
MAPPINGS_FILE=$HOME/.π²/dir-aliases.txt
# echo lat,lon based on IP address
function __pse_get_location() {
curl -s "http://ipinfo.io/" | grep loc | grep -o \\-*[0-9][0-9]*.[0-9][0-9]*,\\-*[0-9][0-9]*.[0-9][0-9]*
}
# echo U.S. zip code based on IP address (via __pse_get_location)
function __pse_get_zip() {
latlng=`__pse_get_location`
#echo latlng $latlng
curl -s "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng=$latlng" | grep -B 1 postal_code | head -n1 | grep -o [0-9][0-9][0-9][0-9][0-9]
}
MOON=π
SUNNY=βοΈ
PARTLY=β
οΈ
CLOUDY=βοΈ
FOGGY=βοΈ
RAINY=βοΈ
SNOW=βοΈ
WINDY=π¨
LIGHTENING=β‘οΈ
UNITS=f
# echo a temperature in deg F and then a weather emoji from above
# $1: U.S. zip code
function __pse_get_weather() {
location=$1
url="http://query.yahooapis.com/v1/public/yql?q=use%20%27https%3A%2F%2Fraw.githubusercontent.com%2Fyql%2Fyql-tables%2Fmaster%2Fweather%2Fweather.bylocation.xml%27%20as%20we%3Bselect%20*%20from%20we%20where%20location%3D%22$location%22%20and%20unit%3D%27$UNITS%27&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
res=`curl -s $url | tr '\n' ' '`
#echo res: $res | tr '\n' ' ' 1>&2
#echo pretty res: $res | tr '\n' ' ' | python -mjson.tool 1>&2
code=`echo $res | tr '\n' ' ' | python -mjson.tool 2>/dev/null | grep code | head -n1 | grep -o "[0-9][0-9]*"` 2>/dev/null
temp=`echo $res | tr '\n' ' ' | python -mjson.tool 2>/dev/null | grep temp | head -n1 | grep -o "\-*[0-9][0-9]*"` 2>/dev/null
forecast_temp=`echo $res | tr '\n' ' ' | python -mjson.tool 2>/dev/null | grep '"forecast": ' -A 7 | grep high | grep -o "\-*[0-9][0-9]*"` 2>/dev/null
if [ "$code" == "" ]; then exit 1; fi
emoji="($code)"
case $code in
3)
emoji="$LIGHTNING $LIGHTNING $LIGHTNING"
;;
9 | 11 | 12 )
emoji=$RAINY
;;
13 | 14 | 15 | 16 | 41 | 42 | 43 | 46)
emoji=$SNOW
;;
32 | 34)
emoji=$SUNNY
;;
4 | 37 | 38 | 39)
emoji=$LIGHTNING
;;
20)
emoji=$FOGGY
;;
23 | 24)
emoji=$WINDY
;;
26 | 27 | 28)
emoji=$CLOUDY
;;
29 | 31 | 33)
emoji=$MOON
;;
21 | 30 | 44)
emoji=$PARTLY
;;
esac
emoji="${emoji} "
tempstr="${temp}Β°"
tempcolor=
if [ "$temp" -lt "40" ]
then
tempcolor=$CYN
elif [ "$temp" -gt "79" ]
then
tempcolor=$RED
elif [ "$temp" -gt "69" ]
then
tempcolor=$YELLOW
fi
forecaststr=""
if [ "$temp" -lt "$forecast_temp" ]; then
forecast_tempcolor=$COLOR_RESET
if [ "$forecast_temp" -lt "40" ]; then
forecast_tempcolor=$CYN
elif [ "$forecast_temp" -gt "79" ]; then
forecast_tempcolor=$RED
elif [ "$forecast_temp" -gt "69" ]; then
forecast_tempcolor=$YELLOW
fi
forecaststr=$COLOR_RESETβ$forecast_tempcolor$forecast_tempΒ°
fi
echo $tempcolor$tempstr$forecaststr$COLOR_RESET$emoji
}
LAST_WEATHER_FILE=/tmp/last_weather_string
LAST_FITBIT_WATER_FILE=/tmp/last_fitbit_water_string
LAST_FITBIT_STEPS_FILE=/tmp/last_fitbit_steps_string
# refreshes the cache in $LAST_WEATHER_FILE via:
# IP β latlon β zip code β weather > $LAST_WEATHER_FILE
function refresh_weather_cache() {
#echo refetching weather...
location=`__pse_get_zip`
#location=94107
#echo zip: $location
weather=`__pse_get_weather $location`
echo $weather > $LAST_WEATHER_FILE
}
# echo the contents of the $LAST_WEATHER_FILE
function _pse_weather_with_cache() {
w=`cat $LAST_WEATHER_FILE 2>/dev/null`
if [ "$w" == "" ]; then
return
fi
echo -e "$w "
}
function __pse_refresh_fitbit_water_cache() {
if which fitbit > /dev/null; then
water_logged=`fitbit water`
if [ "$water_logged" == "" ]; then
return
fi
echo $water_logged > $LAST_FITBIT_WATER_FILE
fi
}
function __pse_refresh_fitbit_steps_cache() {
if which fitbit > /dev/null; then
steps=`fitbit steps`
if [ "$steps" == "" ]; then
return
fi
echo $steps > $LAST_FITBIT_STEPS_FILE
fi
}
function _pse_fitbit_water_with_cache() {
water=`cat $LAST_FITBIT_WATER_FILE 2>/dev/null`
if [ "$water" == "" ]; then
return
fi
color=$CYAN
hour=`date +%H`
if [[ "$water" -gt "71" ]]; then
color=$GREEN
elif [[ "$water" -gt "47" ]]; then
if [[ "$hour" -gt "18" ]]; then
color=$RED_BLINK
elif [[ "$hour" -gt "17" ]]; then
color=$RED
elif [[ "$hour" -gt "16" ]]; then
color=$YELLOW
fi
elif [[ "$water" -gt "23" ]]; then
if [[ "$hour" -gt "16" ]]; then
color=$RED_BLINK
elif [[ "$hour" -gt "15" ]]; then
color=$RED
elif [[ "$hour" -gt "14" ]]; then
color=$YELLOW
fi
else
if [[ "$hour" -gt "13" ]]; then
color=$RED_BLINK
elif [[ "$hour" -gt "12" ]]; then
color=$RED
elif [[ "$hour" -gt "11" ]]; then
color=$YELLOW
fi
fi
droplet="π§ "
oz=oz
echo -e "$color$droplet$water$oz $COLOR_RESET"
}
function _pse_fitbit_steps_with_cache() {
steps=`cat $LAST_FITBIT_STEPS_FILE 2>/dev/null`
if [ "$steps" == "" ]; then
return
fi
color=$CYAN
hour=`date +%H`
if [[ "$steps" -gt "9999" ]]; then
color=$GREEN
elif [[ "$steps" -gt "7999" ]]; then
if [[ "$hour" -gt "20" ]]; then
color=$RED_BLINK
elif [[ "$hour" -gt "19" ]]; then
color=$RED
elif [[ "$hour" -gt "18" ]]; then
color=$YELLOW
fi
elif [[ "$steps" -gt "4999" ]]; then
if [[ "$hour" -gt "17" ]]; then
color=$RED_BLINK
elif [[ "$hour" -gt "16" ]]; then
color=$RED
elif [[ "$hour" -gt "15" ]]; then
color=$YELLOW
fi
else
if [[ "$hour" -gt "16" ]]; then
color=$RED_BLINK
elif [[ "$hour" -gt "15" ]]; then
color=$RED
elif [[ "$hour" -gt "14" ]]; then
color=$YELLOW
fi
fi
footprints="π£ "
echo -e "$footprints$color$steps$COLOR_RESET "
}
# echo an emoji based on the moon cycle
function __pse_get_moon() {
now=`date +%s`
#now=1639814418 # test: Dec 18th, 2021, should be full moon
new_moon=1383500000
cycle=2953059
multiple=100000
let moon_mod=($now - $new_moon)/60/60/24*$multiple%$cycle/$multiple
# echo $now $moon_mod 1>&2
# Lunar cycle
moon=$moon_mod
case $moon_mod in
29 | 0)
moon=π
;;
1 | 2 | 3 | 4)
moon=π
;;
5 | 6 | 7 | 8)
moon=π
;;
9 | 10 | 11 | 12)
moon=π
;;
13 | 14)
moon=π
;;
15 | 16 | 17 | 18)
moon=π
;;
19 | 20 | 21 | 22 | 23)
moon=π
;;
24 | 25 | 26 | 27 | 28)
moon=π
;;
esac
echo $moon
}
# echo a lock emoji if the current directory is not user writable
function __pse_get_lock() {
if [ ! -w . ]; then
echo "π "
fi
}
# echo the battery percentage (Mac only)
function __pse_get_battery_percentage() {
which pmset >/dev/null && pmset -g batt | grep -o [0-9]*% | sed 's/%//g'
}
# echo a battery warning emoji and percentage if the battery level is below
# certain thresholds (14%, 9%, 4%)
function __pse_get_battery() {
local first=$1
local second=$2
local third=$3
local res
batt=`__pse_get_battery_percentage`
if [ "$batt" == "" ]; then
return
elif [ "$batt" -gt "$first" ]; then
return
elif [ "$batt" -lt "$third" ]; then
# prepend red and blinking format if we are below $third threshold
res="$RED_BLINK"
elif [ "$batt" -lt "$second" ]; then
# prepend red format if we are below $second threshold
res="$RED"
fi
echo -e "$res π ($batt%)$COLOR_RESET"
}
function __pse_get_warnings() {
res=
#if [ "`system_profiler SPUSBDataType | grep iPhone`" != "" ]; then
# res="π± $res"
#fi
res=`__pse_get_lock`$res
res="`__pse_get_battery 14 9 4`$res"
echo $res
}
function __pse_get_time_of_day() {
local res=
# Holidays, etc
case "`date +%H:%M`" in
17:0*)
res=π»
;;
12:0*)
res=π
;;
esac
echo -e $res
}
function __pse_get_holiday() {
local res=
# Holidays, etc
case "`date +%m-%d`" in
02-14)
res=π
;;
03-16)
res=π
;;
03-17)
res=π
;;
03-25)
res=π
;;
06-23)
res=π
;;
06-26)
res=π»
;;
07-04)
res=πΊπΈ
;;
07-14)
res=π«π·
;;
07-17)
res=π
;;
10-31)
res="π π»"
;;
11-05)
res=π¬π§
;;
12-25)
res="π
π"
;;
esac
echo -e $res
}
function _pse_chrono() {
local res=""
local temp=""
temp=`__pse_get_holiday`
if [ "$temp" != "" ]; then res="$res$temp "; fi
temp=`__pse_get_time_of_day`
if [ "$temp" != "" ]; then res="$res$temp "; fi
res=$res`__pse_get_moon`
if [ "$res" != "" ]; then echo -e "$res "; fi
}
function _pse_exit_code() {
if [ "$1" != "0" ]; then
echo -n -e "βοΈ "
fi
}
function _pse_warnings() {
_pse_exit_code $1
__pse_get_warnings
}
function __pse_get_matching_dir() {
full_path=$1
# set emoji as a array from the second parameter
declare -a EMOJI_MAPPING=("${!2}")
rhs=$3
count=${#EMOJI_MAPPING[@]}
if [ "$count" -lt "1" ]; then
return
fi
# since the array is actually alternating key value pairs, we want to skip
# every other item, so let's just divide count by two
let "count /= 2"
# subtract 1 because of zero-indexed arrays
let count--
len_full_path=${#full_path}
let min_len=len_full_path-${#rhs}
for i in `seq 0 $count`;
do
# index of the directory path
let dir_i=i*2
# index after the directory is the emoji replacement
let emoji_i=dir_i+1
# grab the directory out of the array
this_dir=${EMOJI_MAPPING[$dir_i]}
len_this_dir=${#this_dir}
if [[ "$len_this_dir" -gt "$len_full_path" ]]; then
continue
fi
if [[ "$len_this_dir" -le "$min_len" ]]; then
continue
fi
if [[ "${full_path:0:$len_this_dir}" == "$this_dir" ]]; then
remainder=${full_path:$len_this_dir}
this_emoji=${EMOJI_MAPPING[$emoji_i]}
if [[ "$remainder" == "" ]]; then
echo -n $this_emoji
else
echo -n $this_emoji $remainder
fi
return
fi
done
}
function _pse_load_dir_mappings() {
if [ ! -f $MAPPINGS_FILE ]; then
return
fi
# read in the dir --> emoji mapping file
EMOJI_TMP=`cat $MAPPINGS_FILE | awk '{ print length, $0 }' | sort -rn | cut -d" " -f2-`
EMOJI_TMP=($EMOJI_TMP)
EMOJI_MAPPING=()
for item in ${EMOJI_TMP[*]};
do
EMOJI_MAPPING+=($(bash -c "echo $item"))
done
}
function _pse_get_dir() {
local orig_path=`pwd`
if [[ "$orig_path" == "/" ]]; then
echo "/"
return
fi
count=$1
while [ $count -gt 0 ]; do
re='.*((/[^/]+){'$count'})'
if [[ $orig_path =~ $re ]]; then
rest_of_path=${BASH_REMATCH[1]}
break
fi
let count=count-1
done
local emo=`__pse_get_matching_dir $orig_path EMOJI_MAPPING[@] $rest_of_path`
if [[ "$emo" != "" ]]; then
echo $emo
return
fi
# fixes // at root and removes leading slash for local directories
if [[ "$rest_of_path" != "$orig_path" ]]; then
rest_of_path=${rest_of_path:1}
fi
echo $rest_of_path
}
function __pse_unset_alias_for_dir() {
local dir=$1
dir=${dir/$HOME/\$HOME}
# replace anything that matches these two patterns:
# /path/to/dir
# /path/to/dir π²
sed -i.bak "s,^$dir\( .*\)*\$,,g" $MAPPINGS_FILE
# the last expression leaves a blank line so remove it
sed -i.bak '/^\s*$/d' $MAPPINGS_FILE
}
function __pse_set_alias_for_dir() {
local dir=$1
dir=${dir/$HOME/\$HOME}
local aliaz=$2
__pse_unset_alias_for_dir $dir
echo $dir $aliaz >> $MAPPINGS_FILE
}
function psemoji() {
local command=$1-$2
case $command in
alias-list)
cat $MAPPINGS_FILE
;;
alias-set)
aliaz=$3
__pse_set_alias_for_dir `pwd` $aliaz
cat $MAPPINGS_FILE
_pse_load_dir_mappings
;;
alias-unset)
__pse_unset_alias_for_dir `pwd`
cat $MAPPINGS_FILE
_pse_load_dir_mappings
;;
esac
}
# initialize the directory to emoji mappings
_pse_load_dir_mappings
PSE_WARNINGS="\$(_pse_warnings \$?)"
PSE_CHRONO="\$(_pse_chrono)"
PSE_WEATHER="\$(_pse_weather_with_cache)"
PSE_FITBIT_WATER="\$(_pse_fitbit_water_with_cache)"
PSE_FITBIT_STEPS="\$(_pse_fitbit_steps_with_cache)"
PSE_DIR_1="\$(_pse_get_dir 1)"
PSE_DIR_2="\$(_pse_get_dir 2)"
PSE_DIR_3="\$(_pse_get_dir 3)"
PSE_DIR_4="\$(_pse_get_dir 4)"
PSE_DIR_5="\$(_pse_get_dir 5)"
PSE_STANDARD=$PSE_WARNINGS$PSE_CHRONO$PSE_WEATHER$YELLOW$PSE_DIR_2$COLOR_RESET