From 3b872f4ad7e65114becaa596e8de7ccd80125df5 Mon Sep 17 00:00:00 2001 From: Matuush Date: Thu, 17 Apr 2025 18:47:28 +0200 Subject: [PATCH] Enhance user input numbers and strings --- main.cpp | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index 9792980..2ec0173 100644 --- a/main.cpp +++ b/main.cpp @@ -43,16 +43,6 @@ void remove(position p, count_type len=1) { file.find(p.r)->text.erase(p.c,len); void remove(count_type r) { file.remove(r); } void append(string t) { file.append(t); } -// Taking user input -count_type get_number() { - char ch = '0'; - string s = ""; - while(ch >= '0' && ch <= '9') { - s += ch; - ch = getch(); - } - return stoi(s); -} // Load file to buffer bool load(string filename) { @@ -103,6 +93,41 @@ void split_line(position p) { remove(p, line.size()-p.c); new_line(p.r+1, newline); } +// Print input +void print_input(string text) { + clear_line(cur.r); + move(cur.r, 0); + adds(text); +} +// Taking user input - number +count_type get_number(string prompt) { + print_input(prompt+": "); + char ch = '0'; + string s = ""; + ch = getch(); + while(ch >= '0' && ch <= '9') { + s += ch; + print_input(prompt+": " + s); + ch = getch(); + } + return stoi(s); +} +// Taking user input - string +string get_string(string prompt) { + print_input(prompt+": "); + char ch; + string s = ""; + ch = getch(); + while(ch != ENTER) { + if(ch == BS) + s.pop_back(); + else + s += ch; + print_input(prompt+": " + s); + ch = getch(); + } + return s; +} // Copying void copy_selection(position p = cur + file_offset) { @@ -255,7 +280,7 @@ int main(int argc, char* argv[]) { move_cursor(ch); break; case 'g': - jump(get_number()); + jump(get_number("line number")); print_file(file_offset); break; case 'd': @@ -323,7 +348,7 @@ int main(int argc, char* argv[]) { move_cursor(ch); break; case 'g': - jump(get_number()); + jump(get_number("line number")); print_file(file_offset); break; case 'v': @@ -331,7 +356,6 @@ int main(int argc, char* argv[]) { mode = NORMAL; break; default: - addch('d'); mode = NORMAL; break; }