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

Loading…
Cancel
Save