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