tAdd buffer display on screen - sex - libtermbox based text editor
 (HTM) git clone git://z3bra.org/sex
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit dcdb608624245c647451aae016c041669f8fe156
 (DIR) parent aa6e1c2e0b096ce764e3d97b920de7944975b93a
 (HTM) Author: z3bra <willyatmailoodotorg>
       Date:   Wed, 25 Nov 2015 00:52:52 +0100
       
       Add buffer display on screen
       
       Loads buffer in memory and display the whole buffer in termbox.
       Tabulations are expanded to the predefined value, and the whole buffer
       is displayed.
       
       No cursor for now, no paging either.
       
       Diffstat:
         M sex.c                               |      98 ++++++++++++++++++++++++++-----
       
       1 file changed, 82 insertions(+), 16 deletions(-)
       ---
 (DIR) diff --git a/sex.c b/sex.c
       t@@ -13,6 +13,8 @@
        #include "arg.h"
        #include "sex.h"
        
       +#define TAB_WIDTH 8
       +
        struct ln_s {
                char  *p;
                size_t len;
       t@@ -30,12 +32,14 @@ struct file_s {
        
        static struct file_s f;
        TAILQ_HEAD(ln_s_head, ln_s) head;
       +struct ln_s *cur;
        
        void
       -cleanup(void) {
       +cleanup(void)
       +{
                munmap(f.map, f.size);
                close(f.d);
       -        /* tb_shutdown(); */
       +        tb_shutdown();
        }
        
        void
       t@@ -47,7 +51,6 @@ init_termbox(void)
                if (r)
                        err(1, "tb_init()");
        }
       -
        void
        open_file(char *path)
        {
       t@@ -87,22 +90,87 @@ open_file(char *path)
                                TAILQ_INSERT_TAIL(&head, tmp, entries);
                        }
                }
       +        cur = TAILQ_FIRST(&head);
       +}
       +
       +ssize_t
       +tb_expand_tab(int x, int y, uint8_t tw)
       +{
       +        uint8_t i, tabsize = 0;
       +
       +        tabsize = tw - (x % tw);
       +        for (i=x; i<(x + tabsize); i++)
       +                tb_change_cell(i, y, ' ', TB_DEFAULT, TB_DEFAULT);
       +
       +        return tabsize;
        }
        
        void
       -edit(char *path) {
       -        open_file(path);
       +tb_print(char *str, int x, int y, int len) {
       +        int n = 0;
       +
       +        while (*str) {
       +                uint32_t uni;
       +
       +                switch(*str) {
       +                case '\t':
       +                        x += tb_expand_tab(x, y, TAB_WIDTH);
       +                        str++;
       +                        break;
       +                default:
       +                        str += tb_utf8_char_to_unicode(&uni, str);
       +                        tb_change_cell(x, y, uni, TB_DEFAULT, TB_DEFAULT);
       +                        x++;
       +                }
       +
       +                if (++n == len)
       +                        return;
       +        }
        }
        
       -ssize_t
       -write_file(int d)
       -{
       -        ssize_t s = 0;
       +void
       +tb_redraw(void) {
       +        /* redraw screen */
                struct ln_s *tmp;
       -        TAILQ_FOREACH(tmp, &head, entries)
       -                s += write(d, tmp->p, tmp->len);
       +        int i = 0;
       +
       +        TAILQ_FOREACH(tmp, &head, entries) {
       +                tb_print(tmp->p, 0, i++, tmp->len);
       +        }
       +        tb_present();
       +}
       +
       +void
       +edit(char *path)
       +{
       +        int needs_redraw;
       +        struct tb_event ev;
       +
       +        open_file(path);
       +        tb_redraw();
       +
       +        while (tb_poll_event(&ev)) {
       +                switch (ev.type) {
       +                case TB_EVENT_KEY:
       +                        /* handle keys */
       +                        if (ev.key == TB_KEY_CTRL_C)
       +                                return;
       +                        break;
       +                case 'j':
       +                        cur = TAILQ_NEXT(cur, entries);
       +                        needs_redraw = 1;
       +                        break;
       +                case TB_EVENT_RESIZE:
       +                        /* handle resizing */
       +                        needs_redraw = 1;
       +                        break;
       +                }
        
       -        return s;
       +                if (needs_redraw) {
       +                        tb_redraw();
       +                        needs_redraw = 0;
       +                }
       +        }
        }
        
        int
       t@@ -118,13 +186,11 @@ main(int argc, char **argv)
                        break;
                } ARGEND
        
       -        /* init_termbox();  */
       +        init_termbox();
                atexit(cleanup);
        
       -        while (*argv) {
       +        while (*argv)
                        edit(*argv++);
       -                write_file(1);
       -        }
        
                return 0;
        }