Switched to using raw mode, which allows me to capture the 'Ctrl+s' keypress. I have implemented save functionality using this keypress, and implemented functionality to capture Ctrl-C as well.

master
Aadhavan Srinivasan 2 years ago
parent 3b20d6ccf7
commit f4f19c7c39

@ -5,6 +5,9 @@
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/stat.h>
#define ctrl(x) ((x) & 0x1f)
int index_to_start = 0;
@ -146,22 +149,51 @@ void page_up_handler(Buffer* buffer) {
}
int is_file(char* path) {
struct stat st;
if (stat(path, &st) < 0) {
return -1;
}
return S_ISREG(st.st_mode);
}
void save_text_helper(Buffer* buffer,char* filename) {
if (is_file(filename) == 1) {
FILE* file = fopen(filename,"w");
int i = 0;
while (i < buffer->size) {
if ((buffer->start - buffer->text) == i) { /* If we have encountered
the start of the gap */
i += buffer->gap_size;
}
if (i >= buffer->size) {
break;
}
fputc(*(buffer->text + i),file);
i++;
}
}
}
void init_curses() {
initscr();
noecho();
keypad(stdscr,TRUE);
cbreak();
raw();
// scrollok(stdscr,TRUE);
}
void sigint_handler(int dummy) {
void end_ncurses() {
endwin();
exit(130);
}
int main(int argc, char** argv) {
signal(SIGINT,sigint_handler);
Buffer* buffer = new_buffer(10);
FILE* logfile = fopen("logfile.txt","w");
@ -253,6 +285,18 @@ int main(int argc, char** argv) {
page_up_handler(buffer);
break;
case ctrl('s'):
if (argc == 2) {
save_text_helper(buffer,argv[1]);
} else if (argc == 1) {
save_text_helper(buffer,NULL);
}
break;
case ctrl('c'):
end_ncurses();
break;
default:
buffer_insert(ch,buffer);

Loading…
Cancel
Save