-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdodecaphony
executable file
·253 lines (235 loc) · 7.08 KB
/
dodecaphony
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
#!/bin/bash
source ~/Repositories/scripts/essential-functions
function load-global-variables {
dx_day0="2025-02-09"
dx_day1="2025-02-10"
zhou_chars=(z 刊)
trithing_chars=(t 义 而 森)
dodec_chars=(d 刃 技 凰 兴 琴 从 查 刷 汇 讴 叫 汝 云 罡 功 更 红 绕 幺 套 织 韭 飘 贝 庶 忠 郭 仍 存 幽)
ram_chars=(r 闯 岸 豹 斐 顶 卫 田 审 启 剀 矛 匝)
zhou_color='38;2;80;118;148'
trithing_color='38;2;69;72;206'
dodec_color='38;2;93;205;217'
ram_color='38;2;254;248;236'
}
function load-math-functions {
function math-ceiling {
perl -w -e "use POSIX; print ceil($1), qq{\n}"
}
function math-floor {
perl -w -e "use POSIX; print floor($1), qq{\n}"
}
}
function test-date-validity {
datetest --isvalid "$@" || return 1
datetest "$dx_day0" --ot "$@" || return 1
}
function format-dx-string {
[[ -z "$3" ]] && return 1
if [[ -z "$4" ]]
then
printf "%.2d.%.2d.%.2d" "$1" "$2" "$3"
else
printf "%.2d.%.1d.%.2d.%.2d" "$1" "$2" "$3" "$4"
fi
}
function gx-dx-convert {
# converts the date from Georgian to Dodecaphony
test-date-validity "$1" || return 1
local rams_since_day0="$(datediff "$dx_day0" "$1")"
# basically epoch for dx
local ram_in_zhou="$(((rams_since_day0 - 1 ) % 360 + 1))"
# {1..360}
local zhous_since_day0="$((( rams_since_day0 - 1 ) / 360 + 1))" # bash can't do float, basically floor function
# z1 begins on dx_day1
local trithing_in_zhou="$(((ram_in_zhou - 1 ) / 120 + 1))"
# {1..3}
local dodec_in_zhou="$(((ram_in_zhou - 1 ) / 12 + 1))"
# {1..30}
local ram_in_dodec="$(((ram_in_zhou - 1 ) % 12 + 1 ))"
# {1..12}
[[ "$2" = "4" || -z "$2" ]] && {
format-dx-string "$zhous_since_day0" "$trithing_in_zhou" "$dodec_in_zhou" "$ram_in_dodec"
return 0
}
[[ "$2" = "3" ]] && {
format-dx-string "$zhous_since_day0" "$dodec_in_zhou" "$ram_in_dodec"
return 0
}
printf "$(sed "s/%z/$zhous_since_day0/g;s/%t/$trithing_in_zhou/g;s/%d/$dodec_in_zhou/g;s/%r/$ram_in_dodec/g" <<< "$2")"
}
function color-number {
[[ -z "$1" || -z "$2" ]] && return 1
case "$1" in
z) printf "\033[${zhou_color}m%.2d" "${2##0}";;
t) printf "\033[${trithing_color}m%.1d" "${2##0}";;
d) printf "\033[${dodec_color}m%.2d" "${2##0}";;
r) printf "\033[${ram_color}m%.2d" "${2##0}";;
*) return 1 ;;
esac
printf '\033[0m'
}
function test-valid-dx-string {
[[ -z "$@" ]] && return 1
grep -q '\.' <<< "$@" || return 1
grep -q '/' <<< "$@" && return 1
grep -q '[a-zA-Z ]' <<< "$@" && return 1
[[ "$(printf "$@" | wc -c)" -gt 10 ]] && return 1
[[ "$(printf "$@" | wc -c)" -lt 8 ]] && return 1
[[ "$(printf "$@" | sed 's/[^.]//g' | wc -c)" -gt 3 ]] && return 1
[[ "$(printf "$@" | sed 's/[^.]//g' | wc -c)" -lt 2 ]] && return 1
:
}
function no-of-fields-in-dx-string {
[[ -z "$@" ]] && return 1
test-valid-dx-string "$@" || return 1
printf "$@" | awk -F '.' '{print NF}'
}
function extract-from-dx-string {
[[ -z "$1" || -z "$2" ]] && return 1
test-valid-dx-string "$2" || return 1
if [[ "$(no-of-fields-in-dx-string "$2")" -eq 4 ]]
then
case "$1" in
z) cut -d '.' -f 1 <<< "$2";;
t) cut -d '.' -f 2 <<< "$2";;
d) cut -d '.' -f 3 <<< "$2";;
r) cut -d '.' -f 4 <<< "$2";;
*) return 1 ;;
esac
else
case "$1" in
z) cut -d '.' -f 1 <<< "$2";;
d) cut -d '.' -f 2 <<< "$2";;
r) cut -d '.' -f 3 <<< "$2";;
*) return 1 ;;
esac
fi
}
function truncate-t-from-dx-string {
local ex_z="$(extract-from-dx-string z "$@")"
local ex_d="$(extract-from-dx-string d "$@")"
local ex_r="$(extract-from-dx-string r "$@")"
printf "$ex_z.$ex_d.$ex_r"
}
function test-valid-dx-string-values {
:
}
function key-parser {
[[ -z "$@" ]] && return 1
keys=(keys ${@/:/ })
}
function give-char {
[[ -z "$2" || -z "$1" ]] && return 1
local char_index="$(printf '%.d' "${2##0}")"
key-parser "$3"
for j in ${keys[@]}
do
case "$j" in
"keys") : ;;
"color") local zhou_color_sequence="\033[${zhou_color}m"
local trithing_color_sequence="\033[${trithing_color}m"
local dodec_color_sequence="\033[${dodec_color}m"
local ram_color_sequence="\033[${ram_color}m";;
*) : ;;
esac
done
case "$1" in
z) printf "$zhou_color_sequence${zhou_chars[$char_index]:-甚}";;
t) printf "$trithing_color_sequence${trithing_chars[$char_index]:-甚}";;
d) printf "$dodec_color_sequence${dodec_chars[$char_index]:-甚}";;
r) printf "$ram_color_sequence${ram_chars[$char_index]:-甚}";;
*) return 1 ;;
esac
printf '\033[0m'
}
function give-chars-for-dx-string {
[[ -z "$1" ]] && return 1
key-parser "$2"
for j in ${keys[@]}
do
case "$j" in
"keys") : ;;
"color") local passkey+=":color";;
"no_t") local passkey+=":no_t";;
*) : ;;
esac
done
local ch_z="$(give-char z $(extract-from-dx-string z "$1" "$passkey"))"
[[ "$(no-of-fields-in-dx-string "$2")" -eq 3 ]] || {
local ch_t="$(give-char t $(extract-from-dx-string t "$1" "$passkey"))"
}
local ch_d="$(give-char d $(extract-from-dx-string d "$1" "$passkey"))"
local ch_r="$(give-char r $(extract-from-dx-string r "$1" "$passkey"))"
printf "$ch_z$ch_t$ch_d$ch_r"
}
function command-option {
function field {
cut -d ' ' -f "$2" <<< "$1"
}
case "$(field "$@" 1)" in
"today")
gx-dx-convert "$(date +"%Y-%m-%d")" || exit 1
echo
;;
"today-char")
give-chars-for-dx-string "$(gx-dx-convert "$(date +"%Y-%m-%d")")" || exit 1
echo
;;
"pretty")
test-date-validity "$(field "$@" 2)" || return 1
printf "\033[${zhou_color}m∎\033[${trithing_color}m∎\033[${dodec_color}m∎\033[${ram_color}m∎\033[0m "
color-number z "$(gx-dx-convert "$(field "$@" 2)" %z)"
printf .
color-number t "$(gx-dx-convert "$(field "$@" 2)" %t)"
printf .
color-number d "$(gx-dx-convert "$(field "$@" 2)" %d)"
printf .
color-number r "$(gx-dx-convert "$(field "$@" 2)" %r)"
printf ' '
give-chars-for-dx-string "$(gx-dx-convert "$(field "$@" 2)" 4)" color
printf '\n'
;;
"to-dx")
gx-dx-convert "$(field "$@" 2)" "$(field "$@" 3)" || exit 1
echo
;;
"to-dx-char")
local dx_date="$(gx-dx-convert "$(field "$@" 2)")"
give-chars-for-dx-string "$dx_date" "$(field "$@" 3)" || exit 1
echo
;;
"to-dx-no-y")
local dx_date="$(gx-dx-convert "$(field "$@" 2)")"
truncate-t-from-dx-string "$dx_date" || exit 1
echo
;;
"to-dx-no-t-char")
local dx_date="$(gx-dx-convert "$(field "$@" 2)")"
local dx_date="$(truncate-t-from-dx-string "$dx_date")"
give-chars-for-dx-string "$dx_date" || exit 1
echo
;;
"test")
color-number r 3
echo
;;
*)
echo ":: Unknown command."
exit 1
;;
esac
}
load-global-variables
load-math-functions
while getopts 'c:' OPTION; do
case "$OPTION" in
"c") command-option "$OPTARG" ;;
*) echo incorrect input; exit ;;
esac
done
(( $OPTIND == 1 )) && command-option today
# gx-dx-convert "$1"
# echo
# give-chars-for-dx-string $(gx-dx-convert "$1")
# echo