| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use v5.16; | ||
| 6 | |||
| 7 | use Template::Sluz; | ||
| 8 | |||
| 9 | ############################################################################### | ||
| 10 | ############################################################################### | ||
| 11 | |||
| 12 | my $s = Template::Sluz->new(); | ||
| 13 | |||
| 14 | #print joina(['one', 'two', 'three'], "-"); | ||
| 15 | #exit; | ||
| 16 | |||
| 17 | $s->assign('name' , "Jason"); | ||
| 18 | $s->assign('colors' , ['red','green', 'blue']); | ||
| 19 | $s->assign('data' , { color => 'red', animal => 'kitten', age => 39 }); | ||
| 20 | $s->assign('version', $Template::Sluz::VERSION); | ||
| 21 | $s->assign('color_white', color('bold_white')); | ||
| 22 | $s->assign('color_yellow', color(228)); | ||
| 23 | $s->assign('color_reset', color('reset')); | ||
| 24 | |||
| 25 | #print $s->parse_string('Hello {$name}'); | ||
| 26 | print $s->fetch(Template::Sluz::SLUZ_INLINE); | ||
| 27 | |||
| 28 | print $s->parse_string('HTML: <div>Name: {$name} / Colors: {$colors|join:", "}</div>') . "\n"; | ||
| 29 | |||
| 30 | $s->set_delimiters("<", ">"); | ||
| 31 | print $s->parse_string('JSON: {"name": "<$name>", "animal": "<$data.animal>"}') . "\n"; | ||
| 32 | |||
| 33 | ############################################################################### | ||
| 34 | ############################################################################### | ||
| 35 | |||
| 36 | sub joina { | ||
| 37 | my $arr = shift(); | ||
| 38 | my $glue = shift(); | ||
| 39 | |||
| 40 | my $ret = join($glue, @$arr); | ||
| 41 | |||
| 42 | return $ret; | ||
| 43 | } | ||
| 44 | |||
| 45 | # String format: '115', '165_bold', '10_on_140', 'reset', 'on_173', 'red', 'white_on_blue' | ||
| 46 | sub color { | ||
| 47 | my ($str, $txt) = @_; | ||
| 48 | |||
| 49 | state $notty = !-t STDOUT; # Cache the TTY check | ||
| 50 | if ($notty || $ENV{NO_COLOR}) { return $txt // ""; } # No interactive terminal | ||
| 51 | if (!length($str) || $str eq 'reset') { return "\e[0m"; } # No string = RESET | ||
| 52 | |||
| 53 | # Some predefined colors/commands | ||
| 54 | state %color_map = qw(red 160 blue 27 green 34 yellow 226 orange 214 purple 93 white 15 black 0); | ||
| 55 | state %cmd_map = qw(bold 1 italic 3 underline 4 blink 5 inverse 7); | ||
| 56 | |||
| 57 | # Pre-process the string | ||
| 58 | $str =~ s/on_/-/g; # "on_" becomes a negative number | ||
| 59 | $str =~ s|([A-Za-z]+)|$color_map{$1} // $1|eg; # color name -> number | ||
| 60 | |||
| 61 | my @parts = map { # If it's negative it's a background color, otherwise foreground | ||
| 62 | $cmd_map{$_} // ($_ =~ /^-(.+)/ ? "48;5;$1" : "38;5;$_") | ||
| 63 | } split("_", $str); | ||
| 64 | |||
| 65 | my $ret = "\e[" . join(";", @parts) . "m"; | ||
| 66 | |||
| 67 | if (defined $txt) { $ret .= $txt . "\e[0m"; } | ||
| 68 | |||
| 69 | return $ret; | ||
| 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 | __DATA__ |