| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use v5.16; | ||
| 6 | use Encode::Bijou64; | ||
| 7 | |||
| 8 | ############################################################################### | ||
| 9 | ############################################################################### | ||
| 10 | |||
| 11 | for my $n (0, 247, 248, 300, 65535, 1234567890, 12039810293801983) { | ||
| 12 | my $enc = encode_bijou64($n); | ||
| 13 | my $dec = decode_bijou64($enc); | ||
| 14 | my $res = color('red', "FAIL"); | ||
| 15 | |||
| 16 | if ($n == $dec) { | ||
| 17 | $res = color('green', ' OK '); | ||
| 18 | } | ||
| 19 | |||
| 20 | printf("%20d => %-20s (%d bytes) => %20d %s\n", $n, unpack("H*", $enc), length($enc), $dec, $res); | ||
| 21 | } | ||
| 22 | |||
| 23 | ############################################################################### | ||
| 24 | ############################################################################### | ||
| 25 | |||
| 26 | # String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue' | ||
| 27 | sub color { | ||
| 28 | my ($str, $txt) = @_; | ||
| 29 | |||
| 30 | if (-t STDOUT == 0 || $ENV{NO_COLOR}) { return $txt // ""; } # No interactive terminal | ||
| 31 | if (!length($str) || $str eq 'reset') { return "\e[0m"; } # No string = RESET | ||
| 32 | |||
| 33 | # Some predefined colors/commands | ||
| 34 | my %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0); | ||
| 35 | my %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7); | ||
| 36 | |||
| 37 | # Pre-process the string. | ||
| 38 | $str =~ s/on_/-/; # "on_" becomes a negative number | ||
| 39 | $str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg; # command number | ||
| 40 | |||
| 41 | my @parts = split("_", $str); | ||
| 42 | foreach my $p (@parts) { | ||
| 43 | my $cmd_num = $cmd_map{$p // 0}; | ||
| 44 | |||
| 45 | if ($cmd_num) { $p = $cmd_num; } | ||
| 46 | elsif (defined($p) && $p =~ /^-(.+)/) { $p = "48;5;$p"; } | ||
| 47 | elsif (defined($p)) { $p = "38;5;$p"; } | ||
| 48 | } | ||
| 49 | |||
| 50 | my $ret = "\e[" . join(";", @parts) . "m"; | ||
| 51 | |||
| 52 | if (defined($txt)) { $ret .= $txt . "\e[0m"; } | ||
| 53 | |||
| 54 | return $ret; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub file_get_contents { | ||
| 58 | open(my $fh, "<", $_[0]) or return undef; | ||
| 59 | binmode($fh, ":encoding(UTF-8)"); | ||
| 60 | |||
| 61 | my $array_mode = ($_[1]) || (!defined($_[1]) && wantarray); | ||
| 62 | |||
| 63 | if ($array_mode) { # Line mode | ||
| 64 | my @lines = readline($fh); | ||
| 65 | |||
| 66 | # Right trim all lines | ||
| 67 | foreach my $line (@lines) { $line =~ s/[\r\n]+$//; } | ||
| 68 | |||
| 69 | return @lines; | ||
| 70 | } else { # String mode | ||
| 71 | local $/ = undef; # Input rec separator (slurp) | ||
| 72 | return my $ret = readline($fh); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | sub file_put_contents { | ||
| 77 | my ($file, $data) = @_; | ||
| 78 | |||
| 79 | open(my $fh, ">", $file) or return undef; | ||
| 80 | binmode($fh, ":encoding(UTF-8)"); | ||
| 81 | print $fh $data; | ||
| 82 | close($fh); | ||
| 83 | |||
| 84 | return length($data); | ||
| 85 | } | ||
| 86 | |||
| 87 | # Creates methods k() and kd() to print, and print & die respectively | ||
| 88 | BEGIN { | ||
| 89 | if (!defined(&trim)) { | ||
| 90 | *trim = sub { | ||
| 91 | my ($s) = (@_, $_); # Passed in var, or default to $_ | ||
| 92 | if (length($s) == 0) { return ""; } | ||
| 93 | $s =~ s/^\s*//; | ||
| 94 | $s =~ s/\s*$//; | ||
| 95 | |||
| 96 | return $s; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | if (eval { require Dump::Krumo }) { | ||
| 101 | Dump::Krumo->import(qw/k kd/); | ||
| 102 | } else { | ||
| 103 | require Data::Dumper; | ||
| 104 | *k = sub { print Data::Dumper::Dumper(\@_) }; | ||
| 105 | *kd = sub { print Data::Dumper::Dumper(\@_); die; }; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | # vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4 |