foreseen

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

commit 08bb51102c7bce88613598a073957135dbdb9664
parent 0b8569794e724280481d27596552b95c79f19faa
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 27 Sep 2026 23:57:33 -0500

Some basic improvements for messaging about site access failures

Diffstat:
Minclude/browser.hpp | 2+-
Msrc/browser.cpp | 7++++---
Msrc/gemini-client.cpp | 9+++++----
Msrc/main.cpp | 51+++++++++++++++++++++++++++++++++++++++++----------
Mtests/pure-test.cpp | 22+++++++++++++++++++++-
5 files changed, 72 insertions(+), 19 deletions(-)

diff --git a/include/browser.hpp b/include/browser.hpp @@ -42,7 +42,7 @@ class Browser { std::optional<uri> getPriorUri(); std::vector<std::pair<std::string, TextRender>> renderSite(); std::vector<Line*> toLines(Site* site); - void followLinkNumber(int linkToFollow); + bool followLinkNumber(int linkToFollow); std::vector<Link>* getLinkLines(); Identity getIdentity(uri uriInput); void justCacheSite(Link link); diff --git a/src/browser.cpp b/src/browser.cpp @@ -318,16 +318,17 @@ std::vector<Link>* Browser::getLinkLines() { } -void Browser::followLinkNumber(int linkToFollow) { +bool Browser::followLinkNumber(int linkToFollow) { if((int)links.size() > linkToFollow-1 && linkToFollow-1 >= 0) { std::size_t pos = links[linkToFollow-1]; if(lines.size() > pos) { Line* ptr = lines[pos]; Link* ptrLnk = dynamic_cast<Link*>(ptr); - goToSite(ptrLnk->getLinkDestination().to_string(), true); + bool res = goToSite(ptrLnk->getLinkDestination().to_string(), true); + return res; } } - + return false; } void Browser::goBack() { diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp @@ -1,3 +1,5 @@ +#include <cassert> +#include <cstddef> #include <stdexcept> #include <sys/socket.h> #include <sys/time.h> @@ -79,13 +81,12 @@ Site* GeminiClient::getNetworkedSite(Link link, std::string crtPath, std::string SSL_CTX_free(ctx); - int nl = response.find("\n"); - if (nl == -1) { - return nullptr; + std::size_t nl = response.find('\n'); + if (nl == std::string::npos) { + return nullptr; } std::size_t end = nl; - if (end > 0 && response[end - 1] == '\r') { end -= 1; } diff --git a/src/main.cpp b/src/main.cpp @@ -2,7 +2,9 @@ #include "../include/utils.hpp" #include <algorithm> #include <cassert> +#include <chrono> #include <cstddef> +#include <cstdint> #include <ncurses.h> #include <locale.h> #include <string> @@ -66,6 +68,8 @@ struct DrawState { bool handleInput; bool handleOpenOther; bool handleRedirect; + std::string issueText; + int64_t timeToClearIssueText; std::string redirInput; std::string openOtherInput; std::string userInput; @@ -200,6 +204,21 @@ void draw(DrawState& ds) { drawInputBox("Destination / Link Number: ", ds.openOtherInput); } + if(ds.issueText != "") { + uint64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); + if(ds.timeToClearIssueText <= now) { + ds.issueText = ""; + } else { + move(0,0); + attron(A_BOLD); + attron(COLOR_PAIR(COLOR_RED+1)); + addstr(ds.issueText.c_str()); + attroff(COLOR_PAIR(COLOR_RED+1)); + attroff(A_BOLD); + } + + } + refresh(); } @@ -316,6 +335,17 @@ std::string handleUserInput(DrawState ds) { return "?" + acc; } +void tryVisitSite(DrawState& ds , Browser& b, std::string site) { + bool visitSuccess = b.goToSite(site); + if(!visitSuccess) { + ds.issueText = "Unable to access the requested site."; + ds.timeToClearIssueText = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() + 1000; + } else { + ds.y = 0; + } + +} + int main(int argc, char** argv) { Browser* bPtr = new Browser{}; @@ -345,7 +375,7 @@ int main(int argc, char** argv) { if(cliDestination.t != NO_DESTINATION) { assert(cliDestination.t == STRING_DESTINATION); // we don't allow numeric link following on startup. - b.goToSite(cliDestination.destination); + tryVisitSite(ds,b, cliDestination.destination); } int input = 0; @@ -381,14 +411,17 @@ int main(int argc, char** argv) { std::string locationToGo = openPageHandler(ds); Destination destination = handleDestinationResolution(locationToGo, false); + bool res; + switch(destination.t) { case NUMBER_DESTINATION: - b.followLinkNumber(destination.linkNumber); - ds.y = 0; + res = b.followLinkNumber(destination.linkNumber); + if(res) { + ds.y = 0; + } break; case STRING_DESTINATION: - b.goToSite(destination.destination); - ds.y = 0; + tryVisitSite(ds, b, destination.destination); break; case NO_DESTINATION: break; @@ -405,8 +438,7 @@ int main(int argc, char** argv) { std::string inputQuery = handleUserInput(ds); if(inputQuery != "?") { // TODO: Better handling - b.goToSite(inputQuery); - ds.y = 0; // todo: make this part of state somewhere. + tryVisitSite(ds, b,inputQuery); } else { b.goBack(); } @@ -422,10 +454,9 @@ int main(int argc, char** argv) { if(dir == BACKWARD) { b.goBack(); } else { - b.goToSite(b.getCurrentSite()->getMeta()); - ds.y = 0; // todo: make this part of state somewhere. + tryVisitSite(ds, b, b.getCurrentSite()->getMeta()); } - } else { // this should handle invalid status codes as well as 5x and 6x (for now) + } else { // this should handle invalid status codes b.goBack(); } } diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp @@ -229,7 +229,6 @@ TEST_CASE("Proper whitespace compliance") { } - TEST_CASE("Crash test") { auto res1 = lineToLine("", std::nullopt, -1, false); REQUIRE(res1->type() == PLAINTEXT); @@ -258,3 +257,24 @@ TEST_CASE("Crash test") { auto res9 = lineToLine("*", std::nullopt, 1, false); REQUIRE(res9->type() == PLAINTEXT); } + +static std::string linecharset = "#>=abc## de_-><0193248$#)(&@)(*&$#^*&#^%^&*&fghijklmnopqrstuvwxyz #````ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + +TEST_CASE("Crash test rng") { + for(int x = 0; x < 10000; ++x) { + std::string strRnd = ""; + for(int i = 0; i < 1000; ++i) { + strRnd += linecharset[rand() % linecharset.length()]; + auto* ln = lineToLine(strRnd, std::nullopt, 1, false); + } + } + for(int x = 0; x < 10000; ++x) { + std::string strRnd = ""; + for(int i = 0; i < 1000; ++i) { + strRnd += linecharset[rand() % linecharset.length()]; + auto* ln = lineToLine(strRnd, std::nullopt, 1, true); + } + } + +} +