commit 3d1e56e24cc45422090c9457cb8afd84a2289761
parent b7c63e3e10ff2318e97c1a8f1e74e5632e2e6ca9
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Thu, 19 May 2022 00:00:29 +0200
Add d[N] to download Nth link
I wanted to add an optional [N] to 'd' and '|' to download/pipe the
Nth link instead of the current page. After getting the first one done,
I found out that '|' already has this option, so I fixed the relative
help line without changing that command.
All in all I don't know how useful this patch is, considering that
'[N]|' was already there. Feel free to discard it and just keep the
documentation change.
Cheers!
Diffstat:
M | src/gmnlm.c | | | 53 | ++++++++++++++++++++++++++++++++++++----------------- |
1 file changed, 36 insertions(+), 17 deletions(-)
diff --git a/src/gmnlm.c b/src/gmnlm.c
@@ -102,9 +102,8 @@ const char *help_msg =
"l\t\tSearch backlinks to current URI via geminispace.info\n"
"/<text>\t\tSearch for text (POSIX regular expression)\n"
"n\t\tJump to next search match\n"
- "d [path>\tDownload page to path, uses pwd & filename if path is omitted\n"
- "|<prog>\t\tPipe page into program\n"
- "[N]|<prog>\tPipe content of Nth link into program\n"
+ "d[N] [path]\tDownload page, or Nth link, to path\n"
+ "[N]|<prog>\tPipe page, or Nth link, into program\n"
"a\t\tToggle usage of alt text instead of preformatted text\n"
"q\t\tQuit\n"
"\n"
@@ -804,22 +803,42 @@ do_prompts(const char *prompt, struct browser *browser)
result = PROMPT_AGAIN;
goto exit;
case 'd':
- if (in[1] != '\0' && !isspace(in[1])) break;
- struct gemini_response resp;
- strncpy(&url[0], browser->plain_url, sizeof(url)-1);
- // XXX: may affect history, do we care?
- enum gemini_result res = do_requests(browser, &resp);
- if (res != GEMINI_OK) {
- fprintf(stderr, "Error: %s\n",
- gemini_strerr(res, &resp));
- result = PROMPT_AGAIN;
- goto exit;
+ endptr = &in[1];
+ char *d_url = browser->plain_url;
+ if (in[1] != '\0' && !isspace(in[1])) {
+ struct link *link = browser->links;
+ int linksel = (int)strtol(in+1, &endptr, 10);
+ while (linksel > 0 && link) {
+ link = link->next;
+ --linksel;
+ }
+
+ if (!link) {
+ fprintf(stderr, "Error: no such link.\n");
+ break;
+ } else {
+ d_url = link->url;
+ }
}
+ struct gemini_response resp;
+ char url[1024] = {0}, old_url[1024] = {0};
+ strncpy(&old_url[0], browser->plain_url, sizeof(url)-1);
+ strncpy(&url[0], d_url, sizeof(url)-1);
+ // XXX: may affect history, do we care?
set_url(browser, url, NULL);
- download_resp(browser->tty, resp, trim_ws(&in[1]), url);
- gemini_response_finish(&resp);
- result = PROMPT_AGAIN;
- goto exit;
+ enum gemini_result res = do_requests(browser, &resp);
+ if (res != GEMINI_OK) {
+ fprintf(stderr, "Error: %s\n",
+ gemini_strerr(res, &resp));
+ result = PROMPT_AGAIN;
+ set_url(browser, old_url, NULL);
+ goto exit;
+ }
+ download_resp(browser->tty, resp, trim_ws(endptr), url);
+ gemini_response_finish(&resp);
+ result = PROMPT_AGAIN;
+ set_url(browser, old_url, NULL);
+ goto exit;
case '|':
strncpy(&url[0], browser->plain_url, sizeof(url)-1);
res = do_requests(browser, &resp);