]> khamidou — GitHub mirrors - te.git/commitdiff
refactoring display code
authorkhamidou <karim@lambda-expr.org>
Fri, 3 Apr 2009 21:01:12 +0000 (23:01 +0200)
committerkhamidou <karim@lambda-expr.org>
Fri, 3 Apr 2009 21:01:12 +0000 (23:01 +0200)
buffer.c
buffer.h
keyb.c
keyb.h
main.c
screen.c
screen.h

index fc847e76926145ec73c142e9b52c2ad2a795cf34..fd26db06e810b7799692557d61c428d8a98dd075 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <curses.h>
 
 #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)
index 47248ee2b5fa7c8b0aad347b3c7c9e41e53b55a1..08c9eda7fafe8fd0494a09d467184f0d6e76d775 100644 (file)
--- 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 189a22db936a06cb9c5dd7b9970850063a8f5476..72e9d50967b88f7c279c868d1627ce84269a5dad 100644 (file)
--- 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 cdb75a15d431fea771b68c19d7df11fd7aa2553e..003c3653f445ee17b3d5282b19b879af5d619ed4 100644 (file)
--- a/keyb.h
+++ b/keyb.h
@@ -2,7 +2,11 @@
 #define __KEYB_H__
 
 #include <stdlib.h>
+#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 5dc4c3fcb34b7fc1778489926c161c470908b216..80a909a4957169688ee85afc385a18548edcd5e0 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,19 +4,18 @@
 
 #include <stdio.h>
 #include <curses.h>
+#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();
 
 }
index 6e37e3d758ada0ea718136299b5b49c8b1af38e3..4c7d3c88476071c67e9431d4c34bba7e1727e052 100644 (file)
--- 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);
 
index 0d95fb1e2ce95dad97c4af797ae6dffbea58b2a6..64b42c0cc33a62e349b0e2418960c5f4dce8bb01 100644 (file)
--- 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, ...);