cgmnlm

Unnamed repository; edit this file 'description' to name the repository.
git clone git://code.clttr.info/cgmnlm.git
Log | Files | Refs | README | LICENSE

commit a40dfb562c3abfdf884f963fed337ce558c70c55
parent 2d7f75383cfca3b4076a889fc7c065e06d146609
Author: René Wagner <rwa@clttr.info>
Date:   Mon,  1 Nov 2021 17:18:41 +0100

introduce -T param to automatically open files

Files downloaded to a tmpfile using the t[N] command
will automatically be sent to the default viewer when
this commandline param is set.

This requires xdg-open.

closes #21

Diffstat:
Mdoc/cgmnlm.scd | 3+++
Msrc/gmnlm.c | 21++++++++++++---------
2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/doc/cgmnlm.scd b/doc/cgmnlm.scd @@ -31,3 +31,6 @@ cgmnlm - colored Gemini line-mode browser *-A* Enables alternate text instead of preformatted text by default. + +*-T* + Open files downloaded with *t* command in default viewer (requires *xdg-open*) diff --git a/src/gmnlm.c b/src/gmnlm.c @@ -45,7 +45,7 @@ struct history { }; struct browser { - bool pagination, unicode, alttext; + bool pagination, unicode, alttext, autoopen; int max_width; struct gemini_options opts; struct gemini_tofu tofu; @@ -763,11 +763,7 @@ do_prompts(const char *prompt, struct browser *browser) } if (url[0]) { fprintf(browser->tty, "=> %s\n", url); - if (in[0] == 'e') { - char target[2048] = {0}; - snprintf(target, sizeof(target), "xdg-open %s", url); - if ( !system(target) ) fprintf(browser->tty, "Link send to xdg-open\n"); - } + char tempfile[] = "/tmp/cgmnlm_XXXXXX"; if (in[0] == 't') { struct gemini_response resp; set_url(browser, url, &browser->history); @@ -776,13 +772,17 @@ do_prompts(const char *prompt, struct browser *browser) fprintf(stderr, "Error: %s\n", gemini_strerr(res, &resp)); } else { - char tempfile[] = "/tmp/cgmnlm_XXXXXX"; close(mkstemp(tempfile)); download_resp(browser->tty, resp, tempfile, url); } gemini_response_finish(&resp); set_url(browser, url, NULL); } + if (in[0] == 'e' || browser->autoopen) { + char target[1024]; + snprintf(target, sizeof(target), "xdg-open %s >/dev/null 2>&1", in[0] == 't' ? tempfile : url); + if ( !system(target) ) fprintf(browser->tty, "Link send to xdg-open\n"); + } fprintf(browser->tty, "\n"); } result = PROMPT_AGAIN; @@ -1134,7 +1134,6 @@ repeat: static bool display_plaintext(struct browser *browser, struct gemini_response *resp) { - // TODO: Strip ANSI escape sequences struct winsize ws; int row = 0, col = 0; ioctl(fileno(browser->tty), TIOCGWINSZ, &ws); @@ -1274,6 +1273,7 @@ main(int argc, char *argv[]) struct browser browser = { .pagination = true, .alttext = false, + .autoopen = false, .tofu_mode = TOFU_ASK, .unicode = true, .url = curl_url(), @@ -1282,7 +1282,7 @@ main(int argc, char *argv[]) }; int c; - while ((c = getopt(argc, argv, "hj:PAUW:")) != -1) { + while ((c = getopt(argc, argv, "hj:PTAUW:")) != -1) { switch (c) { case 'h': usage(argv[0]); @@ -1299,6 +1299,9 @@ main(int argc, char *argv[]) return 1; } break; + case 'T': + browser.autoopen = true; + break; case 'A': browser.alttext = true; break;