From 2631698879c8a91aeca932e0b0c4758608cf2d65 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 13 Jul 2017 10:18:40 -0400 Subject: [PATCH] fixed logic to select the command output --- inc/edit.h | 2 +- lib/buf.c | 8 ++++++++ lib/exec.c | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index da3cbc4..0396d24 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -21,7 +21,6 @@ char* strconcat(char* dest, ...); bool file_exists(char* path); char* strmcat(char* first, ...); - enum { INPUT, OUTPUT }; typedef void (*event_cbfn_t)(int fd, void* data); @@ -88,6 +87,7 @@ size_t buf_end(Buf* buf); size_t buf_insert(Buf* buf, bool indent, size_t off, Rune rune); size_t buf_delete(Buf* buf, size_t beg, size_t end); size_t buf_change(Buf* buf, size_t beg, size_t end); +void buf_chomp(Buf* buf); void buf_undo(Buf* buf, Sel* sel); void buf_redo(Buf* buf, Sel* sel); void buf_loglock(Buf* buf); diff --git a/lib/buf.c b/lib/buf.c index 51732b9..782b696 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -197,6 +197,14 @@ size_t buf_change(Buf* buf, size_t beg, size_t end) { return off; } +void buf_chomp(Buf* buf) { + size_t end = buf_end(buf); + if (!end) return; + delete(buf, end-1); + if (buf->undo->insert && buf->undo->data.ins.end == end) + buf->undo->data.ins.end--; +} + void buf_undo(Buf* buf, Sel* sel) { if (!buf->undo) return; uint transid = buf->undo->transid; diff --git a/lib/exec.c b/lib/exec.c index 053d56e..f48be39 100644 --- a/lib/exec.c +++ b/lib/exec.c @@ -136,6 +136,7 @@ typedef struct Job Job; typedef struct { View* view; /* destination view */ + size_t beg; /* start of output */ size_t count; /* number of bytes written */ } Rcvr; @@ -156,6 +157,7 @@ static Job* JobList = NULL; static void send_data(int fd, void* data); static void recv_data(int fd, void* data); static void watch_or_close(bool valid, int dir, int fd, void* data); +static void rcvr_finish(Rcvr* rcvr); void exec_reap(void) { int pid; @@ -163,6 +165,8 @@ void exec_reap(void) { Job* job = JobList; for (; job && job->pid != pid; job = job->next); if (job && job->pid == pid) { + rcvr_finish(&(job->out_rcvr)); + rcvr_finish(&(job->err_rcvr)); if (job->prev) { job->prev->next = job->next; job->next->prev = job->prev; @@ -210,21 +214,31 @@ static void send_data(int fd, void* data) { } static void recv_data(int fd, void* data) { - static char buf[4096]; - long i = 0, nread = read(fd, buf, sizeof(buf)); + static char buffer[4096]; + long i = 0, nread = read(fd, buffer, sizeof(buffer)); Rcvr* rcvr = data; + View* view = rcvr->view; + Buf* buf = &(rcvr->view->buffer); + Sel sel = view->selection; if (nread > 0) { + if (!rcvr->count) { + if (sel.end < sel.beg) { + size_t temp = sel.beg; + sel.beg = sel.end, sel.end = temp; + } + rcvr->beg = sel.beg = sel.end = buf_change(buf, sel.beg, sel.end); + view->selection = sel; + buf_loglock(buf); + } for (; i < nread;) { Rune r; size_t len = 0; - while (!utf8decode(&r, &len, buf[i++])); + while (!utf8decode(&r, &len, buffer[i++])); view_insert(rcvr->view, false, r); rcvr->count++; } } else { close(fd); - if (rcvr->count) - view_selprev(rcvr->view); } } @@ -235,3 +249,14 @@ static void watch_or_close(bool valid, int dir, int fd, void* data) { else close(fd); } + +static void rcvr_finish(Rcvr* rcvr) { + if (rcvr->count) { + View* view = rcvr->view; + Buf* buf = &(rcvr->view->buffer); + if (rcvr->view == win_view(TAGS)) + buf_chomp(buf), rcvr->count--; + view->selection.beg = rcvr->beg; + view->selection.end = rcvr->beg + rcvr->count; + } +} -- 2.49.0