add total length argument to pipe callback... - sob - simple output bar
(HTM) git clone git://git.codemadness.org/sob
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 5cfc09e6af0a5fec859dc9938210646ea97da167
(DIR) parent 47706e9bc61a337498f43c7053048932ce617b75
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 12 Oct 2014 22:05:51 +0000
add total length argument to pipe callback...
... so callbacks can detect a first read and do an action like clear the
line, word or other things.
Diffstat:
M sob.c | 28 +++++++++++++++++-----------
1 file changed, 17 insertions(+), 11 deletions(-)
---
(DIR) diff --git a/sob.c b/sob.c
@@ -32,8 +32,8 @@ struct line {
size_t dirtylen; /* dirty length (in columns) */
};
-static void cb_pipe_insert(const char *, size_t);
-static void cb_pipe_replaceword(const char *, size_t);
+static void cb_pipe_insert(const char *, size_t, size_t);
+static void cb_pipe_replaceword(const char *, size_t, size_t);
static void line_clear(void);
static void line_copywordcursor(char *, size_t);
@@ -63,8 +63,10 @@ static int line_promptlen(void);
static int line_pipeto(char **);
static int line_wordpipeto(char **);
-static int pipe_readline(int, int, char *, void (*)(const char *, size_t));
-static int pipe_cmd(char *[], char *, void (*)(const char *, size_t));
+static int pipe_readline(int, int, char *,
+ void (*)(const char *, size_t, size_t));
+static int pipe_cmd(char *[], char *,
+ void (*)(const char *, size_t, size_t));
static void cleanup(void);
static void clear(void);
@@ -548,11 +550,12 @@ line_copywordcursor(char *buf, size_t bufsiz)
static int
pipe_readline(int fd_in, int fd_out, char *writestr,
- void (*f)(const char *, size_t))
+ void (*f)(const char *, size_t, size_t))
{
char buf[PIPE_BUF];
struct timeval tv;
fd_set fdr, fdw;
+ size_t total = 0;
int r, w, maxfd, status = -1, haswritten = 0;
for(;;) {
@@ -584,8 +587,9 @@ pipe_readline(int fd_in, int fd_out, char *writestr,
goto fini;
} else {
buf[r] = '\0';
+ total += r;
if(f)
- f(buf, r);
+ f(buf, r, total);
if(!r) {
status = 0;
goto fini;
@@ -613,7 +617,7 @@ fini:
}
static int
-pipe_cmd(char *cmd[], char *writestr, void (*f)(const char *, size_t))
+pipe_cmd(char *cmd[], char *writestr, void (*f)(const char *, size_t, size_t))
{
struct sigaction sa;
pid_t pid;
@@ -661,20 +665,22 @@ pipe_cmd(char *cmd[], char *writestr, void (*f)(const char *, size_t))
}
static void
-cb_pipe_insert(const char *buf, size_t len)
+cb_pipe_insert(const char *buf, size_t len, size_t total)
{
- if(!len)
+ if(!len || !total)
return;
memset(&line, 0, sizeof(line));
handleinput((unsigned char *)buf, len);
}
static void
-cb_pipe_replaceword(const char *buf, size_t len)
+cb_pipe_replaceword(const char *buf, size_t len, size_t total)
{
if(!len)
return;
- line_delwordcursor();
+ /* first read: delete word under cursor. */
+ if(len == total)
+ line_delwordcursor();
handleinput((unsigned char *)buf, len);
}