Last active
October 26, 2020 08:37
-
-
Save michaelkl/1d5d14856a40ae6919d565d7a04b2371 to your computer and use it in GitHub Desktop.
Interactive calendar for your console. Allows to scroll through the calendar interactively. Uses `cal` utility to print the calendar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
# Interactive cal | |
# | |
# Allows to scroll through the calendar interactively. | |
# Uses `cal` utility to print the calendar. | |
# | |
# © Michael Klimenko | |
set_current_date() { | |
Y=$(($(date +"%Y") + 0)) | |
M=$(($(date +"%-m") + 0)) | |
} | |
normalize_date() { | |
((M == 0)) && M=12 && ((Y--)) | |
((M == 13)) && M=1 && ((Y++)) | |
} | |
read_key() { | |
read -sn1 key # 1 char (not delimiter), silent | |
read -sn1 -t 0.0001 k1 # This grabs all three symbols | |
read -sn1 -t 0.0001 k2 # and puts them together | |
read -sn1 -t 0.0001 k3 # so you can case their entire input. | |
key+=${k1}${k2}${k3} | |
} | |
prev_month() { | |
((M--)) | |
normalize_date | |
} | |
next_month() { | |
((M++)) | |
normalize_date | |
} | |
set_current_date | |
while true | |
do | |
clear | |
cal -3 $M $Y | |
read_key | |
case "$key" in | |
# k|$'\e[A'|$'\e0A') # up arrow | |
# echo up;; | |
# j|$'\e[B'|$'\e0B') # down arrow | |
# echo down;; | |
l|$'\e[D'|$'\e0D') # left arrow | |
prev_month;; | |
h|$'\e[C'|$'\e0C') # right arrow | |
next_month;; | |
$'\e[1~'|$'\e0H'|$'\e[H') # home key: | |
set_current_date;; | |
# $'\e[4~'|$'\e0F'|$'\e[F') # end key: | |
# echo end;; | |
q|'') # q, CR: quit | |
echo Bye! | |
exit;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment