tShow loaded text - ve - a minimal text editor (work in progress)
 (HTM) git clone git://src.adamsgaard.dk/ve
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 980ae0e805b66f9a92d2c36a920a1b6380e227a4
 (DIR) parent 812ddc49f3f4312a59bf50452a2b5384160dd415
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Tue,  6 Aug 2019 11:58:40 +0200
       
       Show loaded text
       
       Diffstat:
         M byote.h                             |       8 ++++++++
         M input.c                             |       2 +-
         A io.c                                |      17 +++++++++++++++++
         A io.h                                |       6 ++++++
         M main.c                              |       2 ++
         M output.c                            |      22 +++++++++++++++-------
         M terminal.c                          |       1 +
       
       7 files changed, 50 insertions(+), 8 deletions(-)
       ---
 (DIR) diff --git a/byote.h b/byote.h
       t@@ -6,9 +6,17 @@
        #define PROGNAME "byote"
        #define VERSION "0.0.1"
        
       +/* editor row: stores a row of text */
       +typedef struct eRow {
       +        int size;
       +        char *chars;
       +} eRow;
       +
        struct editor_config {
                int cursor_x, cursor_y;
                int screen_rows, screen_cols;
       +        int num_rows;
       +        eRow row;
                struct termios orig_termios;
                int status_height;
                int mode; /* 0: normal, 1: insert, 2: visual */
 (DIR) diff --git a/input.c b/input.c
       t@@ -39,7 +39,7 @@ editor_process_keypress()
                if (E.mode == 0) {  /* normal mode */
                        switch (c) {
                                case CTRL_KEY('q'):
       -                                write(STDOUT_FILENO, "\x1b[2J", 4);
       +                                write(STDOUT_FILENO, "\x1b[2J", 4); /* clear screen */
                                        write(STDOUT_FILENO, "\x1b[H", 3);
                                        exit(0);
                                        break;
 (DIR) diff --git a/io.c b/io.c
       t@@ -0,0 +1,17 @@
       +#include <stdlib.h>
       +#include <sys/types.h>
       +#include <string.h>
       +#include "byote.h"
       +
       +void
       +file_open()
       +{
       +        char *line = "Hello, world!";
       +        ssize_t linelen = 13;
       +
       +        E.row.size = linelen;
       +        E.row.chars = malloc(linelen + 1);
       +        memcpy(E.row.chars, line, linelen);
       +        E.row.chars[linelen] = '\0';
       +        E.num_rows = 1;
       +}
 (DIR) diff --git a/io.h b/io.h
       t@@ -0,0 +1,6 @@
       +#ifndef IO_H_
       +#define IO_H_
       +
       +void file_open();
       +
       +#endif
 (DIR) diff --git a/main.c b/main.c
       t@@ -1,6 +1,7 @@
        #include "terminal.h"
        #include "output.h"
        #include "input.h"
       +#include "io.h"
        
        int
        /* main(int argc, char* argv[]) */
       t@@ -8,6 +9,7 @@ main()
        {
                enable_raw_mode();
                init_editor();
       +        file_open();
                while (1) {
                        editor_refresh_screen();
                        editor_process_keypress();
 (DIR) diff --git a/output.c b/output.c
       t@@ -69,17 +69,25 @@ draw_status(struct abuf *ab)
        }
        
        /* draw editor screen.
       - * show tilde characters after EOF */
       + * show tilde characters after EOF, and show status on last line */
        void
        editor_draw_rows(struct abuf *ab)
        {
       -        int y;
       -        for (y = -1; y < E.screen_rows; ++y) {
       +        int y, len;
       +        for (y = 0; y < E.screen_rows; ++y) {
        
       -                if (y == E.screen_rows-1)
       -                        draw_status(ab);
       -                else
       -                        ab_append(ab, "~", 1);
       +                if (y < E.num_rows) {
       +                        len = E.row.size;
       +                        if (len > E.screen_cols)
       +                                len = E.screen_cols;
       +                        ab_append(ab, E.row.chars, len);
       +
       +                } else {
       +                        if (y == E.screen_rows-1)
       +                                draw_status(ab);
       +                        else
       +                                ab_append(ab, "~", 1);
       +                }
        
                        ab_append(ab, "\x1b[K", 3); /* erase to end of line */
                        if (y < E.screen_rows - 1)
 (DIR) diff --git a/terminal.c b/terminal.c
       t@@ -111,6 +111,7 @@ init_editor() {
                E.cursor_x = 0;
                E.cursor_y = 0;
                E.mode = 0;
       +        E.num_rows = 0;
                if (get_window_size(&E.screen_rows, &E.screen_cols) == -1)
                        die("get_window_size");
        }