From 2786baa20b5e8e8f2ed9c6b91ebfb3bc3637d115 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 1 Jan 2016 13:00:20 -0500 Subject: [PATCH] Implemented simple paging mechanism --- Makefile | 2 +- source/main.c | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 50ba670..304fa69 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ LD = ${CC} INCS = -Isource/ CPPFLAGS = -D_XOPEN_SOURCE=700 CFLAGS = ${INCS} ${CPPFLAGS} -LDFLAGS = ${LIBS} +LDFLAGS = ${LIBS} -lncurses # commands COMPILE = @echo CC $@; ${CC} ${CFLAGS} -c -o $@ $< diff --git a/source/main.c b/source/main.c index 2184c8b..860c43f 100644 --- a/source/main.c +++ b/source/main.c @@ -1,6 +1,7 @@ /* Includes *****************************************************************************/ #include +#include /* Type Definitions *****************************************************************************/ @@ -20,6 +21,8 @@ typedef struct { /* Globals *****************************************************************************/ File Curr_File = { .name = NULL, .first = NULL, .last = NULL }; +int Max_Row = 0; +int Max_Col = 0; /* Declarations *****************************************************************************/ @@ -32,10 +35,18 @@ static void edit(void); *****************************************************************************/ static void setup(void) { + initscr(); + getmaxyx(stdscr, Max_Row, Max_Col); + raw(); + keypad(stdscr, true); + noecho(); } static void cleanup(void) { + /* shutdown ncurses */ + endwin(); + /* free up malloced data */ Line* line = Curr_File.first; while (line) { Line* deadite = line; @@ -65,11 +76,29 @@ static void load(char* fname) static void edit(void) { - Line* line = Curr_File.first; - while (line) { - printf("%s", line->text); - line = line->next; - } + Line* start = Curr_File.first; + int ch = 0; + do { + /* Handle input */ + switch (ch) { + case 'k': + case KEY_UP: + if(start->prev) start = start->prev; + break; + case 'j': + case KEY_DOWN: + if(start->next) start = start->next; + break; + } + + /* Refresh the screen */ + clear(); + Line* line = start; + for (int i = 0; (i < Max_Row) && line; i++, line = line->next) { + mvprintw(i, 0, "%s", line->text); + } + refresh(); + } while((ch = getch()) != 'q'); } /* Main Routine -- 2.54.0