From 43ad920bd86af84f6d676b0524983658cae034b9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sat, 15 Dec 2012 19:11:22 +0100 Subject: [PATCH] menu commands has a -select-cmds option when this option is given, menu expect three argument per entry instead of two, the last one being the command to run when the entry is selected, but not yet validated. --- src/commands.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/commands.cc b/src/commands.cc index 571e73b1..5060f0c4 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -575,13 +575,17 @@ void eval_string(const CommandParameters& params, Context& context) void menu(const CommandParameters& params, Context& context) { - ParametersParser parser(params, { { "auto-single", false } }); + ParametersParser parser(params, { { "auto-single", false }, + { "select-cmds", false } }); - size_t count = parser.positional_count(); - if (count == 0 or (count % 2) != 0) + const bool with_select_cmds = parser.has_option("select-cmds"); + const size_t modulo = with_select_cmds ? 3 : 2; + + const size_t count = parser.positional_count(); + if (count == 0 or (count % modulo) != 0) throw wrong_argument_count(); - if (count == 2 and parser.has_option("auto-single")) + if (count == modulo and parser.has_option("auto-single")) { CommandManager::instance().execute(parser[1], context); return; @@ -589,16 +593,21 @@ void menu(const CommandParameters& params, Context& context) std::vector choices; std::vector commands; - for (int i = 0; i < count; i += 2) + std::vector select_cmds; + for (int i = 0; i < count; i += modulo) { choices.push_back(parser[i]); commands.push_back(parser[i+1]); + if (with_select_cmds) + select_cmds.push_back(parser[i+2]); } context.input_handler().menu(choices, [=](int choice, MenuEvent event, Context& context) { if (event == MenuEvent::Validate and choice >= 0 and choice < commands.size()) CommandManager::instance().execute(commands[choice], context); + if (event == MenuEvent::Select and choice >= 0 and choice < select_cmds.size()) + CommandManager::instance().execute(select_cmds[choice], context); }, context); }