From 34fff163576418fb7daae0a787deffa3dac42e39 Mon Sep 17 00:00:00 2001 From: Andinus Date: Mon, 16 Aug 2021 00:26:23 +0530 Subject: Add seconds-to-str subroutine to format seconds, add Taurus::Seconds Formatting seconds was moved to seconds-to-str module. --- lib/Taurus/CLI.rakumod | 15 +++++++++------ lib/Taurus/Seconds.rakumod | 11 +++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 lib/Taurus/Seconds.rakumod (limited to 'lib') diff --git a/lib/Taurus/CLI.rakumod b/lib/Taurus/CLI.rakumod index 56ef889..a7759b0 100644 --- a/lib/Taurus/CLI.rakumod +++ b/lib/Taurus/CLI.rakumod @@ -1,3 +1,5 @@ +use Taurus::Seconds; + use CSV::Parser; use Terminal::UI 'ui'; use Terminal::ANSI::OO 't'; @@ -83,9 +85,9 @@ multi sub MAIN ( my $outgoing = .grep(*.[2] eqv "Outgoing Call").map(*.[4]).sum; my $incoming = .grep(*.[2] eqv "Incoming Call").map(*.[4]).sum; - $p1.put: "%-*s %.2f hours".sprintf($fmt, "Outgoing:", $outgoing / 3600); - $p1.put: "%-*s %.2f hours".sprintf($fmt, "Incoming:", $incoming / 3600); - $p1.put: "%-*s %.2f hours".sprintf($fmt, "Total:", ($incoming + $outgoing) / 3600); + $p1.put: "%-*s %s".sprintf($fmt, "Outgoing:", seconds-to-str($outgoing)); + $p1.put: "%-*s %s".sprintf($fmt, "Incoming:", seconds-to-str($incoming)); + $p1.put: "%-*s %s".sprintf($fmt, "Total:", seconds-to-str($outgoing + $incoming)); $p1.put: ""; } } elsif %meta -> $num { @@ -99,9 +101,9 @@ multi sub MAIN ( my $outgoing = .grep(*.[2] eqv "Outgoing Call").map(*.[4]).sum; my $incoming = .grep(*.[2] eqv "Incoming Call").map(*.[4]).sum; - $p1.put: "%-*s %.2f hours".sprintf($fmt, "Outgoing:", $outgoing / 3600); - $p1.put: "%-*s %.2f hours".sprintf($fmt, "Incoming:", $incoming / 3600); - $p1.put: "%-*s %.2f hours".sprintf($fmt, "Total:", ($incoming + $outgoing) / 3600); + $p1.put: "%-*s %s".sprintf($fmt, "Outgoing:", seconds-to-str($outgoing)); + $p1.put: "%-*s %s".sprintf($fmt, "Incoming:", seconds-to-str($incoming)); + $p1.put: "%-*s %s".sprintf($fmt, "Total:", seconds-to-str($outgoing + $incoming)); $p1.put: ""; $p1.put: "%-*s %d".sprintf($fmt, "Declined:", @@ -118,6 +120,7 @@ multi sub MAIN ( ui.shutdown; } + multi sub MAIN( Bool :$version #= print version ) is export { put "Taurus v" ~ $?DISTRIBUTION.meta; } diff --git a/lib/Taurus/Seconds.rakumod b/lib/Taurus/Seconds.rakumod new file mode 100644 index 0000000..647f96d --- /dev/null +++ b/lib/Taurus/Seconds.rakumod @@ -0,0 +1,11 @@ +unit module Taurus::Seconds; + +sub seconds-to-str(UInt $seconds --> Str) is export { + given $seconds { + when * < 60 { $seconds ~ "s" } + when * < 3600 { "%2dm %2ds".sprintf($_ div 60, $_ % 60) } + when * < 86400 { "%2dh %2dm %2ds".sprintf($_ div 3600, ($_ % 3600) div 60, $_ % 60) } + default { "%dd %2dh %2dm %2ds" + .sprintf($_ div 86400, ($_ % 86400) div 3600, ($_ % 3600) div 60, $_ % 60) } + } +} -- cgit 1.4.1-2-gfad0