From 1c950cf29a2769a38465e0ead09fc767d1630ce8 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 10 May 2018 23:03:04 -0400 Subject: [PATCH] added colors to status box --- inc/edit.h | 4 +++- lib/buf.c | 15 +++++++-------- lib/x11.c | 10 ++++++---- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index 29db7d5..ce7a424 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -17,6 +17,9 @@ typedef struct { /* gap buffer main data structure */ typedef struct { + enum { + NORMAL = 0, MODIFIED, OUTDATED, ERRORED + } status; char* path; /* the path to the open file */ uint64_t modtime; /* modification time of the opened file */ size_t bufsize; /* size of the buffer in runes */ @@ -26,7 +29,6 @@ typedef struct { char* gapend; /* end of the gap */ Log* undo; /* undo list */ Log* redo; /* redo list */ - bool modified; /* tracks whether the buffer has been modified */ Sel selection; /* the currently selected text */ } Buf; diff --git a/lib/buf.c b/lib/buf.c index c6a2c7b..57ae900 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -24,7 +24,7 @@ void buf_init(Buf* buf) { buf_logclear(buf); } /* reset the state to defaults */ - buf->modified = false; + buf->status = NORMAL; buf->bufsize = 8192; buf->bufstart = malloc(buf->bufsize); buf->bufend = buf->bufstart + buf->bufsize; @@ -61,8 +61,8 @@ void buf_load(Buf* buf, char* path) { if (fd > 0) close(fd); /* reset buffer state */ - buf->modified = false; - buf->modtime = (uint64_t)sb.st_mtime; + buf->status = NORMAL; + buf->modtime = (uint64_t)sb.st_mtime; buf_logclear(buf); } @@ -87,12 +87,9 @@ void buf_save(Buf* buf) { wptr += nwrite, towrite -= nwrite; close(fd); /* report success or failure */ - if (nwrite >= 0) - buf->modified = false; - //else - // buf->errfn("Failed to write file"); + buf->status = (nwrite >= 0 ? NORMAL : ERRORED); } else { - //buf->errfn("Need a filename: SaveAs "); + buf->status = ERRORED; } } @@ -150,6 +147,7 @@ static void putb(Buf* buf, char b, Sel* p_sel) { *(buf->gapstart++) = b; p_sel->end = p_sel->end + 1u; p_sel->beg = p_sel->end; + buf->status = MODIFIED; } static char getb(Buf* buf, size_t off) { @@ -197,6 +195,7 @@ void buf_del(Buf* buf) { Sel sel = getsel(buf); size_t nbytes = sel.end - sel.beg; if (nbytes > 0) { + buf->status = MODIFIED; //char* str = buf_gets(buf, &sel); syncgap(buf, sel.beg); buf->gapend += nbytes; diff --git a/lib/x11.c b/lib/x11.c index e2613b5..2a4a1d7 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -275,10 +275,12 @@ static void draw_rect(int color, int x, int y, int width, int height) { } static void draw_statbox(drawcsr* csr) { - View* view = win_view(EDIT); - int statclr = Orange; //(view->buffer.modified ? Purple : TagsBg); draw_rect(VerBdr, ScrollWidth, 0, 1, X.height/4); - draw_rect(statclr, 0, 0, ScrollWidth, X.height/4); + switch (win_view(EDIT)->buffer.status) { + case NORMAL: draw_rect(TagsBg, 0, 0, ScrollWidth, X.height/4); break; + case MODIFIED: draw_rect(Purple, 0, 0, ScrollWidth, X.height/4); break; + case ERRORED: draw_rect(Red, 0, 0, ScrollWidth, X.height/4); break; + } } static void draw_view(int i, size_t nrows, drawcsr* csr, int bg, int fg, int sel) { @@ -560,7 +562,7 @@ void win_loop(void) { void win_quit(void) { static uint64_t before = 0; - if (!win_buf(EDIT)->modified || (X.now - before) <= (uint64_t)ClickTime) + if ((win_buf(EDIT)->status != MODIFIED) || (X.now - before) <= (uint64_t)ClickTime) exit(0); before = X.now; } -- 2.49.0