Change client menu api to give more control to the caller
This commit is contained in:
parent
1ca502d23d
commit
51e80558d9
|
@ -13,6 +13,13 @@ class Window;
|
|||
class String;
|
||||
class Context;
|
||||
|
||||
enum class MenuCommand
|
||||
{
|
||||
SelectPrev,
|
||||
SelectNext,
|
||||
Close,
|
||||
};
|
||||
|
||||
class Client : public SafeCountable
|
||||
{
|
||||
public:
|
||||
|
@ -23,7 +30,9 @@ public:
|
|||
virtual String prompt(const String& prompt, const Context& context,
|
||||
Completer completer = complete_nothing) = 0;
|
||||
virtual Key get_key() = 0;
|
||||
virtual int menu(const memoryview<String>& choices) = 0;
|
||||
|
||||
virtual void show_menu(const memoryview<String>& choices) = 0;
|
||||
virtual void menu_ctrl(MenuCommand command) = 0;
|
||||
};
|
||||
|
||||
struct prompt_aborted {};
|
||||
|
|
|
@ -699,7 +699,8 @@ public:
|
|||
|
||||
bool has_key_left() const { return m_pos < m_keys.size(); }
|
||||
|
||||
int menu(const memoryview<String>& choices) { return 0; }
|
||||
void show_menu(const memoryview<String>&) {}
|
||||
void menu_ctrl(MenuCommand) {}
|
||||
|
||||
private:
|
||||
const KeyList& m_keys;
|
||||
|
@ -749,6 +750,45 @@ void exec_string(const CommandParameters& params, Context& context)
|
|||
exec_keys(keys, context);
|
||||
}
|
||||
|
||||
int menu_select(const memoryview<String>& choices, Client& client)
|
||||
{
|
||||
int selected = 0;
|
||||
client.show_menu(choices);
|
||||
while (true)
|
||||
{
|
||||
Key key = client.get_key();
|
||||
if (key == Key(Key::Modifiers::Control, 'n') or
|
||||
key == Key(Key::Modifiers::None, 'j'))
|
||||
{
|
||||
client.menu_ctrl(MenuCommand::SelectNext);
|
||||
selected = std::min(selected+1, (int)choices.size()-1);
|
||||
}
|
||||
if (key == Key(Key::Modifiers::Control, 'p') or
|
||||
key == Key(Key::Modifiers::None, 'k'))
|
||||
{
|
||||
client.menu_ctrl(MenuCommand::SelectPrev);
|
||||
selected = std::max(selected-1, 0);
|
||||
}
|
||||
if (key == Key(Key::Modifiers::Control, 'm'))
|
||||
{
|
||||
client.menu_ctrl(MenuCommand::Close);
|
||||
return selected;
|
||||
}
|
||||
if (key == Key(Key::Modifiers::None, 27))
|
||||
{
|
||||
client.menu_ctrl(MenuCommand::Close);
|
||||
return -1;
|
||||
}
|
||||
if (key.modifiers == Key::Modifiers::None and
|
||||
key.key >= '0' and key.key <= '9')
|
||||
{
|
||||
client.menu_ctrl(MenuCommand::Close);
|
||||
return key.key - '0' - 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void menu(const CommandParameters& params, Context& context)
|
||||
{
|
||||
ParametersParser parser(params, { { "auto-single", false } });
|
||||
|
@ -767,8 +807,7 @@ void menu(const CommandParameters& params, Context& context)
|
|||
for (int i = 0; i < count; i += 2)
|
||||
choices.push_back(parser[i]);
|
||||
|
||||
int i = context.client().menu(choices);
|
||||
|
||||
int i = menu_select(choices, context.client()) + 1;
|
||||
if (i > 0 and i < (count / 2) + 1)
|
||||
CommandManager::instance().execute(parser[(i-1)*2+1], context);
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
#include "window.hh"
|
||||
#include "register_manager.hh"
|
||||
|
||||
#include <ncurses.h>
|
||||
#include <menu.h>
|
||||
#include <map>
|
||||
|
||||
#define CTRL(x) x - 'a' + 1
|
||||
|
@ -13,6 +11,7 @@ namespace Kakoune
|
|||
{
|
||||
|
||||
NCursesClient::NCursesClient()
|
||||
: m_menu(nullptr)
|
||||
{
|
||||
// setlocale(LC_ALL, "");
|
||||
initscr();
|
||||
|
@ -317,50 +316,46 @@ void NCursesClient::print_status(const String& status)
|
|||
refresh();
|
||||
}
|
||||
|
||||
int NCursesClient::menu(const memoryview<String>& choices)
|
||||
void NCursesClient::show_menu(const memoryview<String>& choices)
|
||||
{
|
||||
std::vector<ITEM*> items;
|
||||
std::vector<String> counts;
|
||||
for (int i = 0; i < choices.size(); ++i)
|
||||
counts.push_back(int_to_str(i+1));
|
||||
for (int i = 0; i < choices.size(); ++i)
|
||||
items.push_back(new_item(counts[i].c_str(), choices[i].c_str()));
|
||||
items.push_back(NULL);
|
||||
MENU* menu = new_menu(&items[0]);
|
||||
assert(m_menu == nullptr);
|
||||
m_choices = std::vector<String>(choices.begin(), choices.end());
|
||||
for (int i = 0; i < m_choices.size(); ++i)
|
||||
m_counts.push_back(int_to_str(i+1));
|
||||
for (int i = 0; i < m_choices.size(); ++i)
|
||||
m_items.push_back(new_item(m_counts[i].c_str(), m_choices[i].c_str()));
|
||||
m_items.push_back(NULL);
|
||||
m_menu = new_menu(&m_items[0]);
|
||||
int max_x,max_y;
|
||||
getmaxyx(stdscr, max_y, max_x);
|
||||
int pos_y = max_y - std::min(10, (int)choices.size()) - 1;
|
||||
set_menu_sub(menu, derwin(stdscr, max_y - pos_y - 1, max_x, pos_y, 0));
|
||||
post_menu(menu);
|
||||
|
||||
int res = -1;
|
||||
while (true)
|
||||
{
|
||||
int c = getch();
|
||||
if ('0' <= c and c <= '9')
|
||||
{
|
||||
res = c - '0';
|
||||
break;
|
||||
}
|
||||
if (c == KEY_DOWN or c == CTRL('n'))
|
||||
menu_driver(menu, REQ_DOWN_ITEM);
|
||||
if (c == KEY_UP or c == CTRL('p'))
|
||||
menu_driver(menu, REQ_UP_ITEM);
|
||||
if (c == 27)
|
||||
break;
|
||||
if (c == '\r')
|
||||
{
|
||||
res = item_index(current_item(menu)) + 1;
|
||||
break;
|
||||
}
|
||||
int pos_y = max_y - std::min(10, (int)m_choices.size()) - 1;
|
||||
set_menu_sub(m_menu, derwin(stdscr, max_y - pos_y - 1, max_x, pos_y, 0));
|
||||
post_menu(m_menu);
|
||||
}
|
||||
|
||||
unpost_menu(menu);
|
||||
for (auto item : items)
|
||||
void NCursesClient::menu_ctrl(MenuCommand command)
|
||||
{
|
||||
switch(command)
|
||||
{
|
||||
case MenuCommand::SelectNext:
|
||||
menu_driver(m_menu, REQ_DOWN_ITEM);
|
||||
break;
|
||||
case MenuCommand::SelectPrev:
|
||||
menu_driver(m_menu, REQ_UP_ITEM);
|
||||
break;
|
||||
case MenuCommand::Close:
|
||||
{
|
||||
unpost_menu(m_menu);
|
||||
free_menu(m_menu);
|
||||
for (auto item : m_items)
|
||||
if (item)
|
||||
free_item(item);
|
||||
free_menu(menu);
|
||||
return res;
|
||||
m_menu = nullptr;
|
||||
m_items.clear();
|
||||
m_counts.clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
#ifndef ncurses_hh_INCLUDED
|
||||
#define ncurses_hh_INCLUDED
|
||||
|
||||
#include <ncurses.h>
|
||||
#include <menu.h>
|
||||
|
||||
#include "client.hh"
|
||||
|
||||
namespace Kakoune
|
||||
|
@ -21,7 +24,13 @@ public:
|
|||
String prompt(const String& prompt, const Context& context, Completer completer);
|
||||
Key get_key();
|
||||
|
||||
int menu(const memoryview<String>& choices);
|
||||
void show_menu(const memoryview<String>& choices);
|
||||
void menu_ctrl(MenuCommand command);
|
||||
private:
|
||||
MENU* m_menu;
|
||||
std::vector<ITEM*> m_items;
|
||||
std::vector<String> m_counts;
|
||||
std::vector<String> m_choices;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user