File: circle-parse-eventdump

package info (click to toggle)
libcircle-be-perl 0.173320-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 384 kB
  • sloc: perl: 6,042; makefile: 4; sh: 1
file content (67 lines) | stat: -rwxr-xr-x 1,574 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;
use warnings;

use File::Slurp qw( slurp );
use Getopt::Long;
use POSIX qw( strftime );
use Text::Balanced qw( extract_bracketed );
use YAML qw( LoadFile );

GetOptions(
   'timestamp|t=s' => \(my $TIMESTAMP = "%Y/%m/%d %H:%M:%S"),
   'theme=s' => \(my $THEME = "../circle-fe-term/share/circle-fe-term.theme"), # TODO
) or exit 1;

my $filename = shift @ARGV; defined $filename or die "Require a filename\n";

my %theme;
{
   foreach ( slurp $THEME ) {
      next unless m/^(.*?)=(.*)$/;
      $theme{$1} = $2;
   }
}

my $events = LoadFile( $filename );

foreach my $ev ( @$events ) {
   my ( $type, $time, $args ) = @$ev;
   my $timestamp = strftime $TIMESTAMP, localtime $time;

   my $template = $theme{$type} or (print "<<unrecognised event $type>>\n"), next;

   my $text = process( $template, $args );

   print "$timestamp: $text\n";
}

sub process
{
   my ( $template, $args ) = @_;

   my $ret = "";
   while( length $template ) {
      if( $template =~ s/^\$(\w+)// ) {
         my $val = $args->{$1};
         my @parts = ref $val eq "ARRAY" ? @$val : ( $val );
         foreach my $part ( @parts ) {
            $ret .= ref $part eq "ARRAY" ? $part->[0] : $part;
         }
      }
      elsif( $template =~ m/^{/ ) {
         my $piece = extract_bracketed( $template, "{}" );
         s/^{//, s/}$// for $piece;

         my ( $code, $content ) = split( m/ /, $piece, 2 );
         $ret .= process( $content, $args );
      }
      else {
         $template =~ s/^([^\$\{]+)//;
         $ret .= $1;
      }
   }

   return $ret;
}