Resolve enter and backspace in insert mode
This commit is contained in:
parent
4929064b30
commit
9d860991f3
1 changed files with 26 additions and 6 deletions
32
main.cpp
32
main.cpp
|
@ -10,6 +10,8 @@ using std::string;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
#define ESC 27
|
#define ESC 27
|
||||||
|
#define ENTER 10
|
||||||
|
#define BS 127
|
||||||
#define adds(s) addstr(s.c_str())
|
#define adds(s) addstr(s.c_str())
|
||||||
enum mode_type { insert, normal };
|
enum mode_type { insert, normal };
|
||||||
|
|
||||||
|
@ -25,7 +27,7 @@ char get(int r, int s) { return get_line(file_offset+r)[s]; }
|
||||||
void set(int r, int s, char ch) { file.find(file_offset+r)->text[s] = ch; }
|
void set(int r, int s, char ch) { file.find(file_offset+r)->text[s] = ch; }
|
||||||
void add_line(int r, string text = "") { file.insert(file_offset+r, text); }
|
void add_line(int r, string text = "") { file.insert(file_offset+r, text); }
|
||||||
void insert_char(int r, int s, char ch) { file.find(file_offset+r)->text.insert(s, string{ch}); }
|
void insert_char(int r, int s, char ch) { file.find(file_offset+r)->text.insert(s, string{ch}); }
|
||||||
void remove_char(int r, int s) { file.find(file_offset+r)->text.erase(s,1); }
|
void remove_char(int r, int s, int len=1) { file.find(file_offset+r)->text.erase(s,len); }
|
||||||
void remove_line(int r) { file.remove(file_offset+r); }
|
void remove_line(int r) { file.remove(file_offset+r); }
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,8 +47,9 @@ bool save(string filename) {
|
||||||
std::ofstream outfile(filename);
|
std::ofstream outfile(filename);
|
||||||
if(!outfile.good())
|
if(!outfile.good())
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
for(int i = 0; i < file.size(); ++i)
|
for(int i = 0; i < file.size(); ++i)
|
||||||
outfile << get_line(i) << std::endl;
|
outfile << get_line(i-file_offset) << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,6 +73,13 @@ void print_file(int start = 0) {
|
||||||
else
|
else
|
||||||
clear_line(i);
|
clear_line(i);
|
||||||
}
|
}
|
||||||
|
// Splitting lines
|
||||||
|
string split_line(int r, int s) {
|
||||||
|
string line = get_line(r);
|
||||||
|
string ret = line.substr(s, line.size());
|
||||||
|
remove_char(r, s, line.size()-s);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO skoky a přesuny
|
// TODO skoky a přesuny
|
||||||
|
@ -127,10 +137,6 @@ void move_cursor(char ch) {
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
// Init
|
|
||||||
initscr();
|
|
||||||
refresh();
|
|
||||||
|
|
||||||
// Check valid filename and load
|
// Check valid filename and load
|
||||||
if(argc <= 1)
|
if(argc <= 1)
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -138,6 +144,9 @@ int main(int argc, char* argv[]) {
|
||||||
if(load(filename))
|
if(load(filename))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
// Init
|
||||||
|
initscr();
|
||||||
|
refresh();
|
||||||
print_file();
|
print_file();
|
||||||
|
|
||||||
mode_type mode = normal;
|
mode_type mode = normal;
|
||||||
|
@ -183,6 +192,17 @@ int main(int argc, char* argv[]) {
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
case ESC:
|
case ESC:
|
||||||
mode = normal;
|
mode = normal;
|
||||||
|
print_line(row);
|
||||||
|
break;
|
||||||
|
case BS:
|
||||||
|
if(col>0)
|
||||||
|
remove_char(row, --col);
|
||||||
|
print_line(row);
|
||||||
|
break;
|
||||||
|
case ENTER:
|
||||||
|
add_line(row+1, split_line(row, col));
|
||||||
|
print_file(row++);
|
||||||
|
jump_line_end();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
insert_char(row, col++, ch);
|
insert_char(row, col++, ch);
|
||||||
|
|
Loading…
Reference in a new issue