some progress with vi_redraw
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 25 Nov 2019 01:44:51 +0000 (03:44 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 25 Nov 2019 01:44:51 +0000 (03:44 +0200)
libvisor/include/visor.h
libvisor/src/visor.c
visor/src/main_unix.c
visor/src/term.c
visor/src/term.h

index 4d2233a..46de4d8 100644 (file)
@@ -97,6 +97,7 @@ struct vi_ttyops {
        void (*del_back)(void *cls);
        void (*del_fwd)(void *cls);
        void (*status)(char *s, void *cls);
+       void (*flush)(void *cls);
 };
 
 /* Create a new instance of the visor editor.
@@ -111,7 +112,7 @@ void vi_destroy(struct visor *vi);
 void vi_set_fileops(struct visor *vi, struct vi_fileops *fop);
 void vi_set_ttyops(struct visor *vi, struct vi_ttyops *tty);
 
-void vi_window(struct visor *vi, int xsz, int ysz);
+void vi_term_size(struct visor *vi, int xsz, int ysz);
 void vi_redraw(struct visor *vi);
 
 /* vi_new_buf creates a new buffer and inserts it in the buffer list. If the
index 3e1df92..86d508b 100644 (file)
@@ -43,6 +43,7 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #define vi_del_back()          vi->tty.del_back(vi->tty_cls)
 #define vi_del_fwd()           vi->tty.del_fwd(vi->tty_cls)
 #define vi_status(s)           vi->tty.status(s, vi->tty_cls)
+#define vi_flush()                     vi->tty.flush(vi->tty_cls)
 
 static int remove_buf(struct visor *vi, struct vi_buffer *vb);
 static int add_span(struct vi_buffer *vb, vi_addr at, int src, vi_addr start, unsigned long size);
@@ -99,20 +100,57 @@ void vi_term_size(struct visor *vi, int xsz, int ysz)
 
 void vi_redraw(struct visor *vi)
 {
+       int i, col, cur_x = 0, cur_y = 0;
+       char c;
        struct vi_buffer *vb;
-       struct vi_span *sp, *spend;
-       vi_addr spoffs;
+       struct vi_span *sp, *spans_end;
+       const char *tptr, *tend;
+       vi_addr spoffs, addr;
 
        vb = vi->buflist;
-       if(!(sp = vi_buf_find_span(vb, vi->view_start, &spoffs))) {
+       if(!(sp = vi_buf_find_span(vb, vb->view_start, &spoffs))) {
                sp = vb->spans;
                spoffs = 0;
        }
-       spend = vb->spans + vb->num_spans;
+       spans_end = vb->spans + vb->num_spans;
+
+       tptr = vi_buf_span_text(vb, sp);
+       tend = tptr + sp->size;
+       tptr += spoffs;
 
        vi_clear();
 
-       for(i=0; i<term_heig
+       addr = vb->view_start;
+       for(i=0; i<vi->term_height; i++) {
+               vi_setcursor(0, i);
+               col = -vb->view_xscroll - 1;
+               while(++col < vi->term_width && (c = (addr++, *tptr++)) != '\n') {
+                       if(addr == vb->cursor) {
+                               cur_x = col;
+                               cur_y = i;
+                       }
+
+                       if(col >= 0) {
+                               vi_putchar(c);
+                       }
+                       if(tptr >= tend) {
+                               if(++sp >= spans_end) {
+                                       goto end;
+                               }
+                               tptr = vi_buf_span_text(vb, sp);
+                               tend = tptr + sp->size;
+                       }
+               }
+       }
+end:
+
+       while(i < vi->term_height) {
+               vi_setcursor(0, i++);
+               vi_putchar('~');
+       }
+
+       vi_setcursor(cur_x, cur_y);
+       vi_flush();
 }
 
 struct vi_buffer *vi_new_buf(struct visor *vi, const char *path)
index a9d6932..647b7da 100644 (file)
@@ -26,6 +26,19 @@ static void file_unmap(vi_file *file);
 static long file_read(vi_file *file, void *buf, long count);
 static long file_write(vi_file *file, void *buf, long count);
 static long file_seek(vi_file *file, long offs, int whence);
+/* tty operations */
+static void tty_clear(void *cls);
+static void tty_clear_line(void *cls);
+static void tty_clear_line_at(int y, void *cls);
+static void tty_setcursor(int x, int y, void *cls);
+static void tty_putchar(char c, void *cls);
+static void tty_putchar_at(int x, int y, char c, void *cls);
+static void tty_scroll(int nlines, void *cls);
+static void tty_del_back(void *cls);
+static void tty_del_fwd(void *cls);
+static void tty_status(char *s, void *cls);
+static void tty_flush(void *cls);
+
 
 static struct visor *vi;
 
@@ -42,13 +55,11 @@ static struct vi_fileops fops = {
        file_read, file_write, file_seek
 };
 
-/*
 static struct vi_ttyops ttyops = {
        tty_clear, tty_clear_line, tty_clear_line_at,
        tty_setcursor, tty_putchar, tty_putchar_at,
-       tty_scroll, tty_del_back, tty_del_fwd, tty_status
+       tty_scroll, tty_del_back, tty_del_fwd, tty_status, tty_flush
 };
-*/
 
 int main(int argc, char **argv)
 {
@@ -106,12 +117,15 @@ static int init(void)
                return -1;
        }
        vi_set_fileops(vi, &fops);
+       vi_set_ttyops(vi, &ttyops);
 
        for(i=0; i<num_fpaths; i++) {
                if(!vi_new_buf(vi, fpaths[i])) {
                        return -1;
                }
        }
+
+       term_resize_func(resized);
        return 0;
 }
 
