foreseen

A text-based gemini browser with prefetching
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

commit f4c8dda85a9ceccf0e875e6a0abc11504e235c08
parent 7c1a6da441572086ba7b12809c6a4cc399dc5a8a
Author: Andrew Laack <andrew@laack.co>
Date:   Thu,  1 Oct 2026 20:12:31 -0500

Argument parsing with positional argument for destination

Diffstat:
MTODO.txt | 8++++++++
Mconfig.mk | 2+-
Minclude/shared.hpp | 17++++++++---------
Msrc/main.cpp | 39+++++++++++++++++++++++++++++++++------
Asrc/shared.cpp | 10++++++++++
5 files changed, 60 insertions(+), 16 deletions(-)

diff --git a/TODO.txt b/TODO.txt @@ -192,3 +192,11 @@ Bug: - cli arguments with editable source code for defaults - add phony file + + +--- + +- cli args: + --cache-pages + --prefetch-links + --search-engine diff --git a/config.mk b/config.mk @@ -14,7 +14,7 @@ DFLAGS = -O0 -fsanitize=address,undefined -g -std=c++23 -Wpedantic -Wall -Wextra CTFLAGS = -O0 -fsanitize=undefined -g -std=c++23 -Wpedantic -Wall -Wextra -Wno-deprecated-declarations -fprofile-arcs -ftest-coverage -fPIC -D DEBUG_MODE -BASE_FILES = src/identity-manager.cpp src/quote.cpp src/list-item.cpp src/preformatted.cpp src/format-switch.cpp src/cache.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp src/heading.cpp src/render.cpp +BASE_FILES = src/identity-manager.cpp src/quote.cpp src/list-item.cpp src/preformatted.cpp src/format-switch.cpp src/cache.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp src/heading.cpp src/render.cpp src/shared.cpp BCOMMAND_P = ${CC} ${BFLAGS} -D DEBUG_MODE BCOMMAND_S = ${BASE_FILES} ${TLIBS} diff --git a/include/shared.hpp b/include/shared.hpp @@ -3,20 +3,19 @@ #include <cstdint> #include <string> -const std::string DEFAULT_SEARCH_ENGINE = "gemini://tlgs.one/search?"; -const int SITE_CACHE_LIMIT = 10; // number of links to prefetch per page -const int THREAD_NUM = 4; // thread count in thread pool for pre-fetching -const int maxWidth = 80; // max text width (left and right will be padded if COLS > maxWidth) -const int CACHE_SIZE = 500; // max number of elements in history cache and prefetch cache (500 for both) -const uint8_t COLOR_LINK = 159; // color used for links -const uint8_t COLOR_PREFORMATTED = 201; // color used for preformatted text regions +extern std::string DEFAULT_SEARCH_ENGINE; // default search option when search is inferred +extern int SITE_CACHE_LIMIT; // number of links to prefetch per page +extern int THREAD_NUM; // thread count in thread pool for pre-fetching +extern int maxWidth; // max text width +extern int CACHE_SIZE; // max elements in history cache and max in prefetch cache +extern uint8_t COLOR_LINK; // color used for links +extern uint8_t COLOR_PREFORMATTED; // color used for preformatted text regions // We guarantee responses can be at least the size limit, and at most 4096 additional bytes -const int RESPONSE_SIZE_LIMIT_MB = 5; +extern int RESPONSE_SIZE_LIMIT_MB; // since the xdg-open stuff is in a seperate process nothing will go to stdout // even in the echo case. - #ifdef DEBUG_MODE const char openUnknownScheme[] = "echo"; #else diff --git a/src/main.cpp b/src/main.cpp @@ -11,6 +11,39 @@ int main(int argc, char** argv) { + argparse::ArgumentParser program("foreseen", "0.0.1"); + + program.add_argument("-c", "--cache-pages") + .help("max number of pages for each cache (history, prefetch)") + .default_value(CACHE_SIZE) + .scan<'i', int>(); + + program.add_argument("-p", "--prefetch-links") + .help("max number of links to prefetch per page (0 disables prefetching)") + .default_value(SITE_CACHE_LIMIT) + .scan<'i', int>(); + + program.add_argument("-e", "--search-engine") + .help("default search engine") + .default_value(DEFAULT_SEARCH_ENGINE); + + program.add_argument("destination") + .help("page to open") + .nargs(argparse::nargs_pattern::optional); + + try { + program.parse_args(argc, argv); + } catch (const std::exception& err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + SITE_CACHE_LIMIT = program.get<int>("--prefetch-links"); + CACHE_SIZE = program.get<int>("--cache-pages"); + DEFAULT_SEARCH_ENGINE = program.get<std::string>("--search-engine") + "?"; + std::string destination = program.present<std::string>("destination").value_or(""); + std::signal(SIGPIPE, SIG_IGN); // need this in case of swapping network connections bc that shouldn't kill the whole process. Browser* bPtr = new Browser{}; @@ -30,12 +63,6 @@ int main(int argc, char** argv) { initColors(); - std::string destination = ""; - - if(argc > 1) { - destination = argv[1]; - } - Destination cliDestination = handleDestinationResolution(destination, true); if(cliDestination.t != NO_DESTINATION) { diff --git a/src/shared.cpp b/src/shared.cpp @@ -0,0 +1,10 @@ +#include "../include/shared.hpp" + +std::string DEFAULT_SEARCH_ENGINE = "gemini://tlgs.one/search"; +int SITE_CACHE_LIMIT = 10; +int THREAD_NUM = 4; +int maxWidth = 80; +int CACHE_SIZE = 500; +uint8_t COLOR_LINK = 159; +uint8_t COLOR_PREFORMATTED = 201; +int RESPONSE_SIZE_LIMIT_MB = 5;