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