PerlTwo has asked for the wisdom of the Perl Monks concerning the following question:

In the cpan example I can't work out how to add an attribute to a menu item eg item_fgcolor() Any help would be humbly appreciated.

Replies are listed 'Best First'.
Re: Term::ANSIMenu help
by FreeBeerReekingMonk (Deacon) on Dec 07, 2016 at 21:20 UTC
    $menu->selection_fgcolor("RED");

    thus:

    #!/usr/bin/perl use Term::ANSIMenu; my $menu = Term::ANSIMenu->new( width => 40, help => [['', \&standard_help], ['hint 1', \&help_item], [ undef, \&standard_help], ['hint 3', undef] ], title => 'title', items => [['1', 'First menu item', \&exec_item], ['2', 'This string is just too long to fit in a normal t +erminal and thus it will be clipped.'], ['3', '', sub { system "man man" }] ], status => 'status', prompt => 'prompt: ' ); $menu->selection_fgcolor("RED"); $menu->print_menu(); while (my $key = $menu->read_key()) { last unless defined $menu->do_key($key); $menu->update_status('') if $key eq 'S'; $menu->update_status('New status') if $key eq 's'; $menu->update_prompt('') if $key eq 'P'; $menu->update_prompt('New prompt: ') if $key eq 'p'; } $menu->pos($menu->line_after_menu() + 1, 1);

    The documentation is pretty clear:

    Attributes can be accessed by using a method that will function as both a accessor and a mutator.

    Thus you can read it like this: $thefgcolor = $menu->selection_fgcolor();

    Its value can be changed like this:

    $menu->selection_fgcolor($newfgcolor);

    edit: tabs to spaces: A bit less indentation...

      For completeness:

      As separate lines:

      $menu->selection_bgcolor("GREEN"); $menu->selection_fgcolor("YELLOW");

      Inside the constructor:

      my $menu = Term::ANSIMenu->new( width => 40, : : : prompt => 'prompt: ', item_fgcolor => "YELLOW", item_style => ["BOLD"], selection_bgcolor => "BLACK", selection_fgcolor => "RED", );

      Many thanks for your speedy response. I have included my code this time. selection_fgcolor =>"RED" works fine but item_fgcolor => "YELLOW", and item_bgcolor => "BLUE" have no impact.

      #!/usr/bin/perl use Term::ANSIMenu; my $menu = Term::ANSIMenu->new( width => 79, title => '[MENU v3.02] Plumbing Sales +MENU', title_style =>['BOLD'], exit_keys=>['X'], item_fgcolor => "YELLOW", item_bgcolor => "BLUE", + selection_fgcolor =>"RED", items => [[ 'A','SALES ORDERS MENU [-MENU-]',sub { +system " kcmenu $ENV{TBX_DISK0}/100,100/sm2so"} ], [ 'B','TICKETS & INVOICING MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_DISK0}/100,100/sm2tkt"} ], [ 'C','STOCK & SUPPLIERS MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_DISK0}/100,100/sm2stk"} ], [ 'D','CUSTOMERS & CONTRACTS MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_DISK0}/100,100/sm2cus"} ], [ 'E','GOODS IN MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_KCM}/GoodsIn.kcm"} ], [ 'F','TRANSPORT MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_DISK0}/100,100/sm2trans"} ], [ 'G','MARKING MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_DISK0}/100,100/sm2mark"} ], [ 'H','QUOTATIONS MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_KCM}/Quotes.kcm"} ], [ 'I','BONUS MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_DISK0}/100,100/bonus1"} ], [ 'J','EXTRAS MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_DISK0}/100,100/sm2extra"} ], [ 'K','DIRECTS MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_DISK0}/100,100/pdso"} ], [ 'L','STOCK TRANSFERS v2.0 MENU [-MENU-]',sub {system " kc +menu $ENV{TBX_KCM}/Transfers.kcm"} ], [ 'S','Supplier Returns v1.0 [gdsrtn]',sub {system " tb +w -i [50,50]gdsrtn"} ], [ 'X','Exit Menu',undef ], ],); $menu->print_menu(); while (my $key = uc($menu->read_key())) { last unless defined $menu->do_key($key); $menu->update_status('') if $key eq 'S'; $menu->update_status('New status') if $key eq 's'; $menu->update_prompt('') if $key eq 'P'; $menu->update_prompt('New prompt: ') if $key eq 'p'; } $menu->pos($menu->line_after_menu() + 1, 1);
        [ 'S','Supplier Returns v1.0.... uses the S and $menu->update_status('') if $key eq 'S'; use a different letter. You also are hitting the maximum?

        The color is simple (a feature to prevent color bleeding). The style is by DEFAULT the value CLEAR, which means "nothing". so you need to unset it to make it work:

        item_fgcolor => "YELLOW", item_style => [],

        item_style: must be one or more of BLINK, REVERSE, BOLD, UNDERLINE and CLEAR

        edit:

        also, end your script with:

        $menu->pos($menu->item_count() + 7, 1);

        It will move the cursor to the bottom of your menu

Re: Term::ANSIMenu help
by 1nickt (Canon) on Dec 07, 2016 at 16:46 UTC

    In order to get help expeditiously, please post the code you are using, contained in a short script that can be downloaded and run, as well as the error messages you are getting.

    The way forward always starts with a minimal test.