Input wrapping prints the end properly

This commit is contained in:
Aaron Cook 2022-04-30 16:14:54 -05:00
commit a99a224a60
2 changed files with 53 additions and 26 deletions

View file

@ -16,15 +16,15 @@
"USAGE\n\n" \ "USAGE\n\n" \
"bytenuts [OPTIONS] <serial path>\n" \ "bytenuts [OPTIONS] <serial path>\n" \
"\nConfigs get loaded from ${HOME}/.bytenuts/config (if file exists)\n" \ "\nConfigs get loaded from ${HOME}/.bytenuts/config (if file exists)\n" \
"\nOPTIONS\n\n" \ "\n OPTIONS\n=========\n\n" \
"-h: show this help\n" \ "-h\n Show this help.\n\n" \
"-b <baud>: set a baud rate (default 115200)\n" \ "-b <baud>\n Set a baud rate (default 115200).\n\n" \
"-l <path>: log all output to the given file\n" \ "-l <path>\n Log all output to the given file.\n\n" \
"-c <path>: load a config from the given path rather than the default\n" \ "-c <path>\n Load a config from the given path rather than the default.\n\n" \
"--colors=<0|1>: turn 8-bit ANSI colors off/on\n" \ "--colors=<0|1>\n Turn 8-bit ANSI colors off/on.\n\n" \
"--echo=<0|1>: turn input echoing off/on\n" \ "--echo=<0|1>\n Turn input echoing off/on.\n\n" \
"--no_crlf=<0|1>: choose to send LF and not CRLF on input\n" \ "--no_crlf=<0|1>\n Choose to send LF and not CRLF on input.\n\n" \
"--escape=<char>: change the default ctrl+b escape character\n" \ "--escape=<char>\n Change the default ctrl+b escape character.\n" \
) )
static int parse_args(int argc, char **argv); static int parse_args(int argc, char **argv);

View file

@ -601,39 +601,66 @@ handle_functions(int ch)
static int static int
inbuf_load() 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; 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); pthread_mutex_lock(ingest.term_lock);
werase(ingest.input); werase(ingest.input);
wmove(ingest.input, 0, 0); wmove(ingest.input, 0, 0);
if (ingest.prepend) if (ingest.prepend) {
wprintw(ingest.input, "%s", ingest.prepend); wprintw(ingest.input, "%s", ingest.prepend);
getyx(ingest.input, starty, startx);
if (start > 0) {
waddch(ingest.input, '$');
start++;
} }
if (start > ingest.inpos) { if (start > ingest.inpos) {
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++) { int i = 0;
if (i == ingest.inpos) int hit_cursor = 0;
getyx(ingest.input, cy, cx); while (1) {
waddch(ingest.input, ingest.inbuf[i]); 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) { if (cx < 0) {
cx = ingest.inlen; cx = ingest.inlen < win_len ? ingest.inlen : win_len - 1;
cy = 0; cy = 0;
} }