handle backspace & ^u
This commit is contained in:
parent
94181e94bd
commit
11fc6c5d86
2 changed files with 16 additions and 4 deletions
19
console.cpp
19
console.cpp
|
@ -88,6 +88,13 @@ void console::flush_input()
|
||||||
input_buffer.clear();
|
input_buffer.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void console::emit_backspace()
|
||||||
|
{
|
||||||
|
put_char(8);
|
||||||
|
put_char(' ');
|
||||||
|
put_char(8);
|
||||||
|
}
|
||||||
|
|
||||||
std::string console::read_line(const std::string & prompt)
|
std::string console::read_line(const std::string & prompt)
|
||||||
{
|
{
|
||||||
put_string(prompt);
|
put_string(prompt);
|
||||||
|
@ -107,15 +114,19 @@ std::string console::read_line(const std::string & prompt)
|
||||||
if (c == 13 || c == 10)
|
if (c == 13 || c == 10)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (c == 8) {
|
if (c == 8 || c == 127) { // backspace
|
||||||
if (!str.empty()) {
|
if (!str.empty()) {
|
||||||
str = str.substr(0, str.size() - 1);
|
str = str.substr(0, str.size() - 1);
|
||||||
|
|
||||||
put_char(8);
|
emit_backspace();
|
||||||
put_char(' ');
|
|
||||||
put_char(8);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (c == 21) { // ^u
|
||||||
|
for(size_t i=0; i<str.size(); i++)
|
||||||
|
emit_backspace();
|
||||||
|
|
||||||
|
str.clear();
|
||||||
|
}
|
||||||
else if (c >= 32 && c < 127) {
|
else if (c >= 32 && c < 127) {
|
||||||
str += c;
|
str += c;
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,7 @@ public:
|
||||||
std::string read_line(const std::string & prompt);
|
std::string read_line(const std::string & prompt);
|
||||||
void flush_input();
|
void flush_input();
|
||||||
|
|
||||||
|
void emit_backspace();
|
||||||
void put_char(const char c);
|
void put_char(const char c);
|
||||||
void put_string(const std::string & what);
|
void put_string(const std::string & what);
|
||||||
virtual void put_string_lf(const std::string & what) = 0;
|
virtual void put_string_lf(const std::string & what) = 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue