| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use File::Slurp qw( slurp ); |
| 7 |
use Getopt::Long; |
| 8 |
use POSIX qw( strftime ); |
| 9 |
use Text::Balanced qw( extract_bracketed ); |
| 10 |
use YAML qw( LoadFile ); |
| 11 |
|
| 12 |
GetOptions( |
| 13 |
'timestamp|t=s' => \(my $TIMESTAMP = "%Y/%m/%d %H:%M:%S"), |
| 14 |
'theme=s' => \(my $THEME = "../circle-fe-term/share/circle-fe-term.theme"), # TODO |
| 15 |
) or exit 1; |
| 16 |
|
| 17 |
my $filename = shift @ARGV; defined $filename or die "Require a filename\n"; |
| 18 |
|
| 19 |
my %theme; |
| 20 |
{ |
| 21 |
foreach ( slurp $THEME ) { |
| 22 |
next unless m/^(.*?)=(.*)$/; |
| 23 |
$theme{$1} = $2; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
my $events = LoadFile( $filename ); |
| 28 |
|
| 29 |
foreach my $ev ( @$events ) { |
| 30 |
my ( $type, $time, $args ) = @$ev; |
| 31 |
my $timestamp = strftime $TIMESTAMP, localtime $time; |
| 32 |
|
| 33 |
my $template = $theme{$type} or (print "<<unrecognised event $type>>\n"), next; |
| 34 |
|
| 35 |
my $text = process( $template, $args ); |
| 36 |
|
| 37 |
print "$timestamp: $text\n"; |
| 38 |
} |
| 39 |
|
| 40 |
sub process |
| 41 |
{ |
| 42 |
my ( $template, $args ) = @_; |
| 43 |
|
| 44 |
my $ret = ""; |
| 45 |
while( length $template ) { |
| 46 |
if( $template =~ s/^\$(\w+)// ) { |
| 47 |
my $val = $args->{$1}; |
| 48 |
my @parts = ref $val eq "ARRAY" ? @$val : ( $val ); |
| 49 |
foreach my $part ( @parts ) { |
| 50 |
$ret .= ref $part eq "ARRAY" ? $part->[0] : $part; |
| 51 |
} |
| 52 |
} |
| 53 |
elsif( $template =~ m/^{/ ) { |
| 54 |
my $piece = extract_bracketed( $template, "{}" ); |
| 55 |
s/^{//, s/}$// for $piece; |
| 56 |
|
| 57 |
my ( $code, $content ) = split( m/ /, $piece, 2 ); |
| 58 |
$ret .= process( $content, $args ); |
| 59 |
} |
| 60 |
else { |
| 61 |
$template =~ s/^([^\$\{]+)//; |
| 62 |
$ret .= $1; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return $ret; |
| 67 |
} |