cgmnlm

colorful gemini line mode browser
git clone https://git.clttr.info/cgmnlm.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit ea595cdb2ba9d1e443c7ccdccf3842e49e7162a8
parent 3547fd11d57da5c7aa610366d54eef3b47d0b1a4
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 20 Sep 2020 19:09:55 -0400

gmnlm: display plaintext files

Diffstat:
Msrc/gmnlm.c | 45+++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 43 insertions(+), 2 deletions(-)

diff --git a/src/gmnlm.c b/src/gmnlm.c @@ -72,7 +72,7 @@ trim_ws(char *in) static void display_gemini(FILE *tty, struct gemini_response *resp, - struct link **next, bool pagination) + struct link **next, bool pagination) { int nlinks = 0; struct gemini_parser p; @@ -141,6 +141,39 @@ display_gemini(FILE *tty, struct gemini_response *resp, gemini_parser_finish(&p); } +static void +display_plaintext(FILE *tty, struct gemini_response *resp, bool pagination) +{ + struct winsize ws; + int row = 0, col = 0; + ioctl(fileno(tty), TIOCGWINSZ, &ws); + + char buf[BUFSIZ]; + int n; + while ((n = BIO_read(resp->bio, buf, sizeof(buf)) != 0)) { + while (n) { + n -= fwrite(buf, 1, n, tty); + } + } + + (void)pagination; (void)row; (void)col; // TODO: generalize pagination +} + +static void +display_response(FILE *tty, struct gemini_response *resp, + struct link **next, bool pagination) +{ + if (strcmp(resp->meta, "text/gemini") == 0 + || strncmp(resp->meta, "text/gemini;", 12) == 0) { + display_gemini(tty, resp, next, pagination); + return; + } + if (strncmp(resp->meta, "text/", 5) == 0) { + display_plaintext(tty, resp, pagination); + return; + } +} + int main(int argc, char *argv[]) { @@ -201,6 +234,14 @@ main(int argc, char *argv[]) assert(0); // TODO: Prompt } + snprintf(prompt, sizeof(prompt), "\n%s at %s\n" + "[n]: follow Nth link; [o <url>]: open URL; " + "[b]ack; [f]orward; " + "[q]uit\n" + "=> ", + resp.status == GEMINI_STATUS_SUCCESS ? resp.meta : "", + plain_url); + switch (gemini_response_class(resp.status)) { case GEMINI_STATUS_CLASS_INPUT: assert(0); // TODO @@ -216,7 +257,7 @@ main(int argc, char *argv[]) resp.status, resp.meta); break; case GEMINI_STATUS_CLASS_SUCCESS: - display_gemini(tty, &resp, &links, pagination); + display_response(tty, &resp, &links, pagination); break; }