Support files longer than terminal line-count

This commit is contained in:
Matúš Púll 2025-04-07 20:58:41 +02:00
parent 895c4ab075
commit 5642cfc408

View file

@ -17,23 +17,16 @@ enum mode_type { insert, normal };
// Global variables
treap file;
int row = 0, col = 0;
int file_offset = 0;
// Accessing the file
string get_line(int r) { return file.find(r)->text; }
char get(int r, int s) { return get_line(r)[s]; }
void set(int r, int s, char ch) { file.find(r)->text[s] = ch; }
void add_line(int r, string text = "") { file.insert(r, text); }
void remove_line(int r) { file.remove(r); }
void insert_char(int r, int s, char ch) { file.find(r)->text.insert(s, string{ch}); }
void remove_char(int r, int s) { file.find(r)->text.erase(s,1); }
// 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);
}
string get_line(int r) { return file.find(file_offset+r)->text; }
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 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 remove_char(int r, int s) { file.find(file_offset+r)->text.erase(s,1); }
void remove_line(int r) { file.remove(file_offset+r); }
// Load file to buffer
@ -47,7 +40,6 @@ bool load(string filename) {
file.insert(file.size(), line);
return 0;
}
// Save file from buffer
bool save(string filename) {
std::ofstream outfile(filename);
@ -58,30 +50,30 @@ bool save(string filename) {
return 0;
}
// Print line
void print_line(int pos) {
move(pos, 0);
clrtoeol();
move(pos, 0);
adds(get_line(pos));
move(row, col);
}
// Print file content
// TODO only visible part
void print_file() {
clear();
for(int i = 0; i < file.size(); i++) {
move(i, 0);
adds(get_line(i));
}
move(row, col);
}
// Print line
void print_line(int r) {
move(r, 0);
clrtoeol();
move(r, 0);
adds(get_line(r));
for(int i = 0; i < LINES && file_offset+i < file.size(); i++)
print_line(i);
move(row, col);
}
// TODO skoky a přesuny
// TODO kopirování
// TODO hledání a nahrazování
// TODO undo
// Jump to end of line
void jump_line_end() {
int line_size = get_line(row).size();
@ -90,7 +82,6 @@ void jump_line_end() {
move(row, col);
}
}
// Cursor movement
void move_cursor(char ch) {
switch(ch) {
@ -100,10 +91,16 @@ void move_cursor(char ch) {
break;
case 'j':
if(row < file.size()-1) {
if(file_offset + row >= file.size()-1) break;
if(row < LINES-1) {
move(++row, col);
jump_line_end();
}
else if(row == LINES-1) {
file_offset++;
jump_line_end();
print_file();
}
break;
case 'k':
@ -111,6 +108,11 @@ void move_cursor(char ch) {
move(--row, col);
jump_line_end();
}
else if(file_offset > 0) {
file_offset--;
jump_line_end();
print_file();
}
break;
case 'l':
@ -120,6 +122,7 @@ void move_cursor(char ch) {
}
}
int main(int argc, char* argv[]) {
// Init
@ -143,7 +146,7 @@ int main(int argc, char* argv[]) {
char ch = getch();
switch(mode) {
case normal:
step_back();
print_line(row);
switch(ch) {
case 'h': case 'j': case 'k': case 'l':
move_cursor(ch);
@ -169,7 +172,7 @@ int main(int argc, char* argv[]) {
case 'i':
mode = insert;
break;
case ESC:
default:
break;
}
break;
@ -187,9 +190,6 @@ int main(int argc, char* argv[]) {
refresh();
}
if(argc > 1 && save(argv[1]))
return 1;
// End
endwin();
return 0;