sex

simple editor extended using libtermbox
git clone git://z3bra.org/sex
Log | Files | Refs | README

commit dcdb608624245c647451aae016c041669f8fe156
parent aa6e1c2e0b096ce764e3d97b920de7944975b93a
Author: z3bra <willyatmailoodotorg>
Date:   Wed Nov 25 00:52:52 2015

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:
 sex.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 82 insertions(+), 16 deletions(-)

diff --git a/sex.c b/sex.c @@ -13,6 +13,8 @@ #include "arg.h" #include "sex.h" +#define TAB_WIDTH 8 + struct ln_s { char *p; size_t len; @@ -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 @@ -47,7 +51,6 @@ init_termbox(void) if (r) err(1, "tb_init()"); } - void open_file(char *path) { @@ -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 @@ -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; }