foreseen

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

commit 6c58936f31db9d8f2817e3107838574e27d6dada
parent 9db57a3faa9f8185414cfb37130212f6b899c73b
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 27 Sep 2026 14:22:47 -0500

Refactoring cli parsing

Diffstat:
Minclude/browser.hpp | 2+-
Minclude/utils.hpp | 1+
Msrc/main.cpp | 37+++++++++++++++----------------------
Msrc/utils.cpp | 28++++++++++++++++++++++++++++
Mtests/pure-test.cpp | 37++++++++++++++++++++++++++++++++++---
5 files changed, 79 insertions(+), 26 deletions(-)

diff --git a/include/browser.hpp b/include/browser.hpp @@ -33,7 +33,7 @@ class Browser { Browser(); void setLinksOfCurrentLines(); ~Browser(); - void goToSite(std::string url, bool addToHistory, bool refresh = false); + void goToSite(std::string url, bool addToHistory = true, bool refresh = false); void setDone(int threadIdx); void refresh(); Site* getCurrentSite(); diff --git a/include/utils.hpp b/include/utils.hpp @@ -27,3 +27,4 @@ std::vector<std::pair<std::string, TextRender>> breakLines(std::vector<std::pair void writeStringToFile(std::string toWrite, std::string filePath); std::string encodeAsFilename(uri link); std::filesystem::path getHome(); +std::optional<std::string> handleCLIDestination(std::string destination); diff --git a/src/main.cpp b/src/main.cpp @@ -5,6 +5,7 @@ #include <cstddef> #include <filesystem> #include <ncurses.h> +#include <optional> #include <string> #include <unctrl.h> #include <utility> @@ -316,7 +317,6 @@ std::string handleUserInput(DrawState ds) { return "?" + acc; } - int main(int argc, char** argv) { Browser* bPtr = new Browser{}; @@ -334,29 +334,22 @@ int main(int argc, char** argv) { initColors(); - b.goToSite("about://newtab",true); + b.goToSite("about://newtab"); // baseline site so we can guarantee we have a starting point that's working. + + + std::string destination = ""; if(argc > 1) { - // TODO: This should have tests. - // This should also be factored into the default cli input handling stuff too, possibly. - if(std::filesystem::exists(argv[1])) { - std::string path = "file:///" + std::filesystem::current_path().string() + "/" + argv[1]; - b.goToSite(path,true); - } else { - std::string inputString = argv[1]; - if(inputString.find("gemini://") == 0) { - b.goToSite(argv[1], true); - } else if (inputString.find(':') == std::string::npos){ - b.goToSite(std::string {"gemini://"} + argv[1], true); - } else { - b.goToSite(argv[1], true); - } - } + destination = argv[1]; + } - } else { - b.goToSite("about://newtab",true); + std::optional<std::string> cliDestination = handleCLIDestination(destination); + + if(cliDestination != std::nullopt) { + b.goToSite(cliDestination.value()); } + int input = 0; // this is the main loop. @@ -409,7 +402,7 @@ int main(int argc, char** argv) { locationToGo = DEFAULT_SEARCH_ENGINE + urlEncode(locationToGo); } } - b.goToSite(locationToGo, true); + b.goToSite(locationToGo); ds.y = 0; // todo: make this part of state somewhere. } @@ -427,7 +420,7 @@ int main(int argc, char** argv) { std::string inputQuery = handleUserInput(ds); if(inputQuery != "?") { // TODO: Better handling - b.goToSite(inputQuery,true); + b.goToSite(inputQuery); ds.y = 0; // todo: make this part of state somewhere. } else { b.goBack(); @@ -444,7 +437,7 @@ int main(int argc, char** argv) { if(dir == BACKWARD) { b.goBack(); } else { - b.goToSite(b.getCurrentSite()->getMeta(),true); + b.goToSite(b.getCurrentSite()->getMeta()); ds.y = 0; // todo: make this part of state somewhere. } } else { // this should handle invalid status codes as well as 5x and 6x (for now) diff --git a/src/utils.cpp b/src/utils.cpp @@ -265,4 +265,32 @@ std::filesystem::path getHome() return home; } +std::optional<std::string> handleCLIDestination(std::string destination) { + if(destination != "") { + // TODO: This should have tests. + // This should also be factored into the default cli input handling stuff too, possibly. + if(std::filesystem::exists(destination)) { + std::string path = "file://" + std::filesystem::current_path().string() + "/" + destination; + + if(destination.find('/') == std::size_t(0)) { + path = "file://" + destination; + } + + return path; + + } else { + std::string inputString = destination; + if(inputString.find("gemini://") == 0) { + return destination; + } else if (inputString.find(':') == std::string::npos){ + return std::string {"gemini://"} + destination; + } else { + return destination; + } + } + } else { + return std::nullopt; + } + +} diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp @@ -2,12 +2,42 @@ #include <catch2/catch_test_macros.hpp> #include <cstdlib> #include <ctime> +#include <filesystem> #include <iostream> #include <optional> #include <utility> #include "../include/browser.hpp" #include "../include/utils.hpp" +TEST_CASE("Test cli input handling") { + std::unordered_map<std::string, std::string> expectations; + + // Maintain arbitrary schemas + expectations["https://github.com"] = "https://github.com"; + expectations["whatever://github.com"] = "whatever://github.com"; + expectations["about://github.com"] = "about://github.com"; + expectations["gopher://github.com"] = "gopher://github.com"; + expectations["gemini://github.com"] = "gemini://github.com"; + expectations["file:///home/whatever"] = "file:///home/whatever"; + + // resolve relative file paths if they exist + std::string cwd = std::filesystem::current_path(); + expectations["tests/sites/basic.gmi"] = "file://" + cwd + "/tests/sites/basic.gmi"; + expectations["tests/sites/basic_2.gmi"] = "file://" + cwd + "/tests/sites/basic_2.gmi"; + + // resolve absolute paths correctly + expectations[cwd + "/tests/sites/basic_2.gmi"] = "file://" + cwd + "/tests/sites/basic_2.gmi"; + + // handle specified inputs without schema that aren't files + expectations["tlgs.one"] = "gemini://tlgs.one"; + expectations["laack.co"] = "gemini://laack.co"; + expectations["blog.laack.co/pygame-vs-raylib.gmi"] = "gemini://blog.laack.co/pygame-vs-raylib.gmi"; + + for(auto& expect : expectations) { + REQUIRE(handleCLIDestination(expect.first) == expect.second); + } + +} TEST_CASE("Test trivial line breaking") { std::vector<std::pair<std::string, TextRender>> strLs {}; @@ -27,9 +57,9 @@ TEST_CASE("Test width invariant") { srand(std::time(NULL)); - for(int i = 0; i < 10000; ++i) { + for(int i = 0; i < 100; ++i) { std::string strRnd; - int ub = rand() % 10000; + int ub = rand() % 100; for(int x = 0; x < ub; ++x) { strRnd += charset[rand() % charset.length()]; } @@ -53,7 +83,7 @@ TEST_CASE("Test lots of spaces") { for(int i = 0; i < 10; ++i) { std::string strRnd; - int ub = rand() % 10000; + int ub = rand() % 100; for(int x = 0; x < ub; ++x) { strRnd += ' '; } @@ -99,3 +129,4 @@ TEST_CASE("Test normal line classification") { } +