-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalsh.sh
196 lines (172 loc) · 4.19 KB
/
calsh.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
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
#!/bin/bash
function is_whole_num {
# Returns zero if argument has only digits
# Otherwise non-zero value is returned
if (( "$#" == 0 ))
then
echo "Error: is_whole_num (function): No arguments" >&2
return 1
fi
case "$1" in
("" | *[!0-9]*)
return 1
esac
return 0
}
function parse_month {
# Check if argument is a valid month name or number
# Write parsed month's number to STDOUT
# Write -1 to STDOUT on error
if (( "$#" == 0 ))
then
echo "Error: parse_month (function): No arguments" >&2
echo "-1"
fi
if is_whole_num "$1" && (( "$1" > 0 )) && (( "$1" < 13 ))
then
echo "Debug: month_arg is a valid month num" >&2
month_num=$1
else
echo "Debug: month_arg is not a valid month num" >&2
month_num=$(date --date="28 $1 2020" +"%m")
if (( "$?" == 1 ))
then
echo "Debug: month_arg is not a valid month name as well" >&2
echo "-1" # rv
fi
fi
echo "Debug: month_arg is a valid month name" >&2
echo "$month_num" # rv
}
function parse_year {
# Checks if argument is a natural number less than 9999
# Write -1 to STDOUT on error
# Returns parsed year
if (( "$#" == 0 ))
then
echo "Error: parse_year (function): No arguments" >&2
echo "-1" # rv
fi
if is_whole_num "$1" && (( "$1" > 0 )) && (( "$1" < 10000 ))
then
echo "Debug: year_arg is a valid year" >&2
echo "check: year $1" >&2
echo "$1" # rv
else
echo "Debug: year_arg is not a valid year" >&2
echo "-1" # rv
fi
}
# Configure keys
n_century=$'\6' # C-f
p_century=$'\2' # C-b
n_decade='J'
p_decade='K'
n_year='j'
p_year='k'
n_month='l'
p_month='h'
# Obtain required arguments
if (( "$#" > 2 ))
then
echo "Error: Too many arguments"
exit 1
elif (( "$#" == 2 ))
then
month_arg=$1
year_arg=$2
elif (( "$#" == 1 ))
then
month_arg=$(date +%m)
year_arg=$1
elif (( "$#" == 0 ))
then
month_arg=$(date +%m)
year_arg=$(date +%Y)
fi
echo "$month_arg, $year_arg" >&2
# Parse month
month=$(parse_month "$month_arg")
if (( month == -1 ))
then
echo "Error: $month_arg is neither a month number (1..12) nor a name"
exit 1
fi
# Parse year
year=$(parse_year "$year_arg")
if (( year == 0 ))
then
echo "Error: year $year not in range 1..9999"
exit 1
fi
echo "$month, $year" >&2
# The event loop
response='a'
while [ "$response" != $'\E' ] && [ "$response" != "q" ]
do
clear
cal "$month" "$year"
read -rn1 response
# printf '\n%q\n' "$response" >&2
case "$response" in
"$n_century")
# Increment century
if (( year < 9900 ))
then
year=$((year + 100))
fi;;
"$p_century")
# Decrement century
if (( year > 100 ))
then
year=$((year - 100))
fi;;
"$n_decade")
# Increment decade
if (( year < 9990 ))
then
year=$((year + 10))
fi;;
"$p_decade")
# Decrement decade
if (( year > 10 ))
then
year=$((year - 10))
fi;;
"$n_year")
# Increment year
if (( year < 9999 ))
then
year=$((year + 1))
fi;;
"$p_year")
# Decrement year
if (( year > 1 ))
then
year=$((year - 1))
fi;;
"$n_month")
# Increment month
if (( "$month" == 12 )) && (( "$year" < 9999 ))
then
month=1
year=$((year + 1))
else
month=$((month + 1))
fi;;
"$p_month")
# Decrement month
if (( "$month" == 1 )) && (( "$year" > 1 ))
then
month=12
year=$((year - 1))
else
month=$((month - 1))
fi;;
esac
done
clear
# Todo:
# - config file to customize key bindings
# - add number modifier
# - use getopt to accept other options of cal