App-ConMenu
view release on metacpan or search on metacpan
Revision history for Perl extension App-ConMenu
1.00 2018-06-13T19:21:03Z
- original version
Build.PL
Changes
LICENSE
META.json
README.md
cpanfile
lib/App/ConMenu.pm
minil.toml
scripts/m.pl
t/App-ConMenu.t
t/test_resources/testMenu.yml
META.yml
MANIFEST
{
"abstract" : "Very simple Menu For Console commands Platform Agnostic",
"author" : [
"Michael Mueller <michael@muellers.net.au>"
],
"dynamic_config" : 0,
"generated_by" : "Minilla/v3.1.1",
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : "2"
},
"name" : "App-ConMenu",
"no_index" : {
"directory" : [
"t",
"xt",
"inc",
"share",
"eg",
"examples",
"author",
"builder"
"perl" : "v5.10.0"
}
},
"test" : {
"requires" : {
"Test::More" : "0.98"
}
}
},
"provides" : {
"App::ConMenu" : {
"file" : "lib/App/ConMenu.pm",
"version" : "1.00"
}
},
"release_status" : "stable",
"resources" : {
"bugtracker" : {
"web" : "https://github.com/code4pay/App-ConMenu/issues"
},
"homepage" : "https://github.com/code4pay/App-ConMenu",
"repository" : {
"url" : "git://github.com/code4pay/App-ConMenu.git",
"web" : "https://github.com/code4pay/App-ConMenu"
}
},
"version" : "1.00",
"x_serialization_backend" : "JSON::PP version 2.94",
"x_static_install" : 1
}
---
abstract: 'Very simple Menu For Console commands Platform Agnostic'
author:
- 'Michael Mueller <michael@muellers.net.au>'
build_requires:
Test::More: '0.98'
configure_requires:
Module::Build::Tiny: '0.035'
dynamic_config: 0
generated_by: 'Minilla/v3.1.1, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: App-ConMenu
no_index:
directory:
- t
- xt
- inc
- share
- eg
- examples
- author
- builder
provides:
App::ConMenu:
file: lib/App/ConMenu.pm
version: '1.00'
requires:
File::HomeDir: '0'
Term::ANSIScreen: '0'
YAML::Tiny: '0'
perl: v5.10.0
resources:
bugtracker: https://github.com/code4pay/App-ConMenu/issues
homepage: https://github.com/code4pay/App-ConMenu
repository: git://github.com/code4pay/App-ConMenu.git
version: '1.00'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
x_static_install: 1
# NAME
App::ConMenu - Very simple Menu For Console commands Platform Agnostic
# SYNOPSIS
use App::ConMenu;
my $menu = App::ConMenu->new();
$menu->{fileName} = '.~/menu.yml';
$menu->loadMenuFile();
$menu->printMenu();
$menu->waitForInput();
1;
# 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.
The `m.pl` in the scripts dir is a script that creates a menu by using the module. By default
the script uses ~/.con\_menu.yml on unix type systems and <HOMEDIR>\\\_con\_menu.yml on Windows type systems. If
the files do not exist then you will be prompted to create an example version.
# LICENSE
Copyright (C) Michael Mueller.
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';
lib/App/ConMenu.pm view on Meta::CPAN
}
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.
The C<m.pl> in the scripts dir is a script that creates a menu by using the module. By default
the script uses ~/.con_menu.yml on unix type systems and <HOMEDIR>\_con_menu.yml on Windows type systems. If
the files do not exist then you will be prompted to create an example version.
=head1 LICENSE
Copyright (C) Michael Mueller.
name = "App-ConMenu"
# badges = ["travis"]
module_maker="ModuleBuildTiny"
static_install = "auto"
scripts/m.pl view on Meta::CPAN
# on Windows type systems. If the files do not exist then you will be prompted to create an
# example version.
use strict;
use warnings;
use 5.10.0;
use File::HomeDir;
use File::Spec;
use Carp qw (croak);
use lib './lib/';
use App::ConMenu;
my $menu = App::ConMenu->new();
my $homeDir = File::HomeDir->my_home;
my $filePrefix = '.'; #.filename for unix type systems
if ($^O eq 'MSWin32') {
$filePrefix = '_'; # _filename for windows.
}
my $fullFileName = File::Spec->catfile($homeDir, $filePrefix.'con_menu.yml') ;
#offer to create a default file
if (! -e $fullFileName ) {
say 'The Yaml menu file does not exist in your home dir. ';
say 'Would you like me to create it ? (Y/N)';
my $selection = <>;
chomp($selection);
if (uc($selection) eq 'Y' ) {
$menu->createDefaultFile($fullFileName);
say ' File created at '.$fullFileName.' edit to add your entries';
}
exit;
}
$menu->{fileName} = $fullFileName;
$menu->loadMenuFile();
$menu->printMenu();
$menu->waitForInput();
t/App-ConMenu.t view on Meta::CPAN
# Before 'make install' is performed this script should be runnable with
# 'make test'. After 'make install' it should work as 'perl Code4Pay-Menu.t'
#########################
# change 'tests => 1' to 'tests => last_test_to_print';
use strict;
use warnings;
use lib 'lib';
use Test::More tests => 4;
BEGIN { use_ok('App::ConMenu') };
#########################
# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.
my $menu = new_ok ('App::ConMenu');
$menu->{'fileName'}= './t/test_resources/testMenu.yml';
my $menuItems = $menu->loadMenuFile();
ok ($menuItems->[0]->{'Test Menu Item 1'}->{'working_dir'} eq '../../t');
my $commandStructure;
#Run a windows command if we are on windows
if ($^O eq 'MSWin32'){
$commandStructure = $menuItems->[0]->{'Test Menu Item 2'};
} else {
$commandStructure = $menuItems->[0]->{'Test Menu Item 1'};
}
ok ($menu->execute($commandStructure));
t/test_resources/testMenu.yml view on Meta::CPAN
Test Menu Item 1:
working_dir: ../../t
commands :
- ls
Test Menu Item 2:
working_dir: /../../
commands:
- dir
( run in 0.739 second using v1.01-cache-2.11-cpan-49f99fa48dc )