-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuni_convert.pl
executable file
·265 lines (206 loc) · 5.77 KB
/
uni_convert.pl
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
#!/usr/bin/perl -CDAS
use v5.32;
use strict;
use warnings;
use utf8;
use charnames qw/ :full /;
use List::Util qw/ max /;
use Getopt::Long;
use Term::ANSIColor;
use subs qw/ r /;
#-----------------------------------------------------------------------
# SUBS
#-----------------------------------------------------------------------
# Input.
sub define_spec {
{
"name" => {
desc => "Input is a name.",
exclusive => 1,
},
"string" => {
desc => "Input is a string.",
exclusive => 1,
},
"num" => {
desc => "Input is a number.",
exclusive => 1,
},
"help" => {
desc => "Show this help section.",
},
"debug" => {
desc => "Show debugging information.",
},
"list_options" => {
desc => "List available options.",
},
}
}
sub build_spec_names {
keys define_spec()->%*;
}
sub build_exclusive_options {
my $spec = define_spec();
grep { $spec->{$_}{exclusive} } keys %$spec;
}
sub list_options {
say for sort
map {
s/ (?=^\w{2,}) /--/x; # Long options.
s/ (?=^\w$) /-/x; # Short options.
$_;
}
map { split /\|/, $_ } build_spec_names();
exit 1;
}
sub build_help_options {
my $spec = define_spec();
my $indent = " " x 6;
join "\n$indent", map {
my $opt = join ", ", map {
s/ (?=^\w{2,}) /--/x; # Long options.
s/ (?=^\w$) /-/x; # Short options.
$_;
}
split /\|/, $_;
sprintf "%-20s %s", $opt, $spec->{$_}{desc};
} sort keys %$spec;
}
sub show_help {
my $self = colored( "uni_convert", "YELLOW" );
my $options = build_help_options();
say <<~HERE;
$self [options]
Options:
$options
HERE
exit 1;
}
sub get_options {
my $opts = {};
GetOptions( $opts, build_spec_names() ) or die $!;
if ( $opts->{debug} ) {
r {
opts => $opts,
ARGV => \@ARGV,
};
}
list_options() if $opts->{list_options};
my @exclusive_opts = build_exclusive_options();
show_help()
if not keys %$opts
or $opts->{help}
or 1 != grep { defined } @$opts{@exclusive_opts}
or not @ARGV;
$opts;
}
sub get_input {
my $have_empty_input = grep { $_ eq "" } @ARGV;
bad_input( "" ) if $have_empty_input;
@ARGV;
}
# Error reporting.
sub bad_input {
_bad_x( "input", @_ );
}
sub bad_name {
_bad_x( "name", @_ );
}
sub _bad_x {
my ( $what, $arg ) = @_;
die colored( "Invalid $what: ", "YELLOW" )
. colored( "'$arg'", "RED" ) . "\n\n";
}
# Unicode conversions.
sub get_name {
my ( $opts, $in ) = @_;
my $name = do {
if ( $opts->{name} ) { uc $in }
elsif ( $opts->{string} ) { string2name( $in ) }
elsif ( $opts->{num} ) { code2name( $in ) }
else { show_help() }
};
if ( not defined $name ) {
die colored( "Name cannot be determined from: ", "YELLOW" )
. colored( $in, "RED" ) . "\n\n";
}
$name;
}
sub name_2_line {
my ( $name ) = @_;
my $string = name2string( $name ) // bad_name( $name );
my $uni = name2uni( $name );
my $hex = name2hex( $name );
my $dec = name2dec( $name );
my $oct = name2oct( $name );
# Make white space and escapes easier to see.
$string =~ s/[\s\p{xPosixCntrl}]/ /g;
[ $string, $name, $uni, $hex, $dec, $oct ];
}
sub string2name {
charnames::viacode ord shift;
} # ❄ -> SNOWFLAKE
sub code2name {
my ( $num ) = @_;
$num =~ s/^\\/0/;
$num = oct( $num ) if $num =~ /^0/;
charnames::viacode( $num );
} # U+2744, 0x2744, 10052 -> SNOWFLAKE
sub name2string {
charnames::string_vianame( shift );
} # SNOWFLAKE -> ❄
sub name2uni {
sprintf "U+%x", name2dec( shift );
} # SNOWFLAKE -> U+2744
sub name2hex {
sprintf "%#x", name2dec( shift );
} # SNOWFLAKE -> 0x2744
sub name2dec { charnames::vianame( shift ) } # SNOWFLAKE -> 10052
sub name2oct {
sprintf "%#o", name2dec( shift );
} # SNOWFLAKE -> 023504
# Render.
sub render_lines {
my $max = get_max_length( @_ );
my $format = sprintf define_raw_format(), @$max;
for ( @_ ) {
printf $format, @$_;
}
}
sub define_raw_format {
colored( "%%%ss", "RED" ) . " "
. colored( "%%-%ss", "YELLOW" ) . " "
. colored( "%%-%ss %%-%ss %%%ss %%%ss", "ON_BRIGHT_BLACK" ) . "\n";
}
sub get_max_length {
my $last_row = $#_;
my $last_column = $_[0]->$#*;
my @max = map {
my $col = $_;
max map { length $_[$_][$col]; } 0 .. $last_row;
} 0 .. $last_column;
\@max;
}
# Debug.
sub r {
require Data::Dumper;
say Data::Dumper->new( [@_] )->Terse( 1 )->Indent( 1 )->Sortkeys( 1 )->Dump;
}
#-----------------------------------------------------------------------
# MAIN
#-----------------------------------------------------------------------
say "";
my $opts = get_options();
my ( $input ) = get_input();
my @process = $opts->{string} ? ( split //, $input ) : ( $input );
my @lines;
for ( @process ) {
my $name = get_name( $opts, $_ ); # Normalize to a name.
push @lines, name_2_line( $name ); # Build other parts.
}
render_lines @lines if @lines; # Format and render.
say "";
#-----------------------------------------------------------------------
# END
#-----------------------------------------------------------------------