Enhance user input numbers and strings

This commit is contained in:
Matúš Púll 2025-04-17 18:47:28 +02:00
parent 9a25e1e1f2
commit 3b872f4ad7

View file

@ -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 remove(count_type r) { file.remove(r); }
void append(string t) { file.append(t); } 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 // Load file to buffer
bool load(string filename) { bool load(string filename) {
@ -103,6 +93,41 @@ void split_line(position p) {
remove(p, line.size()-p.c); remove(p, line.size()-p.c);
new_line(p.r+1, newline); 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 // Copying
void copy_selection(position p = cur + file_offset) { void copy_selection(position p = cur + file_offset) {
@ -255,7 +280,7 @@ int main(int argc, char* argv[]) {
move_cursor(ch); move_cursor(ch);
break; break;
case 'g': case 'g':
jump(get_number()); jump(get_number("line number"));
print_file(file_offset); print_file(file_offset);
break; break;
case 'd': case 'd':
@ -323,7 +348,7 @@ int main(int argc, char* argv[]) {
move_cursor(ch); move_cursor(ch);
break; break;
case 'g': case 'g':
jump(get_number()); jump(get_number("line number"));
print_file(file_offset); print_file(file_offset);
break; break;
case 'v': case 'v':
@ -331,7 +356,6 @@ int main(int argc, char* argv[]) {
mode = NORMAL; mode = NORMAL;
break; break;
default: default:
addch('d');
mode = NORMAL; mode = NORMAL;
break; break;
} }