Line inserts/deletes
This commit is contained in:
parent
515a25cee0
commit
571fefc8c0
1 changed files with 67 additions and 18 deletions
85
main.cpp
85
main.cpp
|
@ -15,6 +15,22 @@ enum mode_type { insert, normal };
|
||||||
vector<string> file(0);
|
vector<string> file(0);
|
||||||
int row = 0, col = 0;
|
int row = 0, col = 0;
|
||||||
|
|
||||||
|
// Accessing the file
|
||||||
|
char get(int r, int s) { return file[r][s]; }
|
||||||
|
void set(int r, int s, char ch) { file[r][s] = ch; }
|
||||||
|
string get_line(int r) { return file[r]; }
|
||||||
|
void add_line(int r) { file.insert(file.begin() + r, ""); }
|
||||||
|
void remove_line(int r) { file.erase(file.begin() + r); }
|
||||||
|
|
||||||
|
// For normal mode not to write to terminal
|
||||||
|
void step_back() {
|
||||||
|
move(row, col);
|
||||||
|
char ch = col < get_line(row).size() ? get(row, col) : ' ';
|
||||||
|
addch(ch);
|
||||||
|
move(row, col);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Load file to buffer
|
// Load file to buffer
|
||||||
bool load(string filename) {
|
bool load(string filename) {
|
||||||
std::ifstream infile(filename);
|
std::ifstream infile(filename);
|
||||||
|
@ -38,6 +54,7 @@ bool save(string filename) {
|
||||||
|
|
||||||
// Print file content
|
// Print file content
|
||||||
void print_file() {
|
void print_file() {
|
||||||
|
clear();
|
||||||
for(int i = 0; i < file.size(); i++) {
|
for(int i = 0; i < file.size(); i++) {
|
||||||
move(i, 0);
|
move(i, 0);
|
||||||
adds(file[i]);
|
adds(file[i]);
|
||||||
|
@ -47,18 +64,23 @@ void print_file() {
|
||||||
|
|
||||||
// TODO psaní do souboru
|
// TODO psaní do souboru
|
||||||
// TODO mazání v souboru
|
// TODO mazání v souboru
|
||||||
// TODO vkládání a mazání řádků
|
|
||||||
// TODO skoky a přesuny
|
// TODO skoky a přesuny
|
||||||
// TODO kopirování
|
// TODO kopirování
|
||||||
// TODO hledání a nahrazování
|
// TODO hledání a nahrazování
|
||||||
// TODO undo
|
// TODO undo
|
||||||
|
|
||||||
|
// Jump to end of line
|
||||||
|
void jump_line_end() {
|
||||||
|
int line_size = get_line(row).size();
|
||||||
|
if(col > line_size) {
|
||||||
|
col = line_size;
|
||||||
|
move(row, col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Cursor movement
|
// Cursor movement
|
||||||
void move_cursor(char ch) {
|
void move_cursor(char ch) {
|
||||||
move(row, col);
|
|
||||||
addch(file[row][col]);
|
|
||||||
move(row, col);
|
|
||||||
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
case 'h':
|
case 'h':
|
||||||
if(col > 0)
|
if(col > 0)
|
||||||
|
@ -68,25 +90,19 @@ void move_cursor(char ch) {
|
||||||
case 'j':
|
case 'j':
|
||||||
if(row < file.size()-1) {
|
if(row < file.size()-1) {
|
||||||
move(++row, col);
|
move(++row, col);
|
||||||
if(file[row].size() <= col) {
|
jump_line_end();
|
||||||
col = file[row].size()-1;
|
|
||||||
move(row, col);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'k':
|
case 'k':
|
||||||
if(row > 0) {
|
if(row > 0) {
|
||||||
move(--row, col);
|
move(--row, col);
|
||||||
if(file[row].size() <= col) {
|
jump_line_end();
|
||||||
col = file[row].size()-1;
|
|
||||||
move(row, col);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'l':
|
case 'l':
|
||||||
if(col < file[row].size()-1)
|
if(col < file[row].size())
|
||||||
move(row, ++col);
|
move(row, ++col);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -97,9 +113,12 @@ int main(int argc, char* argv[]) {
|
||||||
// Init
|
// Init
|
||||||
initscr();
|
initscr();
|
||||||
refresh();
|
refresh();
|
||||||
if(argc > 1 && load(argv[1]))
|
|
||||||
|
// Check valid filename and load
|
||||||
|
if(argc <= 1)
|
||||||
return 1;
|
return 1;
|
||||||
if(argc > 1 && save(argv[1]))
|
string filename = argv[1];
|
||||||
|
if(load(filename))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
print_file();
|
print_file();
|
||||||
|
@ -107,12 +126,42 @@ int main(int argc, char* argv[]) {
|
||||||
mode_type mode = normal;
|
mode_type mode = normal;
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
while(true) {
|
bool run = true;
|
||||||
|
while(run) {
|
||||||
char ch = getch();
|
char ch = getch();
|
||||||
move_cursor( ch);
|
switch(mode) {
|
||||||
|
case normal:
|
||||||
|
step_back();
|
||||||
|
switch(ch) {
|
||||||
|
case 'h': case 'j': case 'k': case 'l':
|
||||||
|
move_cursor(ch);
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
remove_line(row);
|
||||||
|
print_file();
|
||||||
|
break;
|
||||||
|
case 'o':
|
||||||
|
add_line(row);
|
||||||
|
print_file();
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
run = false;
|
||||||
|
break;
|
||||||
|
case 'w':
|
||||||
|
save(filename);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case insert:
|
||||||
|
// insert
|
||||||
|
break;
|
||||||
|
}
|
||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(argc > 1 && save(argv[1]))
|
||||||
|
return 1;
|
||||||
|
|
||||||
// End
|
// End
|
||||||
endwin();
|
endwin();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue