-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
echo-sd
executable file
·1684 lines (1535 loc) · 38.6 KB
/
echo-sd
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
## -*- encoding: utf-8 -*- vim:tabstop=8:shiftwidth=2
##
## _人人人人人人_
## > 突然の死 < ジェネレーター (Echo "sudden death" message)
##  ̄Y^Y^Y^Y^Y^Y ̄
## Copyright (C) 2013-2022 SATOH Fumiyasu @ OSSTech Corp., Japan
## <https://github.com/fumiyas/home-commands/blob/master/echo-sd>
## <https://fumiyas.github.io/>
## <https://www.osstech.co.jp/cgi-bin/echo-sd>
##
## License: GNU General Public License version 3
##
## Requirements:
## * bash(1) 4.0, ksh(1) 93u or zsh(1) 4.3
## * openssl(1) 1.0.2 if you use Twitter
##
## How to install:
##
## $ mkdir -p $HOME/bin
## $ cd $HOME/bin
## $ wget -nv https://raw.githubusercontent.com/fumiyas/home-commands/master/echo-sd
## $ chmod +x echo-sd
## $ export PATH="$HOME/bin:$PATH"
## $ alias echo=echo-sd
## $ alias banner=echo-sd
##
## Examples for Command-line mode:
##
## $ echo-sd --help
## ...
## $ echo-sd
## _人人人人人人_
## > 突然の死 <
##  ̄Y^Y^Y^Y^Y^Y ̄
## $ echo-sd -t
## (Tweet message in Twitter)
## $ echo-sd ぬるぽっ!!
## _人人人人人人人_
## > ぬるぽっ!! <
##  ̄Y^Y^Y^Y^Y^Y^Y ̄
## $ echo-sd -v ガッ!
## _人人_
## > ガ <
## > ッ <
## > ! <
##  ̄Y^Y ̄
## $ echo-sd -p1 ズキュウウゥン!!
## _人人人人人人人人人人_
## > <
## > ズキュウウゥン!! <
## > <
##  ̄Y^Y^Y^Y^Y^Y^Y^Y^Y^Y ̄
## $ echo-sd -vp1 ゴゴゴゴゴゴゴゴ 「世界」ッ! 時よ止まれ!
## _人人人人人_
## > <
## > 時 ┐ ゴ <
## > よ 世 ゴ <
## > 止 界 ゴ <
## > ま └ ゴ <
## > れ ッ ゴ <
## > ! ! ゴ <
## > ゴ <
## > ゴ <
## > <
##  ̄Y^Y^Y^Y^Y ̄
##
## Examples for CGI mode:
##
## $ GATEWAY_INTERFACE=CGI/1.0 QUERY_STRING=s=おっす!\&o=v echo-sd
## Content-Type: text/html; charset=UTF-8
## ...
## <pre>
## _人人_
## > お <
## > っ <
## > す <
## > ! <
##  ̄Y^Y ̄
## </pre><hr>
## ...
##
## On-line demo site:
##
## https://www.OSSTech.co.jp/cgi-bin/echo-sd
##
## TODO:
##
## * --rest (REST IN PEACE)
## * --center with --vertical
##
## Inspired by:
##
## * 突然の死ジェネレータ - powered by starwing.net, created by @karno.
## http://starwing.net/suddenly_death.html
## * 突然の死ジェネレータ - 純粋関数空間
## http://tanakh.jp/tools/sudden.html
## * 元ネタ
## https://dic.nicovideo.jp/a/%E7%AA%81%E7%84%B6%E3%81%AE%E6%AD%BB
##
if [[ ${0##*/} == echo-sd ]] && [[ ${zsh_eval_context-toplevel} == toplevel ]]; then
set -u
case "${BASH_VERSION-}" in
[1-3].*)
if type zsh >/dev/null 2>&1; then
unset BASH_VERSION
exec zsh "$0" ${1+"$@"}
exit 1
fi
echo "${0##*/}: ERROR: bash 4 or later required" 1>&2
exit 1
;;
esac
fi
typeset SD_arg0="$0"
typeset SD_copyright='(C) 2013-2022 SATOH Fumiyasu @ OSSTech Corp., Japan'
typeset SD_url='https://github.com/fumiyas/home-commands/blob/master/echo-sd'
typeset SD_lang_orig SD_lang
typeset SD_bc_tl SD_bc_t SD_bc_tr
typeset SD_bc_l SD_bc_r
typeset SD_bc_bl SD_bc_b SD_bc_br
typeset -A SD_vc_map
typeset SD_c_lf='
'
## ======================================================================
function SD_die {
SD_echo_command "${SD_arg0##*/}: ERROR: $1" 1>&2
SD_echo_command 1>&2
exit "${2-1}"
}
function SD_init {
if [[ -n ${ZSH_VERSION-} ]]; then
setopt SH_WORD_SPLIT
setopt BSD_ECHO
setopt KSH_GLOB
setopt KSH_ARRAYS
elif [[ -n ${BASH_VERSION-} ]]; then
shopt -u xpg_echo
else ## ksh
if [[ $(echo -n) == -n ]]; then
alias echo='print -r'
fi
fi
typeset locale lang_ja lang
SD_lang_orig="${LANG-}"
if [[ ${SD_lang_orig#*.} != @(UTF-8|utf-8|UTF8|utf8) ]]; then
if type locale >/dev/null 2>&1; then
while read -r locale; do
if [[ ${locale#*.} == @(UTF-8|utf-8|UTF8|utf8) ]]; then
if [[ ${locale%.*} == ja_JP ]]; then
lang_ja="$locale"
else
lang="$locale"
fi
fi
done < <(locale -a)
fi
fi
SD_lang="${lang_ja-${lang-ja_JP.UTF-8}}"
SD_bc_tl="_"
SD_bc_t="人"
SD_bc_tr="_"
SD_bc_l=">"
SD_bc_r="<"
SD_bc_bl=" ̄"
SD_bc_b="Y^"
SD_bc_br=" ̄"
if [[ -n ${ZSH_VERSION-} ]]; then
SD_vc_map=()
else
SD_vc_map=
fi
SD_vc_map[↑]=' → '
SD_vc_map[↓]=' ← '
SD_vc_map[←]=' ↑ '
SD_vc_map[→]=' ↓ '
SD_vc_map[。]=' ︒ '
SD_vc_map[、]=' ︑ '
SD_vc_map[ー]=' | '
SD_vc_map[─]=' | '
SD_vc_map[−]=' | '
SD_vc_map[-]=' | '
SD_vc_map[〜]=' ∫ '
SD_vc_map[~]=' ∫ '
SD_vc_map[/]=' \ '
SD_vc_map[…]=' ︙ '
SD_vc_map[‥]=' ︰ '
SD_vc_map[︙]=' … '
SD_vc_map[:]=' ‥ '
SD_vc_map[:]=' ‥ '
SD_vc_map[=]=' ॥ '
SD_vc_map[=]=' ॥ '
SD_vc_map[(]=' ⏜ '
SD_vc_map[\(]=' ⏜ '
SD_vc_map[)]=' ⏝ '
SD_vc_map[\)]=' ⏝ '
SD_vc_map[[]=' ⎴ '
SD_vc_map[\[]=' ⎴ '
SD_vc_map[]]=' ⎵ '
SD_vc_map[\]]=' ⎵ '
SD_vc_map[{]=' ⏞ '
SD_vc_map[\{]=' ⏞ '
SD_vc_map[<]=' ∧ '
SD_vc_map[\<]=' ∧ '
SD_vc_map[>]=' ∨ '
SD_vc_map[\>]=' ∨ '
SD_vc_map[}]=' ⏟ '
SD_vc_map[\}]=' ⏟ '
SD_vc_map[「]=' ┐'
SD_vc_map[」]='└ '
SD_vc_map[【]=' ︗ '
SD_vc_map[】]=' ︘ '
SD_vc_map[〖]=' ︗ '
SD_vc_map[〗]=' ︘ '
SD_vc_map[「]=' ┐'
SD_vc_map[」]='└ '
SD_vc_map[-]=' | '
SD_vc_map[ー]=' | '
SD_vc_map[,]=" ' "
SD_vc_map[、]=' ` '
}
function _SD_fill_string_by_char {
typeset str char
str="$1"; shift
char="$1"; shift
## Why bash on ja_JP.UTF-8 locale matches '[¥]' with '︙' (and others?)
#str="${str//[ -~。-゚¢£¥¦¬¯]/$char}"
str="${str//[ -~。-゚¢£¦¬¯]/$char}"
str="${str//¥/$char}"
str="${str//[! ]/$char$char}"
echo "$str"
}
function _SD_pad_space {
printf "%${1}s" ''
}
function _SD_string_width {
typeset str
str=$(_SD_fill_string_by_char "$1" ' ')
echo "${#str}"
}
function _SD_echo_with_padding {
typeset str width
str="$1"; shift
width="$1"; shift
echo "${str}$(_SD_pad_space "$((width-$(_SD_string_width "${1-$str}")))")"
}
function SD_echo_header {
typeset opt
typeset round="set"
typeset bc_t="$SD_bc_t" bc_tl="$SD_bc_tl" bc_tr="$SD_bc_tr" bc_tc=""
typeset bc_l="$SD_bc_l" bc_r="$SD_bc_r"
while [[ $# -gt 0 ]]; do
opt="$1"; shift
case "$opt" in
--no-round)
round=""
;;
--bc-[tlr]|--bc-t[clr])
if [[ ! -n ${1+set} ]]; then
SD_die "Option value required: $opt"
fi
eval "bc_${opt##*-}=\"\$1\""
shift
;;
--)
break
;;
-*)
SD_die "Invalid option: $opt"
;;
*)
set -- "$opt" ${1+"$@"}
break
;;
esac
done
typeset width="$1"; shift
typeset padding="$1"; shift
typeset template left=
typeset -a paddings=()
if [[ -n ${round-} && $width -ge 2 && $padding -gt 0 ]]; then
left=" "
let width--
else
left=""
fi
template=$(_SD_pad_space "$width")
while [[ $padding -gt 0 ]]; do
if [[ -n ${round-} ]] && [[ $width -ge 2 ]]; then
template="${template#?}"
let width--
fi
paddings=(
"$left$bc_l ${template// / } $bc_r"
${paddings[@]+"${paddings[@]}"}
)
if [[ -n ${round-} ]] && [[ $width -ge 2 ]]; then
template="${template#?}"
left="$left "
let width--
fi
let padding--
done
typeset top
if [[ -n $bc_tc ]]; then
width=$(_SD_string_width "$template")
template="$(_SD_pad_space $((width / 2)))"
top="${template// /$bc_t}$bc_tc${template// /$bc_t}"
else
template+=" "
top="${template// /$bc_t}"
fi
echo "${left// / }$bc_tl${top// /}$bc_tr"
for padding_s in ${paddings[@]+"${paddings[@]}"}; do
echo "${padding_s// / }"
done
}
function SD_echo_footer {
typeset opt
typeset round="set"
typeset bc_b="$SD_bc_b" bc_bl="$SD_bc_bl" bc_br="$SD_bc_br"
typeset bc_l="$SD_bc_l" bc_r="$SD_bc_r"
while [[ $# -gt 0 ]]; do
opt="$1"; shift
case "$opt" in
--no-round)
round=""
;;
--bc-[blr]|--bc-b[lr])
if [[ ! -n ${1+set} ]]; then
SD_die "Option value required: $opt"
fi
eval "bc_${opt##*-}=\"\$1\""
shift
;;
--)
break
;;
-*)
SD_die "Invalid option: $opt"
;;
*)
set -- "$opt" ${1+"$@"}
break
;;
esac
done
typeset width="$1"; shift
typeset padding="$1"; shift
typeset template left=
if [[ -n ${round-} && $width -ge 2 && $padding -gt 0 ]]; then
left=" "
let width--
fi
template=$(_SD_pad_space "$width")
while [[ $padding -gt 0 ]]; do
if [[ -n ${round-} ]] && [[ $width -ge 2 ]]; then
template="${template#?}"
let width--
fi
echo "${left// / }$bc_l ${template// / } $bc_r"
if [[ -n ${round-} ]] && [[ $width -ge 2 ]]; then
template="${template#?}"
left="$left "
fi
let padding--
done
template+=" " ## FIXME: Support --bc-bc
typeset bottom
bottom="${template// /$bc_b}"
echo "${left// / }$bc_bl${bottom// /}$bc_br"
}
function SD_echo_horizontal {
typeset opt
typeset round
typeset bc_tl bc_t bc_tr bc_l bc_r bc_bl bc_b bc_br
typeset padding_top=0 padding_bottom=0 padding_left=0 padding_right=0
typeset center
while [[ $# -gt 0 ]]; do
opt="$1"; shift
case "$opt" in
--no-round)
no_round="set"
;;
--bc-[tblr]|--bc-t[lr]|--bc-b[lr])
if [[ ! -n ${1+set} ]]; then
SD_die "Option value required: $opt"
fi
eval "bc_${opt##*-}=\"\$1\""
shift
;;
--padding)
if [[ ${1-BAD} == @(*[!0-9]*) ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
padding_top="$((($1 + 1) / 2))"
padding_bottom="$padding_top"
padding_left="$1"
padding_right="$1"
shift
;;
--padding-top|--padding-bottom|--padding-left|--padding-right)
if [[ ${1-BAD} == @(*[!0-9]*) ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
case "$opt" in
--padding-top)
padding_top="$1"
;;
--padding-bottom)
padding_bottom="$1"
;;
--padding-left)
padding_left="$1"
;;
--padding-right)
padding_right="$1"
;;
esac
shift
;;
--center)
center="set"
;;
--)
break
;;
-*)
SD_die "Invalid option: $opt"
;;
*)
set -- "$opt" ${1+"$@"}
break
;;
esac
done
export LANG="$SD_lang"
typeset -a scripts=()
typeset script width width_tmp
typeset padding_left_s padding_right_s
width=0
padding_left_s=$(_SD_pad_space "$padding_left")
padding_right_s=$(_SD_pad_space "$padding_right")
for script in ${1+"$@"}; do
script="$padding_left_s$script$padding_right_s"
scripts+=("$script")
width_tmp=$(_SD_string_width "$script")
if [[ $width_tmp -gt $width ]]; then
width="$width_tmp"
fi
done
if [[ -n ${center-} && $((width % 2)) -eq 1 ]]; then
let width++
fi
SD_echo_header \
${no_round:+--no-round} \
--bc-tl "${bc_tl:-$SD_bc_tl}" \
--bc-t "${bc_t:-$SD_bc_t}" \
--bc-tr "${bc_tr:-$SD_bc_tr}" \
--bc-l "${bc_l:-$SD_bc_l}" \
--bc-r "${bc_r:-$SD_bc_r}" \
"$width" \
"$padding_top" \
;
typeset line
for script in ${scripts[@]+"${scripts[@]}"}; do
if [[ -n ${center-} ]]; then
typeset padding=$(((width - $(_SD_string_width "$script")) / 2))
if [[ $padding -gt 0 ]]; then
script="$(_SD_pad_space "$padding")$script"
fi
fi
line="${bc_l:-$SD_bc_l} $(_SD_echo_with_padding "${script}" "$width") ${bc_r:-$SD_bc_r}"
echo "${line// / }"
done
SD_echo_footer \
${no_round:+--no-round} \
--bc-bl "${bc_bl:-$SD_bc_bl}" \
--bc-b "${bc_b:-$SD_bc_b}" \
--bc-br "${bc_br:-$SD_bc_br}" \
--bc-l "${bc_l:-$SD_bc_l}" \
--bc-r "${bc_r:-$SD_bc_r}" \
"$width" \
"$padding_bottom" \
;
export LANG="$SD_lang_orig"
}
function SD_echo_vertical {
typeset opt
typeset no_round
typeset bc_tc bc_tl bc_t bc_tr bc_l bc_r bc_bl bc_b bc_br
typeset padding_top=0 padding_bottom=0 padding_left=0 padding_right=0
typeset center
typeset tanzaku
while [[ $# -gt 0 ]]; do
opt="$1"; shift
case "$opt" in
--no-round)
no_round="set"
;;
--bc-[tblr]|--bc-t[clr]|--bc-b[lr])
if [[ ! -n ${1+set} ]]; then
SD_die "Option value required: $opt"
fi
eval "bc_${opt##*-}=\"\$1\""
shift
;;
--padding)
if [[ ${1-BAD} == @(*[!0-9]*) ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
padding_top="$((($1 + 1) / 2))"
padding_bottom="$padding_top"
padding_left="$1"
padding_right="$1"
shift
;;
--padding-top|--padding-bottom|--padding-left|--padding-right)
if [[ ${1-BAD} == @(*[!0-9]*) ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
case "$opt" in
--padding-top)
padding_top="$1"
;;
--padding-bottom)
padding_bottom="$1"
;;
--padding-left)
padding_left="$1"
;;
--padding-right)
padding_right="$1"
;;
esac
shift
;;
--center)
center="set"
;;
--tanzaku)
tanzaku="set"
;;
--)
break
;;
-*)
SD_die "Invalid option: $opt"
;;
*)
set -- "$opt" ${1+"$@"}
break
;;
esac
done
export LANG="$SD_lang"
## FIXME: Implement --center option
typeset -a lines=()
typeset line line_n
typeset script script_n trailer trailer2 letter letter_width next
letter_width=4
script_n=0
for script in ${1+"$@"}; do
line_n=0
trailer="$script"
while [[ -n $script ]]; do
trailer="${trailer#?}"
letter="${script%$trailer}"
script="${script#?}"
if [[ -n ${SD_vc_map[$letter]-} ]]; then
line="${SD_vc_map[$letter]}"
else
trailer2="${trailer#?}"
next="${script%$trailer2}"
if [[ $next == @(゙|゚) ]] || [[ $letter$next == @([\?!][\?!]) ]]; then
line=" $letter$next "
trailer="$trailer2"
script="${script#?}"
else
line=" ${letter} "
fi
fi
if [[ -z ${lines[$line_n]-} ]]; then
lines[$line_n]="$(_SD_pad_space $((script_n * letter_width)))"
fi
lines[$line_n]="$(_SD_echo_with_padding "${line}" $letter_width)${lines[$line_n]}"
let line_n+=1
done
while [[ $line_n -lt ${#lines[@]} ]]; do
lines[$line_n]="$(_SD_pad_space $letter_width)${lines[$line_n]}"
let line_n+=1
done
let script_n+=1
done
typeset line_width=$(((script_n - 1) * letter_width))
let line_width+=$padding_left+$padding_right
typeset padding_left_s padding_right_s
padding_left_s=$(_SD_pad_space "$padding_left")
padding_right_s=$(_SD_pad_space "$padding_right")
SD_echo_header \
${no_round:+--no-round} \
${bc_tc:+--bc-tc "$bc_tc"} \
--bc-tl "${bc_tl:-$SD_bc_tl}" \
--bc-t "${bc_t:-$SD_bc_t}" \
--bc-tr "${bc_tr:-$SD_bc_tr}" \
--bc-l "${bc_l:-$SD_bc_l}" \
--bc-r "${bc_r:-$SD_bc_r}" \
"$line_width" \
"$padding_top" \
;
for line in ${lines[@]+"${lines[@]}"}; do
line="$padding_left_s$line$padding_right_s"
echo "${bc_l:-$SD_bc_l}${line// / }${bc_r:-$SD_bc_r}"
done
SD_echo_footer \
${no_round:+--no-round} \
--bc-bl "${bc_bl:-$SD_bc_bl}" \
--bc-b "${bc_b:-$SD_bc_b}" \
--bc-br "${bc_br:-$SD_bc_br}" \
--bc-l "${bc_l:-$SD_bc_l}" \
--bc-r "${bc_r:-$SD_bc_r}" \
"$line_width" \
"$padding_bottom" \
;
export LANG="$SD_lang_orig"
}
function SD_echo_command_help {
{
SD_bc_r="< ジェネレーター" SD_echo_command
cat <<EOT
Usage: ${SD_arg0##*/} [OPTIONS] [SCRIPT ...]
Options:
--help
Print this message
-v, --vertical
Print scripts vertically
-t, --tweet
Tweet in Twitter
-h, --header HEADER
-f, --footer FOOTER
Header and footer script(s)
-s, --read-stdin
Read scripts from stdin
--stress
"Stress" mode
--stream
Stream mode
-b, --border-char CHARACTER
Border character
--tanzaku
"Tanzaku" border
--center
Center scripts (not implemented in vertical mode yet)
-R, --no-round
Do not round border
--square
Square border
-p, --padding SIZE
--pt, --padding-top SIZE
--pb, --padding-bottom SIZE
--pl, --padding-left SIZE
--pr, --padding-right SIZE
Padding size
EOT
} |SD_echo_command --read-stdin
}
function SD_echo_command {
typeset opt
typeset read_stdin vertical stress stream
typeset no_round square tanzaku border_char
typeset padding padding_top padding_bottom padding_left padding_right
typeset center
typeset header= footer=
typeset tweet
while [[ $# -gt 0 ]]; do
opt="$1"; shift
## -ovalue → -o value
if [[ -z "${opt##-[bphf]?*}" ]]; then
set -- "${opt#??}" ${1+"$@"}
opt="${opt%$1}"
## -abc → -a -bc
elif [[ -z "${opt##-[!-]?*}" ]]; then
set -- "-${opt#??}" ${1+"$@"}
opt="${opt%${1#-}}"
## --option=value → --option value
elif [[ -z "${opt##--*=*}" ]]; then
set -- "${opt#--*=}" ${1+"$@"}
opt="${opt%%=*}"
fi
case "$opt" in
--help)
SD_echo_command_help
exit 0
;;
-s|--read-stdin)
read_stdin="set"
stream=""
;;
-v|--vertical)
vertical="set"
;;
--stress)
stress="set"
;;
--stream)
stream="set"
read_stdin=""
;;
-R|--no-round)
no_round="set"
;;
-p|--padding)
if [[ ${1-BAD} == @(*[!0-9]*) ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
padding="$1"
shift
;;
--p[tblr]|--padding-top|--padding-bottom|--padding-left|--padding-right)
if [[ ${1-BAD} == @(*[!0-9]*) ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
case "$opt" in
--pt|--padding-top)
padding_top="$1"
;;
--pb|--padding-bottom)
padding_bottom="$1"
;;
--pl|--padding-left)
padding_left="$1"
;;
--pr|--padding-right)
padding_right="$1"
;;
esac
shift
;;
-b|--bc|--border-char|--border-character)
if [[ -z ${1-} ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
border_char="$1"
shift
;;
--square)
square="set"
no_round="set"
;;
--tanzaku)
tanzaku="set"
vertical="set"
no_round="set"
;;
--center)
center="set"
;;
-h|--header)
if [[ -z ${1-} ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
header+="$1"
shift
;;
-f|--footer)
if [[ -z ${1-} ]]; then
SD_die "Invalid option value: $opt ${1-UNSPECIFIED}"
fi
footer+="$1"
shift
;;
-t|--tweet)
tweet="set"
;;
--)
break
;;
-*)
SD_die "Invalid option: $opt"
;;
*)
set -- "$opt" ${1+"$@"}
break
;;
esac
done
typeset -a sd_opts=(
${no_round:+--no-round}
${center:+--center}
${padding:+--padding "$padding"}
${padding_top:+--padding-top "$padding_top"}
${padding_bottom:+--padding-bottom "$padding_bottom"}
${padding_left:+--padding-left "$padding_left"}
${padding_right:+--padding-right "$padding_right"}
)
if [[ -n ${border_char-} ]]; then
sd_opts+=(
--bc-tl "$border_char" --bc-t "$border_char" --bc-tr "$border_char"
--bc-l "$border_char" --bc-r "$border_char"
--bc-bl "$border_char" --bc-b "$border_char" --bc-br "$border_char"
)
elif [[ -n ${square-} ]]; then
sd_opts+=(
--bc-tl '┌' --bc-t '─' --bc-tr '┐'
--bc-l '│' --bc-r '│'
--bc-bl '└' --bc-b '─' --bc-br '┘'
)
elif [[ -n ${tanzaku-} ]]; then
sd_opts+=(
--bc-tc '-┷-'
--bc-tl '┏' --bc-t '━' --bc-tr '┓'
--bc-l '┃' --bc-r '┃'
--bc-bl '┗' --bc-b '━' --bc-br '┛'
)
fi
header="$(echo -e "$header")${header:+$SD_c_lf}"
footer="$(echo -e "$footer")"
if [[ -n ${stress-} ]]; then
header+="${1-仕事のストレス}$SD_c_lf"; ${1+shift}
header+=" ↘$SD_c_lf"
header+=" ${1-仕事のストレス}$SD_c_lf"; ${1+shift}
header+=" ↙$SD_c_lf"
header+="${1-仕事のストレス}$SD_c_lf"; ${1+shift}
header+=" ↘$SD_c_lf"
header+=" ${1-仕事のストレス}$SD_c_lf"; ${1+shift}
header+=" ↙$SD_c_lf"
fi
if [[ -n ${read_stdin-} ]]; then
while IFS= read -r line; do
set -- ${1+"$@"} "$line"
done
fi
if [[ -z ${stream-} ]]; then
if [[ $# -eq 0 ]]; then
if [[ -n ${stress-} ]]; then
set -- '仕事のストレス'
else
set -- '突然の死'
fi
fi
typeset sd="$header"
if [[ -z ${vertical-} ]]; then
sd+=$(SD_echo_horizontal ${sd_opts[@]+"${sd_opts[@]}"} -- ${1+"$@"})
else
sd+=$(SD_echo_vertical ${sd_opts[@]+"${sd_opts[@]}"} -- ${1+"$@"})
fi
sd+="${footer:+$SD_c_lf$footer}"
if [[ -n ${tweet-} ]]; then
if [[ $sd = @( *) ]]; then
sd=".${sd# }"
elif [[ $sd = @( *) ]]; then
sd=".${sd# }"
fi
Tweet_init
Tweet_command "$sd"
else
echo "$sd"
fi
else
echo -n "$header"
while :; do
IFS= read -r line || break
set -- ${1+"$@"} "$line"
if [[ -z ${vertical-} ]]; then
SD_echo_horizontal ${sd_opts[@]+"${sd_opts[@]}"} -- ${1+"$@"}
else
SD_echo_vertical ${sd_opts[@]+"${sd_opts[@]}"} -- ${1+"$@"}
fi
shift $#
done
echo "$footer"
fi
}
function _SD_escape_html {
if [[ -n ${1+set} ]]; then
echo "$1"
else
cat
fi \
|sed \
-e 's/&/\&/g;' \
-e 's/</\</g;' \
-e 's/>/\>/g;' \
;
}
function _SD_urlencode {
## FIXME: Unsafe?
if [[ -n ${1+set} ]]; then
echo "$1"
else
cat
fi \
|sed \
-e 's/%/%25/g;' \
-e 's/ /%20/g;' \
-e 's/#/%23/g;' \
-e 's/&/%26/g;' \
-e 's/+/%2B/g;' \
-e 's/;/%3B/g;' \
-e 's/=/%3D/g;' \
-e 's/?/%3F/g;' \
-e 's/\\/%5C/g;' \
-e 's/\^/%5E/g;' \
-e 's/{/%7B/g;' \
-e 's/|/%7C/g;' \
-e 's/}/%7D/g;' \
-e 's/~/%7E/g;' \
;
}
function _SD_urldecode {
## FIXME: Support ksh
echo -e "$(echo "$1" |sed 's/+/ /g;s/%\(..\)/\\x\1/g;')"
}
function SD_echo_cgi {
typeset query="${QUERY_STRING-}"
typeset param value
typeset tweet text vertical padding square
typeset -a prefaces=()
typeset -a scripts=()
while [[ -n $query ]]; do
param="${query%%&*}"
## '\' is workaround for zsh 4.3.x bug that causes "* not found" error