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);
|
::move(cur.r, 0);
|
||||||
adds(text);
|
adds(text);
|
||||||
}
|
}
|
||||||
// Taking user input - number
|
// Generic input taking
|
||||||
count_type Editor::get_number(string prompt) {
|
template <typename F>
|
||||||
|
string Editor::get_input(string prompt, F func) {
|
||||||
print_input(prompt+": ");
|
print_input(prompt+": ");
|
||||||
char ch = '0';
|
|
||||||
string s = "";
|
string s = "";
|
||||||
ch = getch();
|
char ch = getch();
|
||||||
while(ch >= '0' && ch <= '9') {
|
while((ch != ENTER && func(ch)) || ch == BS) {
|
||||||
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) {
|
|
||||||
if(ch == BS)
|
if(ch == BS)
|
||||||
s.pop_back();
|
s.pop_back();
|
||||||
else
|
else
|
||||||
|
@ -70,6 +53,14 @@ string Editor::get_string(string prompt) {
|
||||||
}
|
}
|
||||||
return s;
|
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
|
// TODO undo
|
||||||
|
|
|
@ -103,8 +103,10 @@ class Editor{
|
||||||
|
|
||||||
// User input
|
// User input
|
||||||
void print_input(string text);
|
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);
|
string get_string(string prompt);
|
||||||
|
count_type get_number(string prompt);
|
||||||
|
|
||||||
// Movement
|
// Movement
|
||||||
void jump_line_end();
|
void jump_line_end();
|
||||||
|
|
Loading…
Reference in a new issue