history support in prompt
This commit is contained in:
parent
14d17a4493
commit
589b8e68bb
46
src/main.cc
46
src/main.cc
|
@ -218,29 +218,63 @@ std::string ncurses_prompt(const std::string& text, Completer completer = comple
|
||||||
addstr(text.c_str());
|
addstr(text.c_str());
|
||||||
clrtoeol();
|
clrtoeol();
|
||||||
|
|
||||||
std::string result;
|
|
||||||
size_t cursor_pos = 0;
|
size_t cursor_pos = 0;
|
||||||
|
|
||||||
Completions completions;
|
Completions completions;
|
||||||
int current_completion = -1;
|
int current_completion = -1;
|
||||||
std::string text_before_completion;
|
std::string text_before_completion;
|
||||||
|
|
||||||
|
std::string result;
|
||||||
|
std::string saved_result;
|
||||||
|
|
||||||
|
static std::unordered_map<std::string, std::vector<std::string>> history_per_prompt;
|
||||||
|
std::vector<std::string>& history = history_per_prompt[text];
|
||||||
|
auto history_it = history.end();
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
char c = getch();
|
int c = getch();
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '\r':
|
case '\r':
|
||||||
|
{
|
||||||
|
std::vector<std::string>::iterator it;
|
||||||
|
while ((it = find(history, result)) != history.end())
|
||||||
|
history.erase(it);
|
||||||
|
|
||||||
|
history.push_back(result);
|
||||||
return result;
|
return result;
|
||||||
case 4:
|
}
|
||||||
|
case KEY_UP:
|
||||||
|
if (history_it != history.begin())
|
||||||
|
{
|
||||||
|
if (history_it == history.end())
|
||||||
|
saved_result = result;
|
||||||
|
--history_it;
|
||||||
|
result = *history_it;
|
||||||
|
cursor_pos = result.length();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case KEY_DOWN:
|
||||||
|
if (history_it != history.end())
|
||||||
|
{
|
||||||
|
++history_it;
|
||||||
|
if (history_it != history.end())
|
||||||
|
result = *history_it;
|
||||||
|
else
|
||||||
|
result = saved_result;
|
||||||
|
cursor_pos = result.length();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case KEY_LEFT:
|
||||||
if (cursor_pos > 0)
|
if (cursor_pos > 0)
|
||||||
--cursor_pos;
|
--cursor_pos;
|
||||||
break;
|
break;
|
||||||
case 5:
|
case KEY_RIGHT:
|
||||||
if (cursor_pos < result.length())
|
if (cursor_pos < result.length())
|
||||||
++cursor_pos;
|
++cursor_pos;
|
||||||
break;
|
break;
|
||||||
case 7:
|
case KEY_BACKSPACE:
|
||||||
if (cursor_pos != 0)
|
if (cursor_pos != 0)
|
||||||
{
|
{
|
||||||
result = result.substr(0, cursor_pos - 1)
|
result = result.substr(0, cursor_pos - 1)
|
||||||
|
@ -288,7 +322,7 @@ std::string ncurses_prompt(const std::string& text, Completer completer = comple
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
current_completion = -1;
|
current_completion = -1;
|
||||||
result = result.substr(0, cursor_pos) + c + result.substr(cursor_pos, std::string::npos);
|
result = result.substr(0, cursor_pos) + (char)c + result.substr(cursor_pos, std::string::npos);
|
||||||
++cursor_pos;
|
++cursor_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user