Generalize and DRY input taking
This commit is contained in:
parent
91be50c397
commit
569d1c530c
2 changed files with 16 additions and 23 deletions
35
editor.cpp
35
editor.cpp
|
@ -37,30 +37,13 @@ void Editor::print_input(string text) {
|
|||
::move(cur.r, 0);
|
||||
adds(text);
|
||||
}
|
||||
// Taking user input - number
|
||||
count_type Editor::get_number(string prompt) {
|
||||
// Generic input taking
|
||||
template <typename F>
|
||||
string Editor::get_input(string prompt, F func) {
|
||||
print_input(prompt+": ");
|
||||
char ch = '0';
|
||||
string s = "";
|
||||
ch = getch();
|
||||
while(ch >= '0' && ch <= '9') {
|
||||
s += ch;
|
||||
print_input(prompt+": " + s);
|
||||
ch = getch();
|
||||
}
|
||||
|
||||
if(s.size() == 0)
|
||||
return file_offset + cur.r;
|
||||
|
||||
return stoi(s);
|
||||
}
|
||||
// Taking user input - string
|
||||
string Editor::get_string(string prompt) {
|
||||
print_input(prompt+": ");
|
||||
char ch;
|
||||
string s = "";
|
||||
ch = getch();
|
||||
while(ch != ENTER) {
|
||||
char ch = getch();
|
||||
while((ch != ENTER && func(ch)) || ch == BS) {
|
||||
if(ch == BS)
|
||||
s.pop_back();
|
||||
else
|
||||
|
@ -70,6 +53,14 @@ string Editor::get_string(string prompt) {
|
|||
}
|
||||
return s;
|
||||
}
|
||||
// Taking user input - string
|
||||
string Editor::get_string(string prompt) {
|
||||
return get_input(prompt, [](char ch) { return true; });
|
||||
}
|
||||
// Taking user input - number
|
||||
count_type Editor::get_number(string prompt) {
|
||||
return stoi(get_input(prompt, [](char ch) { return ch >= '0' && ch <= '9'; }));
|
||||
}
|
||||
|
||||
|
||||
// TODO undo
|
||||
|
|
|
@ -103,8 +103,10 @@ class Editor{
|
|||
|
||||
// User input
|
||||
void print_input(string text);
|
||||
count_type get_number(string prompt);
|
||||
template <typename F>
|
||||
string get_input(string prompt, F func);
|
||||
string get_string(string prompt);
|
||||
count_type get_number(string prompt);
|
||||
|
||||
// Movement
|
||||
void jump_line_end();
|
||||
|
|
Loading…
Reference in a new issue