foreseen

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

commit 6254ff6de2feeee86d3b83d87d121694e6fddb4d
parent b6a6e96cb21d4573621e75e6085aeb2a94aec43e
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 29 Sep 2026 19:32:49 -0500

Refactored link following

Diffstat:
MTODO.txt | 4++++
Msrc/render.cpp | 70++++++++++++++++++++++++++++++----------------------------------------
Mtests/puppet-test.cpp | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 93 insertions(+), 40 deletions(-)

diff --git a/TODO.txt b/TODO.txt @@ -126,3 +126,7 @@ - setup headless execution - getch passed to headless execution function as char +- open in text editor + - go to site -> vim buffer + - stdin? +- make sure keybindings are up to date on new tab page diff --git a/src/render.cpp b/src/render.cpp @@ -6,6 +6,7 @@ #include <chrono> #include <csignal> #include <cstdint> +#include <iostream> #include <ncurses.h> #include <locale.h> #include <string> @@ -204,42 +205,26 @@ void openPageHandler(DrawState& ds, int sel) { draw(ds); } -enum Direction { - FORWARD, - BACKWARD -}; - -Direction handleRedir(DrawState ds) { - - ds.handleRedirect = true; - ds.redirInput = ""; - draw(ds); - - while(true) { - // TODO: Remove this getch invocation and make this entire file non-blocking - int input = getch(); - if(input == 'y') { - ds.redirInput = "y"; - draw(ds); - ds.handleRedirect = false; - return FORWARD; - } - if(input == 'n') { - ds.redirInput = "n"; - draw(ds); - ds.handleRedirect = false; - return BACKWARD; - } +void handleRedir(DrawState& ds, int input, Browser& b) { + if(input == 'y') { + ds.redirInput = "y"; draw(ds); + ds.handleRedirect = false; + tryVisitSite(ds, b, b.getCurrentSite()->getMeta()); + } + if(input == 'n') { + ds.redirInput = "n"; + draw(ds); + ds.handleRedirect = false; + b.goBack(); } + draw(ds); } -std::string handleUserInput(DrawState ds) { - +std::string handleUserInput(DrawState& ds) { ds.handleInput = true; draw(ds); std::string acc = ""; - while(true) { // TODO: Remove this getch invocation and make this entire file non-blocking @@ -318,6 +303,12 @@ bool mainLoop(DrawState& ds, Browser& b, int input) { ds.header = clk->getLinkDestination().to_string(); draw(ds); return true; + } else if (ds.handleRedirect) { + handleRedir(ds, input, b); + if(!ds.handleRedirect) { + mainLoop(ds, b, KEY_RESIZE); + } + return true; } @@ -355,7 +346,11 @@ bool mainLoop(DrawState& ds, Browser& b, int input) { } // this is the loop where we deal with redirects and stuff like that. - while(b.getCurrentSite()->getStatusCode() < 20 || b.getCurrentSite()->getStatusCode() > 29) { + // TODO: I don't think this has to still be a loop? + // Broadly, we are moving away from a loop based approach, sequestering them to either browser with forward / backward + // or main.cpp / tests. + + while( (b.getCurrentSite()->getStatusCode() < 20 || b.getCurrentSite()->getStatusCode() > 29) && !ds.handleRedirect) { if(b.getCurrentSite()->getStatusCode() >= 10 && b.getCurrentSite()->getStatusCode() <= 19) { auto* st = b.getCurrentSite(); if(st != nullptr) { @@ -369,21 +364,16 @@ bool mainLoop(DrawState& ds, Browser& b, int input) { b.goBack(); } - } else if (b.getCurrentSite()->getStatusCode() >= 30 && b.getCurrentSite()->getStatusCode() <= 39){ - + } else if (b.getCurrentSite()->getStatusCode() >= 30 && b.getCurrentSite()->getStatusCode() <= 39 && !ds.handleRedirect){ auto* st = b.getCurrentSite(); if(st != nullptr) { ds.metaLine = st->getMeta(); } - Direction dir = handleRedir(ds); - if(dir == BACKWARD) { - b.goBack(); - } else { - tryVisitSite(ds, b, b.getCurrentSite()->getMeta()); - } - } else { // this should handle invalid status codes - b.goBack(); + ds.redirInput = ""; + ds.handleRedirect = true; + mainLoop(ds, b, KEY_RESIZE); + } } diff --git a/tests/puppet-test.cpp b/tests/puppet-test.cpp @@ -103,3 +103,62 @@ TEST_CASE("Cancel 'o' menu by pressing escape") { REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "about://newtab"); } +TEST_CASE("Handle redirects") { + Browser b {}; + DrawState ds {}; + REQUIRE(mainLoop(ds, b, 'o')); + REQUIRE(mainLoop(ds, b, 'c')); + REQUIRE(mainLoop(ds, b, 'o')); + REQUIRE(mainLoop(ds, b, 'n')); + REQUIRE(mainLoop(ds, b, 'm')); + REQUIRE(mainLoop(ds, b, 'a')); + REQUIRE(mainLoop(ds, b, 'n')); + REQUIRE(mainLoop(ds, b, '.')); + REQUIRE(mainLoop(ds, b, 'o')); + REQUIRE(mainLoop(ds, b, 'r')); + REQUIRE(mainLoop(ds, b, 'g')); + REQUIRE(mainLoop(ds, b, '\n')); + + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://conman.org"); + + REQUIRE(mainLoop(ds, b, 'y')); // follow redirect + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://gemini.conman.org/"); + +} + +//TEST_CASE("Enter an input") { +// Browser b {}; +// DrawState ds {}; +// +// REQUIRE(mainLoop(ds, b, 'o')); +// REQUIRE(mainLoop(ds, b, 't')); +// REQUIRE(mainLoop(ds, b, 'l')); +// REQUIRE(mainLoop(ds, b, 'g')); +// REQUIRE(mainLoop(ds, b, 's')); +// REQUIRE(mainLoop(ds, b, '.')); +// REQUIRE(mainLoop(ds, b, 'o')); +// REQUIRE(mainLoop(ds, b, 'n')); +// REQUIRE(mainLoop(ds, b, 'e')); +// REQUIRE(mainLoop(ds, b, '\n')); +// +// REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://tlgs.one"); +// +// REQUIRE(mainLoop(ds,b,'o')); +// REQUIRE(mainLoop(ds,b,'2')); +// REQUIRE(mainLoop(ds, b, '\n')); +// +// REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://tlgs.one/search"); +// +// std::string rnd = genRandom(10); +// +// INFO(rnd); +// +// for(auto& ch : rnd) { +// REQUIRE(mainLoop(ds, b, ch)); +// } +// +// REQUIRE(mainLoop(ds, b, '\n')); +// +// REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://tlgs.one/search?" + urlEncode(rnd)); +//} +