From 5fb7e79ad5f7d55e2a196905aad181f750631dd9 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 13 May 2023 23:24:34 -0400 Subject: [PATCH] feat: add shortcuts plugin --- plugins/shortcuts/README.md | 3 ++ plugins/shortcuts/shortcuts.c | 79 +++++++++++++++++++++++++++++++++++ plugins/shortcuts/shortcuts.h | 6 +++ 3 files changed, 88 insertions(+) create mode 100644 plugins/shortcuts/README.md create mode 100644 plugins/shortcuts/shortcuts.c create mode 100644 plugins/shortcuts/shortcuts.h diff --git a/plugins/shortcuts/README.md b/plugins/shortcuts/README.md new file mode 100644 index 0000000..724c5d6 --- /dev/null +++ b/plugins/shortcuts/README.md @@ -0,0 +1,3 @@ +## About + +This code automatically redirects shortcuts to their longer expansions. Similar to DuckDuckGo's bangs () diff --git a/plugins/shortcuts/shortcuts.c b/plugins/shortcuts/shortcuts.c new file mode 100644 index 0000000..bc25bbf --- /dev/null +++ b/plugins/shortcuts/shortcuts.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#include "../libre_redirect/str_replace_start.h" + +#define SHORTCUT_N 20 + +/* Uncomment for debug */ +/* #define DEBUG */ + +/* Inspired by https://duckduckgo.com/bangs */ + +void str_init(char* str, int n) +{ + for (int i = 0; i < n; i++) + str[i] = ' '; + str[n] = '\0'; +} // could also use + +int shortcut_expand(const char* uri, char* output) +{ + int l1 = strlen(uri); + int l2 = strlen(output); + int len; + char tmp_uri[l2++]; + char tmp_output[l2++]; + + if ((l2 - l1) < SHORTCUT_N) { +#ifdef DEBUG + printf("Not enough memory\n"); +#endif + return 1; // not enough memory. + } else { + strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0' + strcpy(tmp_output, output); + + char* shortcuts[] = { + "!fnf", + "!fnc", + "!hn", + "!hnb" + }; + + char* expansions[] = { + "https://forum.nunosempere.com/frontpage", + "https://forum.nunosempere.com/comments", + "https://news.ycombinator.com", + "https://news.ycombinator.com/best", + }; + + // len = sizeof(shortcuts) / sizeof(shortcuts[0]); + len = sizeof(shortcuts) / sizeof(char *); + + for (int i = 0; i < len; i++) { + int replace_check = str_replace_start(tmp_uri, shortcuts[i], + expansions[i], output); + if (replace_check == 2) { +#ifdef DEBUG + printf("tmp_uri: %s\n", tmp_uri); + printf("output: %s\n", output); +#endif + return 2; + } else if (replace_check == 1) { +#ifdef DEBUG + printf("replace_check failed\n"); +#endif + return 1; + } + strcpy(tmp_uri, output); + str_init(output, l2); + } + strcpy(output, tmp_uri); + } +#ifdef DEBUG + printf("No match found\n\n"); +#endif + return 0; +} diff --git a/plugins/shortcuts/shortcuts.h b/plugins/shortcuts/shortcuts.h new file mode 100644 index 0000000..00d08bd --- /dev/null +++ b/plugins/shortcuts/shortcuts.h @@ -0,0 +1,6 @@ +#pragma once + +#define SHORTCUT_N 20 + +int shortcut_expand(const char* uri, char* output); +void str_init(char* str, int n);