Skip to content

Instantly share code, notes, and snippets.

@barefootcoder
Created November 16, 2014 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barefootcoder/d49b6a4f24e80bc054c9 to your computer and use it in GitHub Desktop.
Save barefootcoder/d49b6a4f24e80bc054c9 to your computer and use it in GitHub Desktop.
Code and example call for "Git-Like Menus". (see http://blogs.perl.org/users/buddy_burden/2014/11/git-like-menus.html)
my %KEYNAMES = ( ' ' => 'SPACE', "\n" => 'ENTER' );
func mini_menu ($choices, $prompt, HashRef :$help, HashRef :$dispatch, CodeRef :$premenu, :$delim = ',')
{
my @choices = split(//, $choices);
if ($help)
{
push @choices, '?';
$help->{'?'} = 'print help';
}
my $opts = join($delim, map { $KEYNAMES{$_} // $_ } @choices);
my $choice;
PROMPT:
{
$premenu->() if $premenu and not defined $choice;
print "$prompt [$opts] ";
$choice = prompt -single;
$choice = "\n" if length($choice) == 0; # empty string means the user just hit ENTER
if ($help and $choice eq '?')
{
say $KEYNAMES{$_} // $_, " - $help->{$_}" foreach @choices;
redo PROMPT;
}
redo PROMPT unless $choices =~ /\Q$choice/;
if ($dispatch and $dispatch->{$choice})
{
if ( $dispatch->{$choice}->($choice) != 0 )
{
undef $choice;
redo PROMPT;
}
}
};
return $choice;
}
mini_menu("ratsvc \n\$" => "What shall we do with it? ",
premenu => sub {
say '';
say $album->basename, ':';
say '';
system qq{ ls -C "$album" };
system qq{ check-picard "$album" };
},
help => {
r => "reset and reclean",
a => "reset artist sort order",
t => "reset title sort order",
s => "break title into title/subtitle",
v => "fix vocals frames",
c => "reset comments to v1 values",
"\n" => "check again",
' ' => "move on",
'$' => "go to a command prompt",
},
dispatch => {
r => sub { die qq{ RE-PROCESS "$album" }; },
a => sub { system qq{ sort-order "$album" A: }; 1 },
t => sub { system qq{ sort-order "$album" T: }; 1 },
s => sub { name_tweak qq{ subtitle "$album" }; 1 },
v => sub { system qq{ comments-from-vocals "$album" }; 1 },
c => sub { system qq{ comments-from-v1 "$album" }; 1 },
' ' => undef,
"\n" => sub { 1 },
'$' => sub { system("bash"); 1 },
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment