commit 7a03ec5c4eeba0f470e83dfb21a11820e1200db3
parent dec5d019e4bc468061a9e42de74e1de7c06679b7
Author: Andrew Laack <andrew@laack.co>
Date: Sat, 3 Oct 2026 22:30:53 -0500
Refactored code to return enum from gotosite.
Diffstat:
6 files changed, 38 insertions(+), 21 deletions(-)
diff --git a/include/browser.hpp b/include/browser.hpp
@@ -11,7 +11,13 @@
#include "cache.hpp"
#include "link.hpp"
#include "utils.hpp"
-#include "shared.hpp"
+
+enum GoToSiteResult {
+ SITE_LOADED,
+ SITE_LOAD_FAILED,
+ FETCHING_SITE_ASYNC
+};
+
class Browser {
private:
@@ -33,7 +39,9 @@ class Browser {
Browser();
void setLinksOfCurrentLines();
~Browser();
- bool goToSite(std::string url, bool addToHistory = true, bool refresh = false);
+
+ // if background is set and the site is found in cache it will go there right away, returning successfully.
+ GoToSiteResult goToSite(std::string url, bool addToHistory = true, bool refresh = false, bool background = false);
void setDone(int threadIdx);
void refresh();
Site* getCurrentSite();
diff --git a/include/shared.hpp b/include/shared.hpp
@@ -6,7 +6,7 @@
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 MAX_WIDTH; // 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
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -6,6 +6,7 @@
#include <thread>
#include <unistd.h>
#include "../include/site.hpp"
+#include "../include/shared.hpp"
#include "../include/errors.hpp"
#include "../include/gemini-client.hpp"
#include "../include/utils.hpp"
@@ -94,7 +95,7 @@ void Browser::refresh() {
goToSite(getPriorUri().value().to_string(), false, true);
}
-bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
+GoToSiteResult Browser::goToSite(std::string url, bool addToHistory, bool refresh, bool background) {
Link* prior = nullptr;
@@ -124,7 +125,7 @@ bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
openThread = std::thread(openUrl, urlString);
delete destination;
- return true;
+ return SITE_LOADED;
}
@@ -151,14 +152,14 @@ bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
delete site;
}
delete destination;
- return false;
+ return SITE_LOAD_FAILED;
}
int sc = site->getStatusCode();
if (sc < 10 || sc >= 40) {
delete site;
delete destination;
- return false;
+ return SITE_LOAD_FAILED; // TODO: better handling, addl enum opt
}
if(addToHistory) {
@@ -195,7 +196,7 @@ bool Browser::goToSite(std::string url, bool addToHistory, bool refresh) {
}
tryCacheTargets();
- return true;
+ return SITE_LOADED;
}
Identity Browser::getIdentity(uri uriInput) {
@@ -272,8 +273,9 @@ Browser::Browser() : threads(THREAD_NUM), done(THREAD_NUM){
}
// this ensures some nice invariants about the browser, like always having at least one valid page.
- bool start = goToSite("about://newtab");
- if(!start) {
+ GoToSiteResult start = goToSite("about://newtab");
+
+ if(start != SITE_LOADED) {
throw std::runtime_error("Browser unexpectedly failed to start.");
}
}
@@ -351,8 +353,13 @@ bool Browser::followLinkNumber(int linkToFollow) {
if(lines.size() > pos) {
Line* ptr = lines[pos];
Link* ptrLnk = dynamic_cast<Link*>(ptr);
- bool res = goToSite(ptrLnk->getLinkDestination().to_string(), true);
- return res;
+ GoToSiteResult res = goToSite(ptrLnk->getLinkDestination().to_string(), true);
+
+ if(res == SITE_LOADED) {
+ return true;
+ }
+
+ return false;
}
}
return false;
@@ -388,7 +395,7 @@ void Browser::goBack() {
return; // safely fail with rollback
}
}
- if(!goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false)) {
+ if(goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false) != SITE_LOADED) {
previousIdx = original;
}
@@ -421,7 +428,7 @@ void Browser::goForward() {
}
}
- if(!goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false)) {
+ if(goToSite(this->siteHistory[previousIdx]->getLinkDestination().to_string(), false) != SITE_LOADED) {
previousIdx = original;
}
} else {
diff --git a/src/main.cpp b/src/main.cpp
@@ -1,6 +1,7 @@
#include "../include/browser.hpp"
#include "../include/utils.hpp"
#include "../include/render.hpp"
+#include "../include/shared.hpp"
#include <cassert>
#include <csignal>
#include <ncurses.h>
diff --git a/src/render.cpp b/src/render.cpp
@@ -1,5 +1,6 @@
#include "../include/browser.hpp"
#include "../include/utils.hpp"
+#include "../include/shared.hpp"
#include "../include/render.hpp"
#include <algorithm>
#include <cassert>
@@ -119,7 +120,7 @@ void draw(DrawState& ds) {
}
if(ds.reBreak) {
- ds.broken = breakLines(ds.prior, std::min(ds.columns, maxWidth), ds.columns);
+ ds.broken = breakLines(ds.prior, std::min(ds.columns, MAX_WIDTH), ds.columns);
ds.reBreak = false;
}
if(ds.toLowest) {
@@ -271,15 +272,15 @@ void handleUserInput(DrawState& ds, Browser& b, int sel) {
}
void tryVisitSite(DrawState& ds , Browser& b, std::string site) {
- bool visitSuccess = b.goToSite(site);
- if(visitSuccess) {
+ GoToSiteResult visitSuccess = b.goToSite(site);
+
+ if(visitSuccess == SITE_LOADED) {
ds.mustReRender = true;
+ ds.y = 0;
}
- if(!visitSuccess) {
+ else {
ds.issueText = "Unable to access the requested site.";
ds.timeToClearIssueText = getCurrentTime() + 1000;
- } else {
- ds.y = 0;
}
}
diff --git a/src/shared.cpp b/src/shared.cpp
@@ -3,7 +3,7 @@
std::string DEFAULT_SEARCH_ENGINE = "gemini://tlgs.one/search";
int SITE_CACHE_LIMIT = 10;
int THREAD_NUM = 4;
-int maxWidth = 80;
+int MAX_WIDTH = 80;
int CACHE_SIZE = 500;
uint8_t COLOR_LINK = 159;
uint8_t COLOR_PREFORMATTED = 201;