Added methods for delete, started working on left and right arrows

master
Aadhavan Srinivasan 2 years ago
parent b0add8f26a
commit 102a0ed093

@ -49,16 +49,27 @@ void buffer_insert(char ch, Buffer* buffer) {
} }
} }
void buffer_delete(Buffer* buffer) {
if (buffer->start != buffer->text) {
buffer->start--;
buffer->gap_size++;
}
}
void buffer_right(Buffer* buffer) { void buffer_right(Buffer* buffer) {
if (buffer->end != buffer->text + buffer->size) {
buffer->start++; buffer->start++;
buffer->end--; buffer->end++;
}
} }
void buffer_left(Buffer* buffer) { void buffer_left(Buffer* buffer) {
if (buffer->start != buffer->text) {
char c = *(buffer->start - 1);
buffer->start--; buffer->start--;
buffer->end--; buffer->end--;
*(buffer->end) = c;
}
} }
void init_curses() { void init_curses() {
@ -79,6 +90,8 @@ int main() {
Buffer* buffer = new_buffer(10); Buffer* buffer = new_buffer(10);
init_curses(); init_curses();
int ch; int ch;
int y, x;
while (true) { while (true) {
clear(); clear();
@ -91,24 +104,36 @@ int main() {
i++; i++;
if ((buffer->start - buffer->text) == i) { /* If we have encountered if ((buffer->start - buffer->text) == i) { /* If we have encountered
the start of the gap */ the start of the gap */
getyx(stdscr,y,x);
i += buffer->gap_size; i += buffer->gap_size;
} }
} }
move(y,x);
} }
ch = getch(); ch = getch();
switch(ch) { switch(ch) {
case KEY_BACKSPACE: case KEY_BACKSPACE:
if (buffer->start != buffer->text) { buffer_delete(buffer);
buffer->start--; break;
buffer->gap_size++;
} case KEY_LEFT:
goto continue_while_loop; buffer_left(buffer);
break;
case KEY_RIGHT:
buffer_right(buffer);
break;
default: default:
}
buffer_insert(ch,buffer); buffer_insert(ch,buffer);
}
continue_while_loop: continue_while_loop:
} }

Loading…
Cancel
Save