diff options
| author | Paul Buetow <paul@buetow.org> | 2023-03-19 20:24:35 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-03-19 20:24:35 +0200 |
| commit | 11c36341dca696b3260d8545fab0613204763b06 (patch) | |
| tree | 9204b64dfbb7b6f959239db58c1b8df9bea45a33 | |
| parent | 48abe9533c0e7910105d94caad77bce99ee7b283 (diff) | |
initial output formatter
| -rw-r--r-- | guprecords.raku | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/guprecords.raku b/guprecords.raku index 281630e..67171e7 100644 --- a/guprecords.raku +++ b/guprecords.raku @@ -4,7 +4,8 @@ use v6.d; enum Category <Host OS OSMajor Uname>; enum Metric <Boots Uptime MetaScore Downtime Lifespan>; -enum OutputFormat <PlaintextSingle PlaintextMulti MarkdownSingle MarkdownMulti>; + +enum OutputFormat <Plaintext Markdown>; subset MetricSubset of Metric where * ne any (Downtime, Lifespan); subset Natural of Int where * >= 0; @@ -93,15 +94,22 @@ class Aggregator { } } -class Reporter { +role OutputFormatter { has OutputFormat $.output-format is required; + has Natural $.header-indent = 1; + + method output-header { $.output-format ~~ Markdown ?? '#' x $.header-indent ~ ' ' !! '' } + method output-block { $.output-format ~~ Markdown ?? '```' !! '' } +} + +class Reporter does OutputFormatter { has Category $.category = Host; has Metric $.metric is required; has Natural $.limit is required; has Hash %.aggregates; method report { - say "Top {$.limit} {$.metric}'s by {$.category}:\n"; + say "{self.output-header}Top {$.limit} {$.metric}'s by {$.category}:\n"; with self!table -> (@table, %size) { my Str \format = '|' ~ join '|', @@ -109,6 +117,7 @@ class Reporter { my Str \border = '+' ~ join '+', '-' x (2+%size<count>), '-' x (2+%size<name>), '-' x (2+%size<value>), "\n"; + say self.output-block; print border; printf format, 'Pos', $.category, $.metric; print border; @@ -118,6 +127,7 @@ class Reporter { } print border; + say self.output-block; } } @@ -182,10 +192,8 @@ multi sub MAIN( Category :$category = Host, #= The category, one of Host, OS, OSMajor, Uname [default: 'Host'] Metric :$metric = Uptime, #= The metric, one of Boots, Uptime, MetaScore, Downtime, Lifespan Natural :$limit = 20, #= Limit output to num of entries. - Bool :$md, #= Output Markdown format. + OutputFormat :$output-format = Plaintext, #= Output format. ) { - my OutputFormat $output-format = $md ?? MarkdownSingle !! PlaintextSingle; - if $category ~~ Host { do-it($stats-dir, HostReporter.new(:$metric, :$limit, :$output-format)); } elsif $metric ~~ MetricSubset { @@ -199,15 +207,14 @@ multi sub MAIN( Str :$stats-dir is required, Bool :$all, #= Generate all possible stats Natural :$limit = 20, - Bool :$md, + OutputFormat :$output-format = Plaintext, #= Output format. ) { - my OutputFormat $output-format = $md ?? MarkdownMulti !! PlaintextMulti; - + my Natural $header-indent = 2; for Category.^enum_value_list X Metric.^enum_value_list -> (Category $category, Metric $metric) { next if $category !~~ Host and $metric !~~ MetricSubset; do-it($stats-dir, $category ~~ Host - ?? HostReporter.new(:$metric, :$limit, :$output-format) - !! Reporter.new(:$category, :$metric, :$limit, :$output-format)); + ?? HostReporter.new(:$metric, :$limit, :$output-format, :$header-indent) + !! Reporter.new(:$category, :$metric, :$limit, :$output-format, :$header-indent)); say ''; } } |
