Adding help and autocompletion.

This commit is contained in:
sergiotarxz 2022-03-04 23:31:44 +01:00
parent 9d3ddadeda
commit 9f02978d4f
2 changed files with 59 additions and 18 deletions

View File

@ -8,6 +8,7 @@ my $build = Module::Build->new(
requires => { requires => {
'Mojolicious' => 0, 'Mojolicious' => 0,
'JSON' => 0, 'JSON' => 0,
'Const::Fast' => 0,
}, },
); );
$build->create_build_script; $build->create_build_script;

View File

@ -9,11 +9,43 @@ use Data::Dumper;
use Term::ReadLine; use Term::ReadLine;
use FileHandle; use FileHandle;
use Const::Fast;
use Mojo::UserAgent; use Mojo::UserAgent;
use JSON; use JSON;
binmode STDOUT, ':utf8'; binmode STDOUT, ':utf8';
const my $COMMANDS => {
countries => {
description => 'List available countries.',
action => \&show_countries,
},
select_country => {
description => 'Select a country.',
action => \&select_country
},
ambits => {
description => 'List available ambits.',
action => \&show_ambits
},
select_ambit => {
description => 'Select a ambit.',
action => \&select_ambit
},
channnels => {
description => 'List available channels.',
action => \&show_channels
},
select_channel => {
description => 'Select a channel.',
action => \&select_channel
},
help => {
description => 'Show this help.',
action => \&show_help
}
};
my $ua = Mojo::UserAgent->new; my $ua = Mojo::UserAgent->new;
my $tdt_channels = decode_json( my $tdt_channels = decode_json(
$ua->get('https://www.tdtchannels.com/lists/tv.json')->result->body ); $ua->get('https://www.tdtchannels.com/lists/tv.json')->result->body );
@ -23,10 +55,13 @@ my $countries = $tdt_channels->{countries};
my $selected_country; my $selected_country;
my $selected_ambit; my $selected_ambit;
my $term = Term::ReadLine->new('tdtcli'); my $term = Term::ReadLine->new('tdtcli');
my $attribs = $term->Attribs;
$attribs->{completion_entry_function} = $attribs->{list_completion_function};
$attribs->{completion_word} = [ keys %$COMMANDS ];
while ( defined( my $line = $term->readline('~> ') ) ) { while ( defined( my $line = $term->readline('~> ') ) ) {
proccess_line($line); process_line($line);
$term->addhistory($line); $term->addhistory($line);
if ( defined $selected_country ) { if ( defined $selected_country ) {
@ -38,29 +73,34 @@ while ( defined( my $line = $term->readline('~> ') ) ) {
} }
} }
sub proccess_line { sub process_line {
my $line = shift; my $line = shift;
return unless defined $line; return unless defined $line;
my @line = split /\s+/, $line; my @line = split /\s+/, $line;
return unless defined $line[0]; return unless defined $line[0];
my $command = shift @line; my $command = shift @line;
show_countries() if $command eq 'countries'; say("No such command $command."), return
select_country( [@line] ) if $command eq 'select_country'; unless exists $COMMANDS->{$command};
show_ambits() if $command eq 'ambits'; $COMMANDS->{$command}{action}->( [@line] );
select_ambit( [@line] ) if $command eq 'select_ambit'; }
show_channels() if $command eq 'channels';
select_channel( [@line] ) if $command eq 'select_channel'; sub show_help {
say <<'EOF';
TDTCli help page:
EOF
for my $command ( sort { $a cmp $b } keys %$COMMANDS ) {
say "$command : $COMMANDS->{$command}{description}";
}
} }
sub select_channel { sub select_channel {
my $arguments = shift; my $arguments = shift;
say('You must first select a ambit with select_ambit'), return
unless defined $selected_ambit;
say('You must pass a channel number.'), return say('You must pass a channel number.'), return
unless exists $arguments->[0]; unless exists $arguments->[0];
my $channel = $arguments->[0]; my $channel = $arguments->[0];
return unless $channel =~ /^\d+$/; return unless $channel =~ /^\d+$/;
say('You must first select a ambit with /select_ambit'),
return undef $selected_ambit
unless defined $selected_ambit;
my $ambits = $countries->[$selected_country]{ambits}; my $ambits = $countries->[$selected_country]{ambits};
my $channels = $ambits->[$selected_ambit]{channels}; my $channels = $ambits->[$selected_ambit]{channels};
my $current_channel_options = $channels->[$channel]{options}; my $current_channel_options = $channels->[$channel]{options};
@ -78,7 +118,7 @@ sub select_channel {
} }
sub show_channels { sub show_channels {
say('You must first select a ambit with /select_ambit'), return say('You must first select a ambit with select_ambit'), return
unless defined $selected_ambit; unless defined $selected_ambit;
my $names = my $names =
[ map { $_->{name} } [ map { $_->{name} }
@ -93,24 +133,24 @@ sub show_channels {
sub select_ambit { sub select_ambit {
my $arguments = shift; my $arguments = shift;
say('You must first select a country with /select_country'), return say('You must first select a country with select_country'), return
unless defined $selected_country; unless defined $selected_country;
say('No ambit passed see /ambits'), return say('No ambit passed see ambits'), return
unless defined $arguments->[0]; unless defined $arguments->[0];
my $ambit = $arguments->[0]; my $ambit = $arguments->[0];
return unless $ambit =~ /^\d+$/; return unless $ambit =~ /^\d+$/;
say('Such ambit doesn\'t exists see /ambits'), return say('Such ambit doesn\'t exists see ambits'), return
unless exists $countries->[$selected_country]{ambits}[$ambit]; unless exists $countries->[$selected_country]{ambits}[$ambit];
$selected_ambit = $ambit; $selected_ambit = $ambit;
} }
sub select_country { sub select_country {
my $arguments = shift; my $arguments = shift;
say('No country passed see /countries'), return say('No country passed see countries'), return
unless defined $arguments->[0]; unless defined $arguments->[0];
my $country = $arguments->[0]; my $country = $arguments->[0];
return unless $country =~ /^\d+$/; return unless $country =~ /^\d+$/;
say('Such country doesn\'t exists see /countries'), return say('Such country doesn\'t exists see countries'), return
if !exists $countries->[$country]; if !exists $countries->[$country];
$selected_country = $country; $selected_country = $country;
undef $selected_ambit; undef $selected_ambit;