foreseen

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

commit 8ff82482b62d9bb24c16da6000a9bf27cfe8b489
parent c2e8dca0dfd4df581f71678ecbe4569f8cc271fc
Author: Andrew Laack <andrew@laack.co>
Date:   Wed, 23 Sep 2026 16:44:30 -0500

Refactoring to put caching / threading in browser instead of main

Diffstat:
Minclude/browser.hpp | 13+++++++++++--
Msrc/browser.cpp | 51+++++++++++++++++++++++++++++++++++++++++++++++----
Msrc/main.cpp | 35+----------------------------------
Mtests/browser-test.cpp | 12++++++------
4 files changed, 65 insertions(+), 46 deletions(-)

diff --git a/include/browser.hpp b/include/browser.hpp @@ -1,6 +1,8 @@ #pragma once +#include <atomic> #include <cstddef> +#include <thread> #include <unordered_map> #include <vector> #include "line.hpp" @@ -9,8 +11,13 @@ #include "link.hpp" #include "utils.hpp" +const int SITE_CACHE_LIMIT = 10; +const int THREAD_NUM = 4; + class Browser { private: + std::vector<std::thread> threads; + std::vector<std::atomic<bool>> done; std::unordered_map<std::string, int> previousStatusCodes; std::vector<Link*> siteHistory; int previousIdx = -1; @@ -19,12 +26,14 @@ class Browser { Site* currentSite; std::vector<Line*> lines; std::vector<std::size_t> links; // these point to line indices - void setLinksOfCurrentLines(); + void tryCacheTargets(); Site* findInCacheAndPromoteIfRelevant(std::string& urlString); public: Browser(); + void setLinksOfCurrentLines(); ~Browser(); void goToSite(std::string url, bool addToHistory, bool refresh = false); + void setDone(int threadIdx); void refresh(); Site* getCurrentSite(); Link* getCurrentLink(); @@ -32,7 +41,7 @@ class Browser { std::vector<std::pair<std::string, TextRender>> renderSite(); std::vector<Line*> toLines(Site* site); void followLinkNumber(int linkToFollow); - std::vector<Link> getLinkLines(); + std::vector<Link>* getLinkLines(); void justCacheSite(Link link); void goBack(); diff --git a/src/browser.cpp b/src/browser.cpp @@ -1,5 +1,6 @@ #include "../include/browser.hpp" #include <string> +#include <thread> #include <unistd.h> #include "../include/site.hpp" #include "../include/gemini-client.hpp" @@ -11,6 +12,36 @@ #include <vector> +void dispatch(std::vector<Link>* targets, Browser& b, int threadIdx) { + std::vector<Link>& refT = *targets; + for(int i = 0 ; i < refT.size() && i < SITE_CACHE_LIMIT; ++i) { + auto& target = refT[i]; + b.justCacheSite(target); + } + b.setDone(threadIdx); + delete targets; +} + +void Browser::setDone(int threadIdx) { + done[threadIdx] = true; +} + +void Browser::tryCacheTargets() { + bool dispatched = false; + for(int i = 0; i < THREAD_NUM && dispatched == false; ++i) { + if(done[i]) { + if(threads[i].joinable()) { + threads[i].join(); + } + auto* lls = getLinkLines(); + done[i] = false; + threads[i] = std::thread(dispatch, lls, std::ref(*this), i); + dispatched = true; + } + } + +} + Site* Browser::findInCacheAndPromoteIfRelevant(std::string& urlString) { Site* site = nullptr; std::optional<Site> cachedSite = visitedCache->getSite(urlString); @@ -108,6 +139,8 @@ void Browser::goToSite(std::string url, bool addToHistory, bool refresh) { if (!addToHistory) { delete destination; } + + tryCacheTargets(); } void Browser::justCacheSite(Link link) { @@ -167,15 +200,25 @@ std::vector<Line*> Browser::toLines(Site* site) { } -Browser::Browser() { +Browser::Browser() : threads(THREAD_NUM), done(THREAD_NUM) { currentSite = nullptr; visitedCache = new Cache{}; preFetchCache = new Cache{}; + for (auto& d : done) { + d = true; + } } // TODO: SHould add more stuff here too, like the links stuff. Browser::~Browser() { + + for (auto& t : threads) { + if (t.joinable()) { + t.join(); + } + } + delete visitedCache; delete preFetchCache; delete currentSite; @@ -212,10 +255,10 @@ std::optional<uri> Browser::getPriorUri() { } -std::vector<Link> Browser::getLinkLines() { - std::vector<Link> res {}; +std::vector<Link>* Browser::getLinkLines() { + std::vector<Link>* res = new std::vector<Link> {}; for(auto& ln : links) { - res.push_back(*dynamic_cast<Link*>(lines[ln])); + res->push_back(*dynamic_cast<Link*>(lines[ln])); } return res; } diff --git a/src/main.cpp b/src/main.cpp @@ -14,24 +14,12 @@ #include <utility> #include <vector> -const int SITE_CACHE_LIMIT = 10; -const int THREAD_NUM = 4; -std::vector<std::thread> threads(THREAD_NUM); -std::vector<std::atomic<bool>> done(THREAD_NUM); - // we do this because this is built against ncurses, would be nice to do away w/ this // bc ppl use lots of emojis on gemini sites. -void aggressiveCaching(Browser* bPtr, std::vector<Link> targets, int threadIdx) { - for(int i = 0 ; i < targets.size() && i < SITE_CACHE_LIMIT; ++i) { - auto& target = targets[i]; - bPtr->justCacheSite(target); - } - done[threadIdx] = true; -} - void removeNonAscii(std::vector<std::pair<std::string, TextRender>>& strLs) { + for(std::size_t i = 0; i < strLs.size(); ++i) { std::string& s = strLs[i].first; @@ -262,9 +250,6 @@ std::string handleUserInput(DrawState ds) { int main(int argc, char** argv) { - for (auto& d : done) { - d = true; - } Browser* bPtr = new Browser{}; Browser& b = *bPtr; @@ -398,29 +383,11 @@ int main(int argc, char** argv) { refresh(); - bool dispatched = false; - for(int i = 0; i < THREAD_NUM && dispatched == false; ++i) { - if(done[i]) { - if(threads[i].joinable()) { - threads[i].join(); - } - auto lls = b.getLinkLines(); - done[i] = false; - threads[i] = std::thread(aggressiveCaching, bPtr, lls, i); - dispatched = true; - } - } input = getch(); } endwin(); - for (auto& t : threads) { - if (t.joinable()) { - t.join(); - } - } - delete bPtr; } diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp @@ -28,9 +28,9 @@ TEST_CASE( "Local filesystem relative navigation" ) { TEST_CASE("Test trivial line breaking") { - std::vector<std::pair<std::string, int>> strLs {}; + std::vector<std::pair<std::string, TextRender>> strLs {}; for(int i = 0; i < 10; ++i) { - std::pair<std::string, int> current {"this is a simple test line", 10}; + std::pair<std::string, TextRender> current {"this is a simple test line", TextRender{10,false}}; strLs.push_back(current); } auto result = breakLines(strLs, 10); @@ -41,7 +41,7 @@ static std::string charset = "abc defghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUV TEST_CASE("Test width invariant") { - std::vector<std::pair<std::string, int>> strLs {}; + std::vector<std::pair<std::string, TextRender>> strLs {}; srand(std::time(NULL)); @@ -51,7 +51,7 @@ TEST_CASE("Test width invariant") { for(int x = 0; x < ub; ++x) { strRnd += charset[rand() % charset.length()]; } - std::pair<std::string, int> current {strRnd, 10}; + std::pair<std::string, TextRender> current {strRnd, TextRender{10, false}}; strLs.push_back(current); } @@ -65,7 +65,7 @@ TEST_CASE("Test width invariant") { TEST_CASE("Test lots of spaces") { - std::vector<std::pair<std::string, int>> strLs {}; + std::vector<std::pair<std::string, TextRender>> strLs {}; srand(std::time(NULL)); @@ -75,7 +75,7 @@ TEST_CASE("Test lots of spaces") { for(int x = 0; x < ub; ++x) { strRnd += ' '; } - std::pair<std::string, int> current {strRnd, 10}; + std::pair<std::string, TextRender> current {strRnd, TextRender{10, false}}; strLs.push_back(current); }