diff --git a/src/bytenuts.c b/src/bytenuts.c index 34fa4bd..de3b57b 100644 --- a/src/bytenuts.c +++ b/src/bytenuts.c @@ -16,15 +16,15 @@ "USAGE\n\n" \ "bytenuts [OPTIONS] \n" \ "\nConfigs get loaded from ${HOME}/.bytenuts/config (if file exists)\n" \ -"\nOPTIONS\n\n" \ -"-h: show this help\n" \ -"-b : set a baud rate (default 115200)\n" \ -"-l : log all output to the given file\n" \ -"-c : load a config from the given path rather than the default\n" \ -"--colors=<0|1>: turn 8-bit ANSI colors off/on\n" \ -"--echo=<0|1>: turn input echoing off/on\n" \ -"--no_crlf=<0|1>: choose to send LF and not CRLF on input\n" \ -"--escape=: change the default ctrl+b escape character\n" \ +"\n OPTIONS\n=========\n\n" \ +"-h\n Show this help.\n\n" \ +"-b \n Set a baud rate (default 115200).\n\n" \ +"-l \n Log all output to the given file.\n\n" \ +"-c \n Load a config from the given path rather than the default.\n\n" \ +"--colors=<0|1>\n Turn 8-bit ANSI colors off/on.\n\n" \ +"--echo=<0|1>\n Turn input echoing off/on.\n\n" \ +"--no_crlf=<0|1>\n Choose to send LF and not CRLF on input.\n\n" \ +"--escape=\n Change the default ctrl+b escape character.\n" \ ) static int parse_args(int argc, char **argv); diff --git a/src/ingest.c b/src/ingest.c index 2da66b2..9271a0e 100644 --- a/src/ingest.c +++ b/src/ingest.c @@ -601,39 +601,66 @@ handle_functions(int ch) static int inbuf_load() { - int cx, cy = -1; + static int start = 0; + int startx = 0, starty = 0; + int cx = -1, cy = -1; int win_len = getmaxx(ingest.input) + 1; - size_t prepend_len = ingest.prepend ? strlen(ingest.prepend) : 0; - int buffer_len = win_len - prepend_len; - int start = ingest.inlen > (win_len + prepend_len) ? - (ingest.inlen - win_len - prepend_len - 1) : 0; pthread_mutex_lock(ingest.term_lock); werase(ingest.input); wmove(ingest.input, 0, 0); - if (ingest.prepend) + if (ingest.prepend) { wprintw(ingest.input, "%s", ingest.prepend); - - if (start > 0) { - waddch(ingest.input, '$'); - start++; + getyx(ingest.input, starty, startx); } if (start > ingest.inpos) { start = ingest.inpos; - } else if ((start + buffer_len) < ingest.inpos) { - start = ingest.inpos - buffer_len; + } + /* this is not perfect as some characters are wider than one space, but it + * makes the algorithm for printing less redundant */ + else if (ingest.inpos > (start + win_len)) { + start = ingest.inpos - win_len; } - for (int i = start; i < ingest.inlen; i++) { - if (i == ingest.inpos) - getyx(ingest.input, cy, cx); - waddch(ingest.input, ingest.inbuf[i]); + int i = 0; + int hit_cursor = 0; + while (1) { + int p = i + start; + int iy, ix; + i++; + + getyx(ingest.input, iy, ix); + + /* nothing left to write... */ + if (p >= ingest.inlen) + break; + + /* print the cursor at this position */ + if (p == ingest.inpos) { + cy = iy; + cx = ix; + hit_cursor = 1; + } + + /* we want to leave a blank character for printing the cursor at the end of the line */ + if (ix >= (win_len-2)) { + /* we printed the cursor, so we can break */ + if (hit_cursor) + break; + /* otherwise, we didn't print the cursor, so start over */ + i = 0; + start++; + wmove(ingest.input, starty, startx); + continue; + } + + waddch(ingest.input, ingest.inbuf[p]); } if (cx < 0) { - cx = ingest.inlen; + cx = ingest.inlen < win_len ? ingest.inlen : win_len - 1; cy = 0; }