App-ConMenu

 view release on metacpan or  search on metacpan

lib/App/ConMenu.pm  view on Meta::CPAN

package App::ConMenu;
use 5.10.0;
use strict;
use warnings;
use Carp qw(croak);
use YAML::Tiny;
use Term::ANSIScreen qw(cls);
our $VERSION = "1.00";


sub new  {
    my $type = shift;
    my $self = {};
    return bless $self, $type;
}

# load the yaml file you should have
# set filename by now.
sub loadMenuFile {
    my $self = shift;
    $self->{fileName} or croak ("No yaml file Name set");
    my $yaml;
    $yaml = YAML::Tiny -> read($self->{fileName});
    $self->{'menu'}= $yaml;
    return $yaml;
}

sub execute {
    my $self = shift;
    my $commandStructure = shift;
    my $commands = $commandStructure->{'commands'};
    foreach my $command (@$commands)
    {
        print `$command`;
    }
    return 1; # return 1 so that testing knows we got this far.
}

sub printMenu {
    my $self = shift;
    my $menuItemsUnsorted = $self->{'menu'}->[0];
    my @menuItems = sort { {$a} cmp {$b} } keys(%$menuItemsUnsorted);
    $self->{menuItems} = \@menuItems;
    cls();
    my $i=1;
    my @menuItemsNumerical = map { '['. $i++.'] '.$_ } @menuItems;
    say join("\n", @menuItemsNumerical);
    say 'Choose a menu item by pressing the corresponding number';
    say 'q to exit';
}

sub waitForInput {
    my $self = shift;
    my $selection = <>;
    if ($selection =~ /[0-9]+/){
        if ($selection  > scalar ($self->{menuItems}) or $selection < 1 ){
            say 'Error no such menu item';
            exit;
        }
    } else {
        exit;
    }
    my $menuItems = $self->{menuItems};
    $self->execute($self->{menu}->[0]->{$menuItems->[$selection -1]})
}

# create a default file to get people going.
sub createDefaultFile{
    my $self = shift;
    my $fileName = shift;
    my $menu = {
        'Menu option 1' => {
            'commands'    => [
                'ls'
            ],
            'working_dir' => './'
        },
        'Menu Option 2' => {
            'commands'    => [
                'dir'
            ],
            'working_dir' => './'
        }

    };
    my $yaml = YAML::Tiny->new($menu);
    $yaml->write($fileName);

}


1;
__END__

=encoding utf-8

=head1 NAME

App::ConMenu - Very simple Menu For Console commands Platform Agnostic

=head1 SYNOPSIS

    use App::ConMenu;
    my $menu = App::ConMenu->new();
    $menu->{fileName} = '.~/menu.yml';
    $menu->loadMenuFile();
    $menu->printMenu();
    $menu->waitForInput();
    1;


=head1 DESCRIPTION

App::ConMenu is a very simple console menu application it allows you to display a menu of
choices then select one of those by pressing the corresponding number.  This will cause ComMenu
to execute the associated commands in the menu.yml file.



( run in 2.169 seconds using v1.01-cache-2.11-cpan-d7a12ab2c7f )