From: khamidou Date: Fri, 3 Apr 2009 21:01:12 +0000 (+0200) Subject: refactoring display code X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=59cd0f0f1f66d1861865aaa24d5af3fac9d75926;p=te.git refactoring display code --- diff --git a/buffer.c b/buffer.c index fc847e7..fd26db0 100644 --- a/buffer.c +++ b/buffer.c @@ -1,4 +1,5 @@ #include +#include #include "zalloc.h" #include "error.h" @@ -38,6 +39,22 @@ struct te_buffer *alloc_and_insert_buffer(void) return b; } +struct te_char *nth_char(struct te_char *c, int n) +{ + if (c == NULL) + return; + + struct te_char *r = c; + int i = 0; + + for(i = 0; r != NULL && i < n; i++) + r = TAILQ_NEXT(c, chars); + + statusprintf("r is %x, r->contents are : %c", r, r->contents); + + return r; +} + void free_char(struct te_buffer *b, struct te_char *c) { if (b == NULL || c == NULL) @@ -76,6 +93,11 @@ struct te_buffer* load_buffer(char *filename) } statusprintf("%s", filename); + + b->point = TAILQ_FIRST(&b->chars_head); + b->scr_top = b->point; + b->scr_bottom = nth_char(b->point, (LINES - 3) * COLS); + return b; } @@ -86,7 +108,12 @@ void scroll_up(struct te_buffer *buf) void scroll_down(struct te_buffer *buf) { + if (buf == NULL || buf->point == NULL) + return; + struct te_char *c = buf->point; + + } void move_left(struct te_buffer *buf) diff --git a/buffer.h b/buffer.h index 47248ee..08c9eda 100644 --- a/buffer.h +++ b/buffer.h @@ -13,7 +13,10 @@ struct te_buffer { char *name; int dirty; int x, y; + struct te_char *point; + struct te_char *scr_top, *scr_bottom; /* the first and last char of the screen */ + TAILQ_HEAD(te_chars_head, te_char) chars_head; TAILQ_ENTRY(te_buffer) buffers; }; @@ -22,6 +25,7 @@ TAILQ_HEAD(te_buffers_head, te_buffer) buffers_head; struct te_char *alloc_and_insert_char(struct te_buffer *buf, char contents); struct te_buffer *alloc_and_insert_buffer(void); +struct te_char *nth_char(struct te_char *c, int n); void free_char(struct te_buffer *b, struct te_char *c); void free_buffer(struct te_buffer *b); struct te_buffer* load_buffer(char *filename); diff --git a/keyb.c b/keyb.c index 189a22d..72e9d50 100644 --- a/keyb.c +++ b/keyb.c @@ -8,6 +8,10 @@ void input_loop(void) switch(c) { case 'q': exit(0); + + case 'k': + screen_move_down(current_buf); + break; } } diff --git a/keyb.h b/keyb.h index cdb75a1..003c365 100644 --- a/keyb.h +++ b/keyb.h @@ -2,7 +2,11 @@ #define __KEYB_H__ #include +#include "buffer.h" +#include "screen.h" void input_loop(void); +struct te_buffer *current_buf; + #endif diff --git a/main.c b/main.c index 5dc4c3f..80a909a 100644 --- a/main.c +++ b/main.c @@ -4,19 +4,18 @@ #include #include +#include "screen.h" +extern struct te_buffer *current_buf; int main(int argc, char **argv) { init_buffers(); init_windows(); - struct te_buffer *b = load_buffer("screen.c"); - paint_buffer(b); - miniprintf("Over the top!"); - - - + current_buf = load_buffer("screen.c"); + paint_buffer(current_buf); + refresh(); input_loop(); } diff --git a/screen.c b/screen.c index 6e37e3d..4c7d3c8 100644 --- a/screen.c +++ b/screen.c @@ -5,10 +5,11 @@ void init_windows(void) initscr(); cbreak(); noecho(); - scrollok(stdscr, TRUE); keypad(stdscr, TRUE); buffer_win = subwin(stdscr, LINES - 2, COLS, 0, 0); + idlok(stdscr, TRUE); + scrollok(buffer_win, FALSE); status_win = subwin(stdscr, 1, COLS, LINES - 2, 0); wattron(status_win, A_REVERSE); @@ -31,38 +32,67 @@ void paint_buffer(struct te_buffer *buf) if (buf == NULL) return; + wmove(buffer_win, 0, 0); + struct te_char *c; - int y, x; + int i = 0; - TAILQ_FOREACH(c, &buf->chars_head, chars) { - getyx(stdscr, y, x); - if (y >= LINES -2) + for(c = buf->scr_top; c != buf->scr_bottom; c = TAILQ_NEXT(c, chars)) { + if (++i > (LINES - 3) * COLS) break; - - waddch(buffer_win, c->contents); + else + waddch(buffer_win, c->contents); } - + miniprintf("%d", i); + wmove(buffer_win, 0, 0); refresh(); } -void screen_move_left(void) +void screen_move_left(struct te_buffer *buf) { } -void screen_move_right(void) +void screen_move_right(struct te_buffer *buf) { } -void screen_move_up(void) +void screen_move_up(struct te_buffer *buf) { + if (buf == NULL) + return; + if (buf->scr_top == NULL || buf->scr_bottom == NULL) + fail("FATAL: buf->scr_top or buf->scr_bottom is NULL\n"); + + scrollok(buffer_win, TRUE); + wscrl(buffer_win, -1); + + wmove(buffer_win, LINES - 3, 0); + struct te_char *c = buf->scr_bottom; + + for (c = buf->scr_bottom; c != NULL && c->contents != '\n'; c = TAILQ_NEXT(c, chars)) + addch(c->contents); + + buf->scr_bottom = c; + + /* Update scr_top accordingly */ + c = buf->scr_top; + for (c = buf->scr_top; c != NULL && c->contents != '\n'; c = TAILQ_NEXT(c, chars)) + continue; + + buf->scr_top = c; + + scrollok(buffer_win, FALSE); + + wrefresh(buffer_win); } -void screen_move_down(void) +void screen_move_down(struct te_buffer *buf) { - + scroll(buffer_win); + wrefresh(buffer_win); } void statusprintf(char *fmt, ...) @@ -94,6 +124,7 @@ void miniprintf(char *fmt, ...) { va_list ap; va_start(ap, fmt); + werase(minibuffer_win); vwprintw(minibuffer_win, fmt, ap); va_end(ap); diff --git a/screen.h b/screen.h index 0d95fb1..64b42c0 100644 --- a/screen.h +++ b/screen.h @@ -13,10 +13,10 @@ void init_windows(void); void cleanup_windows(void); void paint_buffer(struct te_buffer *buf); -void screen_move_left(void); -void screen_move_right(void); -void screen_move_up(void); -void screen_move_down(void); +void screen_move_left(struct te_buffer *buf); +void screen_move_right(struct te_buffer *buf); +void screen_move_up(struct te_buffer *buf); +void screen_move_down(struct te_buffer *buf); void statusprintf(char *fmt, ...); void miniprintf(char *fmt, ...);