| 1 | package Salus::Command; | ||
| 2 | use strict; use warnings; | ||
| 3 | use Salus; | ||
| 4 | use Rope; | ||
| 5 | use Rope::Autoload; | ||
| 6 | use Term::Table; | ||
| 7 | use Term::Size; | ||
| 8 | use JSON qw//; | ||
| 9 | use ANSI::Palette qw/bold_16 reset/; | ||
| 10 | |||
| 11 | prototyped ( | ||
| 12 | info => [qw/ | ||
| 13 | title options_title header_title header_description diff_title diff_desc | ||
| 14 | read_title read_desc add_title add_desc all_title all_desc get_title | ||
| 15 | get_desc get_col_title get_col_desc set_title set_desc set_col_title | ||
| 16 | set_col_desc delete_title delete_desc delete_col_title delete_col_desc | ||
| 17 | write_title write_desc sort_title sort_desc search_title search_desc | ||
| 18 | find_title find_desc agg_title agg_desc exit_title exit_desc help_title | ||
| 19 | help_desc | ||
| 20 | /], | ||
| 21 | title => qq|Welcome to the command line interface for Salus\n|, | ||
| 22 | options_title => qq|The following are commands that can be used to manipulate a Salus CSV\n|, | ||
| 23 | header_title => q|+ Headers|, | ||
| 24 | header_description => [ | ||
| 25 | qq|\tprint the headers of the csv - |, | ||
| 26 | qq|headers|, | ||
| 27 | ], | ||
| 28 | diff_title => qq|+ Diff Files|, | ||
| 29 | diff_desc => [ | ||
| 30 | qq|\tdiff two salus csv files - |, | ||
| 31 | qq|diff \$filepath1 \$filepath2| | ||
| 32 | ], | ||
| 33 | read_title => qq|+ Read File|, | ||
| 34 | read_desc => [ | ||
| 35 | qq|\tread a salus csv file - |, | ||
| 36 | qq|read \$filepath \$unprotected|, | ||
| 37 | ], | ||
| 38 | add_title => qq|+ Add Row|, | ||
| 39 | add_desc => [ | ||
| 40 | qq|\tadd a new row, the delimeter is a space not wrapped in quotations - |, | ||
| 41 | qq|add \$one \$two \$three|, | ||
| 42 | ], | ||
| 43 | all_title => qq|+ All|, | ||
| 44 | all_desc => [ | ||
| 45 | qq|\tprint all rows - |, | ||
| 46 | qq|all \$include_index| | ||
| 47 | ], | ||
| 48 | get_title => qq|+ Get Row|, | ||
| 49 | get_desc => [ | ||
| 50 | qq|\tretrieve a row by index and print it to the terminal - |, | ||
| 51 | qq|get \$index|, | ||
| 52 | ], | ||
| 53 | get_col_title => qq|+ Get Row Column|, | ||
| 54 | get_col_desc => [ | ||
| 55 | qq|\tretrieve a column by row and column index and print it to the terminal - |, | ||
| 56 | qq|get_col \$row \$col| | ||
| 57 | ], | ||
| 58 | set_title => qq|+ Set Row|, | ||
| 59 | set_desc => [ | ||
| 60 | qq|\tset an existing row by index or name - |, | ||
| 61 | q|set \$index|, | ||
| 62 | ], | ||
| 63 | set_col_title => qq|+ Set Row Column|, | ||
| 64 | set_col_desc => [ | ||
| 65 | qq|\tset a columns value by row and column index or name - |, | ||
| 66 | qq|set_col \$row \$col|, | ||
| 67 | ], | ||
| 68 | delete_title => qq|+ Delete Row|, | ||
| 69 | delete_desc => [ | ||
| 70 | qq|\tdelete a row by index - |, | ||
| 71 | qq|delete \$row| | ||
| 72 | ], | ||
| 73 | delete_col_title => qq|+ Delete Row|, | ||
| 74 | delete_col_desc => [ | ||
| 75 | qq|\tdelete a column by row index and column index or name - |, | ||
| 76 | qq|delete \$row \$col| | ||
| 77 | ], | ||
| 78 | write_title => qq|+ Write File|, | ||
| 79 | write_desc => [ | ||
| 80 | qq|\twrite data to a file - |, | ||
| 81 | qq|write \$filepath| | ||
| 82 | ], | ||
| 83 | sort_title => qq|+ Sort|, | ||
| 84 | sort_desc => [ | ||
| 85 | qq|\treorder the rows by column index or name - |, | ||
| 86 | qq|sort \$col \$dir| | ||
| 87 | ], | ||
| 88 | search_title => qq|+ Search|, | ||
| 89 | search_desc => [ | ||
| 90 | qq|\tsearch all rows by column value and print any matches to the terminal - |, | ||
| 91 | qq|search \$col \$search \$include_index| | ||
| 92 | ], | ||
| 93 | find_title => qq|+ Find|, | ||
| 94 | find_desc => [ | ||
| 95 | qq|\tfind a row index by column value and print it to the terminal - |, | ||
| 96 | qq|find \$col \$search| | ||
| 97 | ], | ||
| 98 | exit_title => qq|+ Exit|, | ||
| 99 | exit_desc => [ | ||
| 100 | qq|\tto exit you can either use - CTRL C - or type - |, | ||
| 101 | qq|exit|, | ||
| 102 | ], | ||
| 103 | help_title => qq|+ Help|, | ||
| 104 | help_desc => [ | ||
| 105 | qq|\tprint all available commands - |, | ||
| 106 | qq|help| | ||
| 107 | ], | ||
| 108 | agg_title => qq|+ Aggregations|, | ||
| 109 | agg_desc => [ | ||
| 110 | qq|\tthe following aggregations are available - count - min - max - sum - mean - median - mode - |, | ||
| 111 | qq|min \$column \$table| | ||
| 112 | ], | ||
| 113 | salus => undef | ||
| 114 | ); | ||
| 115 | |||
| 116 | function INITIALISED => sub { | ||
| 117 | my ($self, $params) = @_; | ||
| 118 | |||
| 119 | push @INC, $params->{lib} if $params->{lib}; | ||
| 120 | |||
| 121 | my $class = $params->{class}; | ||
| 122 | |||
| 123 | if ($class) { | ||
| 124 | eval "require $class"; | ||
| 125 | $self->class = $class = $class->new(); | ||
| 126 | } else { | ||
| 127 | if (!$params->{headers}) { | ||
| 128 | self->say("No class or headers passed while starting the salus script"); | ||
| 129 | exit; | ||
| 130 | } | ||
| 131 | |||
| 132 | if (!ref $params->{headers}) { | ||
| 133 | $params->{headers} = JSON->new->decode($params->{headers}); | ||
| 134 | } | ||
| 135 | |||
| 136 | $self->class = $class = Salus->new({ | ||
| 137 | headers => $params->{headers} | ||
| 138 | }); | ||
| 139 | } | ||
| 140 | |||
| 141 | $self->salus = $class; | ||
| 142 | }; | ||
| 143 | |||
| 144 | function run => sub { | ||
| 145 | my ($self, $params) = @_; | ||
| 146 | |||
| 147 | $self->help(); | ||
| 148 | |||
| 149 | while (1) { | ||
| 150 | $self->say("cmd: ", 1); | ||
| 151 | my $input = <STDIN>; | ||
| 152 | chomp($input); | ||
| 153 | if ($input eq 'exit') { | ||
| 154 | exit; | ||
| 155 | } | ||
| 156 | my ($cmd, @args) = $self->extract_line_args($input); | ||
| 157 | $self->$cmd(@args); | ||
| 158 | } | ||
| 159 | }; | ||
| 160 | |||
| 161 | function help => sub { | ||
| 162 | my $self = shift; | ||
| 163 | for (@{ $self->info }) { | ||
| 164 | $self->say($self->$_); | ||
| 165 | } | ||
| 166 | }; | ||
| 167 | |||
| 168 | function diff => sub { | ||
| 169 | my ($self, $file1, $file2) = @_; | ||
| 170 | my $diff = eval { $self->salus->diff_files($file1, $file2); }; | ||
| 171 | $self->say($@ ? $@ : $diff); | ||
| 172 | }; | ||
| 173 | |||
| 174 | function read => sub { | ||
| 175 | my ($self, $file, $unprotected) = @_; | ||
| 176 | $self->unprotected_read = $unprotected if $unprotected; | ||
| 177 | $self->salus->file = $file; | ||
| 178 | eval { $self->salus->read(); }; | ||
| 179 | $self->say($@ ? $@ : "read file"); | ||
| 180 | $self->unprotected_read = 0; | ||
| 181 | }; | ||
| 182 | |||
| 183 | function combine => sub { | ||
| 184 | my ($self, $file, $unprotected) = @_; | ||
| 185 | $self->unprotected_read = $unprotected if $unprotected; | ||
| 186 | $self->salus->file = $file; | ||
| 187 | eval { $self->salus->combine() }; | ||
| 188 | $self->say($@ ? $@ : "combined file"); | ||
| 189 | $self->unprotected_read = 0; | ||
| 190 | }; | ||
| 191 | |||
| 192 | function write => sub { | ||
| 193 | my ($self, $file) = @_; | ||
| 194 | $self->salus->file = $file if $file; | ||
| 195 | eval { $self->salus->write(); }; | ||
| 196 | $self->say($@ ? $@ : "wrote file"); | ||
| 197 | }; | ||
| 198 | |||
| 199 | function add => sub { | ||
| 200 | my ($self, @row) = @_; | ||
| 201 | eval { $self->salus->add_row(\@row); }; | ||
| 202 | $self->say($@ ? $@ : "created row"); | ||
| 203 | }; | ||
| 204 | |||
| 205 | function all => sub { | ||
| 206 | my ($self, $include_index) = @_; | ||
| 207 | my ($index, @rows) = (0); | ||
| 208 | for (@{$self->salus->rows}) { | ||
| 209 | my $row = $_->as_array(); | ||
| 210 | push @rows, ($include_index ? [ $index++, @{$row} ] : $row); | ||
| 211 | } | ||
| 212 | $self->print_table(\@rows, $include_index); | ||
| 213 | }; | ||
| 214 | |||
| 215 | function get => sub { | ||
| 216 | my ($self, $index) = @_; | ||
| 217 | my $row = eval { $self->salus->get_row($index) }; | ||
| 218 | unless ($row) { | ||
| 219 | $self->say($@ ? $@ : "no row available for index ${index}"); | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | $self->print_table($row->as_array); | ||
| 223 | }; | ||
| 224 | |||
| 225 | function get_col => sub { | ||
| 226 | my ($self, $r, $c) = @_; | ||
| 227 | my $col = eval { $self->salus->get_row_col($r, $c) }; | ||
| 228 | unless ($col) { | ||
| 229 | $self->say($@ ? $@ : "no column available for index ${r} ${c}"); | ||
| 230 | return; | ||
| 231 | } | ||
| 232 | $self->say(($col->header->label || $col->header->name) . " value " . $col->value); | ||
| 233 | }; | ||
| 234 | |||
| 235 | function set => sub { | ||
| 236 | my ($self, $r, @row) = @_; | ||
| 237 | eval { $self->salus->set_row($r, \@row) }; | ||
| 238 | $self->say($@ ? $@ : "set row"); | ||
| 239 | }; | ||
| 240 | |||
| 241 | function set_col => sub { | ||
| 242 | my ($self, $r, $c, $v) = @_; | ||
| 243 | eval { $self->salus->set_row_col($r, $c, $v) }; | ||
| 244 | $self->say($@ ? $@ : "set column"); | ||
| 245 | }; | ||
| 246 | |||
| 247 | function delete => sub { | ||
| 248 | my ($self, $row) = @_; | ||
| 249 | eval { $self->salus->delete_row($row) }; | ||
| 250 | $self->say($@ ? $@ : "deleted row"); | ||
| 251 | }; | ||
| 252 | |||
| 253 | function delete_col => sub { | ||
| 254 | my ($self, $r, $c) = @_; | ||
| 255 | eval { $self->salus->delete_row_col($r, $c) }; | ||
| 256 | $self->say($@ ? $@ : "deleted column"); | ||
| 257 | }; | ||
| 258 | |||
| 259 | function extract_line_args => sub { | ||
| 260 | my ($self, $line) = @_; | ||
| 261 | my @array = map { | ||
| 262 | my $m = $_; | ||
| 263 | if ($m ne "") { | ||
| 264 | $m =~ s/^("|')|("|')$//g; | ||
| 265 | $m; | ||
| 266 | } else { | ||
| 267 | () | ||
| 268 | } | ||
| 269 | } split /\s*("[^"]+"|[^\s]+)/, $line; | ||
| 270 | return @array; | ||
| 271 | }; | ||
| 272 | |||
| 273 | function headers => sub { | ||
| 274 | my ($self) = @_; | ||
| 275 | $self->print_table(); | ||
| 276 | }; | ||
| 277 | |||
| 278 | function sort => sub { | ||
| 279 | my ($self, $col, $order) = @_; | ||
| 280 | eval { $self->salus->sort($col, $order); }; | ||
| 281 | $self->say($@ ? $@ : "sorted rows"); | ||
| 282 | }; | ||
| 283 | |||
| 284 | function search => sub { | ||
| 285 | my ($self, $col, $search, $include_index) = @_; | ||
| 286 | my ($rows, $indexes) = eval { $self->salus->search($col, $search) }; | ||
| 287 | my @rows; | ||
| 288 | for (my $i = 0; $i < @{$rows}; $i++) { | ||
| 289 | my $row = $rows->[$i]->as_array(); | ||
| 290 | push @rows, ($include_index ? [ $indexes->[$i], @{$row} ] : $row); | ||
| 291 | } | ||
| 292 | $self->print_table(\@rows, $include_index); | ||
| 293 | }; | ||
| 294 | |||
| 295 | function find => sub { | ||
| 296 | my ($self, $col, $search) = @_; | ||
| 297 | my $index = eval { $self->salus->find($col, $search) }; | ||
| 298 | $self->say(defined $index | ||
| 299 | ? qq|Found row matching search query (${search}) with index: ${index}| | ||
| 300 | : qq|Cannot find row matching search (${search})| | ||
| 301 | ); | ||
| 302 | }; | ||
| 303 | |||
| 304 | function count => sub { | ||
| 305 | my ($self) = @_; | ||
| 306 | my $count = eval { $self->salus->count() }; | ||
| 307 | $self->say($@ ? $@ : qq|Count for csv is ${count}|); | ||
| 308 | }; | ||
| 309 | |||
| 310 | function sum => sub { | ||
| 311 | my ($self, $col) = @_; | ||
| 312 | my $sum = eval { $self->salus->sum($col) }; | ||
| 313 | $self->say($@ ? $@ : qq|Sum for column ${col} is ${sum}|); | ||
| 314 | }; | ||
| 315 | |||
| 316 | function mean => sub { | ||
| 317 | my ($self, $col) = @_; | ||
| 318 | my $mean = eval { $self->salus->mean($col) }; | ||
| 319 | $self->say($@ ? $@ : qq|Mean for column ${col} is ${mean}|); | ||
| 320 | }; | ||
| 321 | |||
| 322 | function median => sub { | ||
| 323 | my ($self, $col, $table) = @_; | ||
| 324 | my $median = eval { $self->salus->median($col, $table) }; | ||
| 325 | $table | ||
| 326 | ? $self->print_table($median->as_array) | ||
| 327 | : $self->say($@ ? $@ : qq|Median for column ${col} is ${median}|); | ||
| 328 | }; | ||
| 329 | |||
| 330 | function mode => sub { | ||
| 331 | my ($self, $col) = @_; | ||
| 332 | my ($key, $mode) = eval { $self->salus->mode($col) }; | ||
| 333 | $self->say($@ ? $@ : qq|Mode for column ${col} is key ${key} with ${mode} occurences.|); | ||
| 334 | }; | ||
| 335 | |||
| 336 | function min => sub { | ||
| 337 | my ($self, $col, $table) = @_; | ||
| 338 | my $min = eval { $self->salus->min($col, $table) }; | ||
| 339 | $table | ||
| 340 | ? $self->print_table($min->as_array) | ||
| 341 | : $self->say($@ ? $@ : qq|Min for column ${col} is ${min}|); | ||
| 342 | }; | ||
| 343 | |||
| 344 | function max => sub { | ||
| 345 | my ($self, $col, $table) = @_; | ||
| 346 | my $max = eval { $self->salus->max($col, $table) }; | ||
| 347 | $table | ||
| 348 | ? $self->print_table($max->as_array) | ||
| 349 | : $self->say($@ ? $@ : qq|Max for column ${col} is ${max}|); | ||
| 350 | }; | ||
| 351 | |||
| 352 | function print_table => sub { | ||
| 353 | my ($self, $rows, $include_indexes) = @_; | ||
| 354 | my ($columns, $r) = Term::Size::chars *STDOUT{IO}; | ||
| 355 | my $headers = $self->salus->headers_stringify; | ||
| 356 | unshift @{$headers}, 'INDEX' if ($include_indexes); | ||
| 357 | my $table = Term::Table->new( | ||
| 358 | max_width => $columns, | ||
| 359 | pad => 4, | ||
| 360 | allow_overflow => 0, | ||
| 361 | collapse => 1, | ||
| 362 | ($rows && scalar @{$rows} ? ( | ||
| 363 | header => $headers, | ||
| 364 | rows => ref $rows->[0] ? $rows : [ | ||
| 365 | $rows | ||
| 366 | ] | ||
| 367 | ) : ( | ||
| 368 | rows => [ | ||
| 369 | $headers | ||
| 370 | ] | ||
| 371 | )) | ||
| 372 | ); | ||
| 373 | print "$_\n" for $table->render; | ||
| 374 | }; | ||
| 375 | |||
| 376 | function say => sub { | ||
| 377 | my ($self, $string, $no, $colour) = @_; | ||
| 378 | |||
| 379 | if (ref $string) { | ||
| 380 | $self->say($string->[0], 1); | ||
| 381 | $self->say($string->[1], 0, 1); | ||
| 382 | return; | ||
| 383 | } | ||
| 384 | |||
| 385 | my %colours; | ||
| 386 | %colours = ( | ||
| 387 | colour => sub { return "\e[$_[0];1;1m" . $_[1]; }, | ||
| 388 | custom => sub { $colours{colour}->(@_) . "\e[0m" . $colours{base}->(''); }, | ||
| 389 | header => sub { $colours{colour}->(@_) . "\e[0m" . $colours{colour}->('34', ''); }, | ||
| 390 | '-' => sub { $colours{custom}->(31, @_); }, | ||
| 391 | '+' => sub { $colours{header}->(32, @_); }, | ||
| 392 | '$' => sub { $colours{custom}->(35, @_); }, | ||
| 393 | 'key' => sub { $colours{custom}->(36, @_); }, | ||
| 394 | 'cmd:' => sub { $colours{custom}->(32, @_); }, | ||
| 395 | 'base' => sub { $colours{colour}->(33, @_) }, | ||
| 396 | ); | ||
| 397 | |||
| 398 | $string =~ s/(-|\+|cmd\:)/$colours{$1}->($1)/eg; | ||
| 399 | |||
| 400 | if ($colour) { | ||
| 401 | $string =~ s/((\$)[^\s]+|[^\s]+)/$colours{$2||'key'}->($1)/eg; | ||
| 402 | } else { | ||
| 403 | $string = $colours{base}->($string); | ||
| 404 | } | ||
| 405 | |||
| 406 | print $string; | ||
| 407 | print "\e[0m\n" unless $no; | ||
| 408 | }; | ||
| 409 | |||
| 410 | 1; |