Added a window within the stdscr, which is used for printing the text, as well as another 'status window'

master
Aadhavan Srinivasan 2 years ago
parent d4021068c8
commit 315245dd54

@ -10,6 +10,8 @@
#define ctrl(x) ((x) & 0x1f)
int index_to_start = 0;
WINDOW* mainwin;
WINDOW* statusbar;
typedef struct Buffer_struct Buffer;
struct Buffer_struct {
@ -104,6 +106,10 @@ void scroll_page_down_handler(Buffer* buffer) {
}
void page_down_handler(Buffer* buffer) {
if ((getcury(mainwin) + 1) == getmaxy(mainwin)) {
scroll_page_down_handler(buffer);
}
buffer_right(buffer); /* I must advance the cursor at least
once, so this hardcoded statement is fine. */
@ -112,10 +118,6 @@ void page_down_handler(Buffer* buffer) {
buffer_right(buffer);
}
if ((getcury(stdscr) + 1) == getmaxy(stdscr)) {
scroll_page_down_handler(buffer);
}
/* You would think that I need to call 'buffer_right' once
more, to advance the cursor onto the next line. In fact, if
you think about it, the place where the cursor (the rectangle)
@ -150,7 +152,7 @@ void page_up_handler(Buffer* buffer) {
}
if ((getcury(stdscr) == 0) && (index_to_start > 0)) {
if ((getcury(mainwin) == 0) && (index_to_start > 0)) {
scroll_page_up_handler(buffer);
}
@ -183,8 +185,17 @@ void save_text_helper(Buffer* buffer,char* filename) {
void init_curses() {
initscr();
mainwin = newwin(getmaxy(stdscr)-1,getmaxx(stdscr),0,0);
statusbar = newwin(1,getmaxx(stdscr),getmaxy(stdscr)-1,0);
wattrset(statusbar,A_REVERSE);
mvwhline(statusbar,0,0,' ',getmaxx(statusbar));
mvwprintw(statusbar,0,0,"Welcome to Editor!");
wrefresh(statusbar);
noecho();
keypad(stdscr,TRUE);
keypad(mainwin,TRUE);
raw();
// scrollok(stdscr,TRUE);
}
@ -221,7 +232,7 @@ int main(int argc, char** argv) {
int y, x;
while (true) {
clear();
wclear(mainwin);
int i=index_to_start;
if ((buffer->start != buffer->text) || (buffer->gap_size != buffer->size)) { /* We don't want to print the string, if the
@ -230,28 +241,30 @@ int main(int argc, char** argv) {
while (i < buffer->size) {
if ((buffer->start - buffer->text) == i) { /* If we have encountered
the start of the gap */
getyx(stdscr,y,x);
getyx(mainwin,y,x);
i += buffer->gap_size;
}
if (i >= buffer->size) {
break;
}
addch(*(buffer->text + i));
waddch(mainwin,*(buffer->text + i));
i++;
fprintf(logfile,"At line %d of %d\n",getcury(stdscr),getmaxy(stdscr));
if (*(buffer->text + i) == '\n' && ((getcury(stdscr) + 1) == getmaxy(stdscr))) {
fprintf(logfile,"At line %d of %d\n",getcury(mainwin),getmaxy(mainwin));
if (*(buffer->text + i) == '\n' && ((getcury(mainwin) + 1) == getmaxy(mainwin))) {
fprintf(logfile,"Read newline\n");
break;
}
}
move(y,x);
wmove(mainwin,y,x);
wrefresh(mainwin);
}
ch = getch();
ch = wgetch(mainwin);
switch(ch) {
case KEY_BACKSPACE:
@ -287,6 +300,9 @@ int main(int argc, char** argv) {
page_up_handler(buffer);
break;
case ctrl(KEY_NPAGE):
abort();
case ctrl('s'):
if (argc == 2) {
save_text_helper(buffer,argv[1]);

Loading…
Cancel
Save