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 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;
}