about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-06-18 05:02:13 +0530
committerAndinus <andinus@nand.sh>2020-06-18 05:02:13 +0530
commit5a2c06fbe7a6a70edd02726da17d5fd931eecd8f (patch)
tree0a2fe67b73297bc839a8942db266a965d5fbfb71
parent81d829b1d2a08e3cfbc996caf3e56ddfc9ab2376 (diff)
downloadara-5a2c06fbe7a6a70edd02726da17d5fd931eecd8f.tar.gz
Move state notes table generator to a module
-rwxr-xr-xara.pl30
-rw-r--r--lib/StateNotes.pm57
2 files changed, 72 insertions, 15 deletions
diff --git a/ara.pl b/ara.pl
index 0bbf062..b1766b0 100755
--- a/ara.pl
+++ b/ara.pl
@@ -3,6 +3,9 @@
 use strict;
 use warnings;
 
+use lib::relative 'lib';
+use StateNotes;
+
 use Path::Tiny;
 use Time::Moment;
 use Text::ASCIITable;
@@ -167,11 +170,7 @@ my $statewise = $json_data->{statewise};
 
 my ( $covid_19_data, $notes_table, @months, $today );
 
-if ( $state_notes ) {
-    $notes_table = Text::ASCIITable->new( { drawRowLine => 1 } );
-    $notes_table->setCols( qw( State Notes ) );
-    $notes_table->setColWidth( 'Notes', 74 );
-} else {
+unless ( $state_notes ) {
     # Map month number to Months.
     @months = qw( lol Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
 
@@ -238,12 +237,7 @@ foreach my $i ( 0 ... scalar @$statewise - 1 ) {
     $state = $statewise->[$i]{statecode}
         if length $state > 16;
 
-    if ( $state_notes ) {
-        $notes_table->addRow(
-            $state,
-            $statewise->[$i]{statenotes},
-        ) unless length($statewise->[$i]{statenotes}) == 0;
-    } else {
+    unless ( $state_notes ) {
         my $update_info;
         my $lastupdatedtime = $statewise->[$i]{lastupdatedtime};
         my $last_update_dmy = substr( $lastupdatedtime, 0, 10 );
@@ -336,11 +330,17 @@ foreach my $i ( 0 ... scalar @$statewise - 1 ) {
     $rows_printed++;
 }
 
-die "No rows in table\n" unless $rows_printed;
-
-# Generate tables.
 if ( $state_notes ) {
-    print $notes_table;
+    my ( $state_notes_table, $rows_in_table ) = StateNotes::get(
+        $statewise,
+        \%hide,
+        \%show,
+        $rows_to_print
+    );
+
+    die "No rows in table\n" unless $rows_in_table;
+    print $state_notes_table;
 } else {
+    die "No rows in table\n" unless $rows_printed;
     print $covid_19_data;
 }
diff --git a/lib/StateNotes.pm b/lib/StateNotes.pm
new file mode 100644
index 0000000..9a3cab0
--- /dev/null
+++ b/lib/StateNotes.pm
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+package StateNotes;
+
+use strict;
+use warnings;
+
+use Text::ASCIITable;
+use Carp qw( croak carp );
+
+sub get {
+    my ( $statewise, $hide, $show, $rows_to_print ) = @_;
+    my $table = Text::ASCIITable->new( { drawRowLine => 1 } );
+    $table->setCols( qw( State Notes ) );
+    $table->setColWidth( 'Notes', 74 );
+
+    my $rows_in_table = 0;
+    foreach my $i ( 0 ... scalar @$statewise - 1 ) {
+        # $rows_printed is incremented at the end of this foreach
+        # loop.
+        if ( $rows_to_print ) {
+            last if $rows_in_table == $rows_to_print;
+        }
+
+        my $state = $statewise->[$i]{state};
+
+        # If user has asked to show specific states then forget about
+        # hide option.
+        if ( scalar keys %$show ) {
+            next
+                unless exists $show->{lc $state}
+                or ( length $state > 16
+                     and exists $show->{lc $statewise->[$i]{statecode}});
+        } else {
+            next
+                if exists $hide->{lc $state}
+                # User sees the statecode if length $state > 16 so we
+                # also match against that.
+                or ( length $state > 16
+                     and exists $hide->{lc $statewise->[$i]{statecode}});
+        }
+
+        $state = $statewise->[$i]{statecode}
+            if length $state > 16;
+
+        unless ( length($statewise->[$i]{statenotes}) == 0 ) {
+            $table->addRow(
+                $state,
+                $statewise->[$i]{statenotes},
+            ) ;
+            $rows_in_table++;
+        }
+    }
+    return ( $table, $rows_in_table );
+}
+
+1;