about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-06-14 02:13:31 +0530
committerAndinus <andinus@nand.sh>2020-06-14 02:13:31 +0530
commit49b8c0df649362698f8c93718e75b0a2d563dd46 (patch)
tree3d584bd10cdaab84783f30092f542c166a04309d
parent56877963f16686af6f357a3cd3ef017bb040482b (diff)
downloadara-49b8c0df649362698f8c93718e75b0a2d563dd46.tar.gz
Add ability to hide columns
Only hiding "State" & "Notes" column is forbidden & will print a
warning for the user.

I should also optimize other parts, like it still creates $deaths &
does all related calulations even if we hide that column.

And for @columns part, #perl (freenode.net) suggested some other
methods too but later we found out they were all broken (logical
error), the solutions were nice though & so I'm recording them here.

I could've created a %fields hash like this:
  my %fields = ( State =>  1 ... );

And then I would delete the things that user wants to hide:
  delete $fields{s/\b(\w)/\U$1/gr} for keys %hide;

Either like that or like this:
  delete @fields{ map ucfirst, keys %hide };

Then just used keys %fields in setCols().

Issue with the first delete method is that it's complicated & I didn't
understand it so I wasn't going to use it anyways.

Issue with the second delete method is that it wouldn't have worked
with "Last Updated" field because there is a space in between, ucfirst
function would've only capitalized 'L' of "Last".

Even if I understood the first method or somehow fix the second method
all this still wont work because keys %fields would get me columns in
random order which isn't what I want.
-rwxr-xr-xara.pl53
1 files changed, 30 insertions, 23 deletions
diff --git a/ara.pl b/ara.pl
index 6e8f622..ab32599 100755
--- a/ara.pl
+++ b/ara.pl
@@ -54,6 +54,10 @@ undef @hide{ @to_hide }
                         # Alternatively can do @hide{ @to_hide } = ()
                         # which will work even if @to_hide is empty.
 
+# Warn when user tries to hide these columns.
+warn LOCALCOLOR RED "Cannot hide state column" if exists $hide{state};
+warn LOCALCOLOR RED "Cannot hide notes column" if exists $hide{notes};
+
 sub HelpMessage {
     print LOCALCOLOR GREEN "Options:
     --local   Use local data
@@ -62,7 +66,7 @@ sub HelpMessage {
     --rows=i  Number of rows to print (i is Integer)
     --nodelta Don't print changes in values
     --nototal Don't print 'Total' row
-    --hide    Hide states from table (space seperated values)";
+    --hide    Hide states, columns from table (space seperated values)";
     print LOCALCOLOR CYAN "
     --help    Print this help message
 ";
@@ -149,21 +153,23 @@ if ( $state_notes ) {
     # Map month number to Months.
     @months = qw( lol Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
 
+    my @columns;
+    push @columns, 'State'; # User cannot hide state column.
+    push @columns, 'Confirmed' unless exists $hide{confirmed};
+    push @columns, 'Active' unless exists $hide{active};
+    push @columns, 'Recovered' unless exists $hide{recovered};
+    push @columns, 'Deaths' unless exists $hide{deaths};
+    push @columns, 'Last Updated' unless exists $hide{'last updated'};
+
     $covid_19_data = Text::ASCIITable->new( { allowANSI => 1 } );
-    $covid_19_data->setCols(
-        'State',
-        'Confirmed',
-        'Active',
-        'Recovered',
-        'Deaths',
-        'Last Updated',
-    );
-
-    $covid_19_data->alignCol( {
-        'Confirmed' => 'left',
-        'Recovered' => 'left',
-        'Deaths' => 'left',
-    } );
+    $covid_19_data->setCols( @columns );
+
+    my %alignment;
+    $alignment{Confirmed} = "left" unless exists $hide{confirmed};
+    $alignment{Recovered} = "left" unless exists $hide{recovered};
+    $alignment{Deaths} = "left" unless exists $hide{deaths};
+
+    $covid_19_data->alignCol( \%alignment );
 
     $today = Time::Moment
         ->now_utc
@@ -276,14 +282,15 @@ foreach my $i ( 0 ... scalar @$statewise - 1 ) {
             }
         }
 
-        $covid_19_data->addRow(
-            $state,
-            $confirmed,
-            $statewise->[$i]{active},
-            $recovered,
-            $deaths,
-            $update_info,
-        );
+        my @row;
+        push @row, $state;
+        push @row, $confirmed unless exists $hide{confirmed};
+        push @row, $statewise->[$i]{active} unless exists $hide{active};
+        push @row, $recovered unless exists $hide{recovered};
+        push @row, $deaths unless exists $hide{deaths};
+        push @row, $update_info unless exists $hide{'last updated'};
+
+        $covid_19_data->addRow( @row );
     }
     $rows_printed++;
 }