@@ -123,6 +137,11 @@ static void cleanup(void)
        term_cleanup();
 }
 
+static void resized(int x, int y)
+{
+       vi_term_size(vi, x, y);
+}
+
 static vi_file *file_open(const char *path, unsigned int flags)
 {
        struct file *file;
@@ -203,3 +222,62 @@ static long file_seek(vi_file *vif, long offs, int whence)
        struct file *file = vif;
        return lseek(file->fd, offs, whence);
 }
+
+/* tty operations */
+
+static void tty_clear(void *cls)
+{
+       term_clear();
+}
+
+static void tty_clear_line(void *cls)
+{
+       /* TODO */
+}
+
+static void tty_clear_line_at(int y, void *cls)
+{
+       term_setcursor(y, 0);
+       /* TODO */
+}
+
+static void tty_setcursor(int x, int y, void *cls)
+{
+       term_setcursor(y, x);
+}
+
+static void tty_putchar(char c, void *cls)
+{
+       term_putchar(c);
+}
+
+static void tty_putchar_at(int x, int y, char c, void *cls)
+{
+       term_setcursor(y, x);
+       term_putchar(c);
+}
+
+static void tty_scroll(int nlines, void *cls)
+{
+       /* TODO */
+}
+
+static void tty_del_back(void *cls)
+{
+       /* TODO */
+}
+
+static void tty_del_fwd(void *cls)
+{
+       /* TODO */
+}
+
+static void tty_status(char *s, void *cls)
+{
+       /* TODO */
+}
+
+static void tty_flush(void *cls)
+{
+       term_flush();
+}
index ee93e03..af7d278 100644 (file)
@@ -91,6 +91,11 @@ void term_send(const char *s, int size)
        }
 }
 
+void term_putchar(char c)
+{
+       term_send(&c, 1);
+}
+
 void term_puts(const char *s)
 {
        term_send(s, strlen(s));
@@ -141,6 +146,16 @@ void term_clear(void)
        term_puts("\033[2J");
 }
 
+void term_cursor(int show)
+{
+       term_printf("\033[?25%c", show ? 'h' : 'l');
+}
+
+void term_setcursor(int row, int col)
+{
+       term_printf("\033[%d;%dH", row + 1, col + 1);
+}
+
 int term_getchar(void)
 {
        int res;
index 82ccd16..c1ce67d 100644 (file)
@@ -41,11 +41,14 @@ void term_getsize(int *width, int *height);
 void term_resize_func(void (*func)(int, int));
 
 void term_send(const char *s, int size);
+void term_putchar(char c);
 void term_puts(const char *s);
 void term_printf(const char *fmt, ...);
 void term_flush(void);
 
 void term_clear(void);
+void term_setcursor(int row, int col);
+
 int term_getchar(void);
 
 #endif /* TERM_H_ */