commit aa6f75cd2ac96a6d4ff5d84a1a014e8787724236
parent 90732d903f296f2473396aad984d4ef480cd3a8a
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 29 Sep 2026 17:12:40 -0500
pure function state machine refactoring for 'o' menu
Diffstat:
3 files changed, 105 insertions(+), 53 deletions(-)
diff --git a/Makefile b/Makefile
@@ -13,6 +13,7 @@ clean:
rm -rf test*.out foreseen.out
rm -rf *.gcda *.gcno
rm -rf coverage.info
+ rm -rf coverage-html
browser-test:
${TCOMMAND_P} tests/browser-test.cpp ${TCOMMAND_S} -o test1.out
diff --git a/src/render.cpp b/src/render.cpp
@@ -179,49 +179,31 @@ void draw(DrawState& ds) {
}
-std::string openPageHandler(DrawState ds) {
+void openPageHandler(DrawState& ds, int sel) {
- ds.handleOpenOther = true;
- draw(ds);
- std::string acc = "";
-
- while(true) {
- int sel = getch();
-
- if(sel == KEY_BACKSPACE) {
- if(acc.size() > 0) {
- acc = acc.substr(0,acc.size() - 1);
- }
- ds.openOtherInput = acc;
- draw(ds);
- continue;
- }
-
- if(sel == 27) {
- acc = "";
- break;
- }
-
- if(sel == '\n' || sel == KEY_ENTER) {
- break;
- }
-
- if(isValidUserInput(sel)) {
- acc += std::string {(char)sel};
+ if(sel == KEY_BACKSPACE) {
+ if(ds.openOtherInput.size() > 0) {
+ ds.openOtherInput = ds.openOtherInput.substr(0,ds.openOtherInput.size() - 1);
}
-
- ds.openOtherInput = acc;
draw(ds);
-
}
- ds.openOtherInput = acc;
- ds.handleOpenOther = false;
- draw(ds);
+ if(sel == 27) {
+ ds.openOtherInput = "";
+ ds.handleOpenOther = false;
+ return;
+ }
+ if(sel == '\n' || sel == KEY_ENTER) {
+ ds.handleOpenOther = false;
+ return;
+ }
+ if(isValidUserInput(sel)) {
+ ds.openOtherInput += std::string {(char)sel};
+ }
- return acc;
+ draw(ds);
}
enum Direction {
@@ -236,6 +218,7 @@ Direction handleRedir(DrawState ds) {
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";
@@ -261,6 +244,7 @@ std::string handleUserInput(DrawState ds) {
while(true) {
+ // TODO: Remove this getch invocation and make this entire file non-blocking
int sel = getch();
if(sel == '\n' || sel == KEY_ENTER) {
break;
@@ -302,6 +286,37 @@ void tryVisitSite(DrawState& ds , Browser& b, std::string site) {
}
bool mainLoop(DrawState& ds, Browser& b, int input) {
+
+ if(ds.handleOpenOther) {
+ openPageHandler(ds,input);
+ if(!ds.handleOpenOther) {
+ draw(ds);
+ if(ds.openOtherInput != "") {
+ Destination destination = handleDestinationResolution(ds.openOtherInput , false);
+ bool res;
+ switch(destination.t) {
+ case NUMBER_DESTINATION:
+ res = b.followLinkNumber(destination.linkNumber);
+ if(res) {
+ ds.y = 0;
+ }
+ break;
+ case STRING_DESTINATION:
+ tryVisitSite(ds, b, destination.destination);
+ break;
+ case NO_DESTINATION:
+ break;
+ }
+ ds.openOtherInput = "";
+ }
+ } else {
+ draw(ds);
+ return true;
+ }
+ return true;
+ }
+
+
if(input == 'q') {
return false;
}
@@ -331,24 +346,8 @@ bool mainLoop(DrawState& ds, Browser& b, int input) {
ds.y = 0; // todo: make this part of state somewhere.
} else if(input == 'o') {
- std::string locationToGo = openPageHandler(ds);
- Destination destination = handleDestinationResolution(locationToGo, false);
-
- bool res;
-
- switch(destination.t) {
- case NUMBER_DESTINATION:
- res = b.followLinkNumber(destination.linkNumber);
- if(res) {
- ds.y = 0;
- }
- break;
- case STRING_DESTINATION:
- tryVisitSite(ds, b, destination.destination);
- break;
- case NO_DESTINATION:
- break;
- }
+ ds.handleOpenOther = true;
+ mainLoop(ds, b, KEY_RESIZE);
}
// this is the loop where we deal with redirects and stuff like that.
diff --git a/tests/puppet-test.cpp b/tests/puppet-test.cpp
@@ -2,6 +2,21 @@
#include "../include/render.hpp"
#include <ncurses.h>
+std::string genRandom(const int len) {
+ static const char alpha[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz";
+ std::string tmp_s;
+ tmp_s.reserve(len);
+
+ for (int i = 0; i < len; ++i) {
+ tmp_s += alpha[rand() % (sizeof(alpha) - 1)];
+ }
+
+ return tmp_s;
+}
+
+
// Puppet entire browser
TEST_CASE("q quits session immediately") {
@@ -15,3 +30,40 @@ TEST_CASE("RESIZE falls through and returns") {
DrawState ds {};
REQUIRE(mainLoop(ds, b, KEY_RESIZE));
}
+
+TEST_CASE("Open page handler returns") {
+ Browser b {};
+ DrawState ds {};
+ REQUIRE(mainLoop(ds, b, 'o'));
+}
+
+TEST_CASE("Open page and navigate to site") {
+ Browser b {};
+ DrawState ds {};
+ REQUIRE(mainLoop(ds, b, 'o'));
+ REQUIRE(mainLoop(ds, b, 'l'));
+ REQUIRE(mainLoop(ds, b, 'a'));
+ REQUIRE(mainLoop(ds, b, 'a'));
+ REQUIRE(mainLoop(ds, b, 'c'));
+ REQUIRE(mainLoop(ds, b, 'k'));
+ REQUIRE(mainLoop(ds, b, '.'));
+ REQUIRE(mainLoop(ds, b, 'c'));
+ REQUIRE(mainLoop(ds, b, 'o'));
+ REQUIRE(mainLoop(ds, b, '\n'));
+ REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://laack.co");
+}
+
+TEST_CASE("Open page and search the web") {
+ Browser b {};
+ DrawState ds {};
+ REQUIRE(mainLoop(ds, b, 'o'));
+
+ 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));
+}
+