summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2012-08-30 21:53:22 +0200
committerMaxime Coste <frrrwww@gmail.com>2012-08-30 21:53:22 +0200
commit51e80558d9824a2795c522c38cc58fdffaba0314 (patch)
tree15ca54d1c4a9447683676c4845682be9cf32f341 /src
parent1ca502d23de987de52eaf4ea698b6bd006a3e5bc (diff)
Change client menu api to give more control to the caller
Diffstat (limited to 'src')
-rw-r--r--src/client.hh11
-rw-r--r--src/commands.cc45
-rw-r--r--src/ncurses.cc75
-rw-r--r--src/ncurses.hh11
4 files changed, 97 insertions, 45 deletions
diff --git a/src/client.hh b/src/client.hh
index 8b2d3444..83c6651c 100644
--- a/src/client.hh
+++ b/src/client.hh
@@ -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 {};
diff --git a/src/commands.cc b/src/commands.cc
index 4e09568a..f26e5ac8 100644
--- a/src/commands.cc
+++ b/src/commands.cc
@@ -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);
}
diff --git a/src/ncurses.cc b/src/ncurses.cc
index cde33183..6809c3dd 100644
--- a/src/ncurses.cc
+++ b/src/ncurses.cc
@@ -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 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);
+}
- int res = -1;
- while (true)
+void NCursesClient::menu_ctrl(MenuCommand command)
+{
+ switch(command)
{
- 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;
- }
+ 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);
+ m_menu = nullptr;
+ m_items.clear();
+ m_counts.clear();
+ break;
+ }
}
-
- unpost_menu(menu);
- for (auto item : items)
- if (item)
- free_item(item);
- free_menu(menu);
- return res;
}
}
diff --git a/src/ncurses.hh b/src/ncurses.hh
index 36a2de81..61e140d8 100644
--- a/src/ncurses.hh
+++ b/src/ncurses.hh
@@ -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;
};
}