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