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 <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h>
#define ctrl(x) ((x) & 0x1f)
int index_to_start = 0; 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() { void init_curses() {
initscr(); initscr();
noecho(); noecho();
keypad(stdscr,TRUE); keypad(stdscr,TRUE);
cbreak(); raw();
// scrollok(stdscr,TRUE); // scrollok(stdscr,TRUE);
} }
void sigint_handler(int dummy) { void end_ncurses() {
endwin(); endwin();
exit(130); exit(130);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
signal(SIGINT,sigint_handler);
Buffer* buffer = new_buffer(10); Buffer* buffer = new_buffer(10);
FILE* logfile = fopen("logfile.txt","w"); FILE* logfile = fopen("logfile.txt","w");
@ -246,13 +278,25 @@ int main(int argc, char** argv) {
break; break;
case KEY_NPAGE: case KEY_NPAGE:
page_down_handler(buffer); page_down_handler(buffer);
break; break;
case KEY_PPAGE: case KEY_PPAGE:
page_up_handler(buffer); page_up_handler(buffer);
break; 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: default:
buffer_insert(ch,buffer); buffer_insert(ch,buffer);

Loading…
Cancel
Save