foreseen

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

commit 70af9dfaf9afa5586109d2c21ba3df7f5f86118f
parent 59cbaf1b90f4dfde5db35d4c9d474497f9d72e11
Author: Andrew Laack <andrew@laack.co>
Date:   Wed, 30 Sep 2026 14:38:23 -0500

Refactor for better rendering

Diffstat:
Mbenchmarking/render-bench.cpp | 47++++++++++++++++++++++++++++++++++++++++++++++-
Minclude/render.hpp | 3+++
Msrc/render.cpp | 45++++++++++++++++++++++++++++++++++-----------
3 files changed, 83 insertions(+), 12 deletions(-)

diff --git a/benchmarking/render-bench.cpp b/benchmarking/render-bench.cpp @@ -7,7 +7,52 @@ #include <rapidcheck/Log.h> #include <string> -TEST_CASE("Benchmarking small file") { +std::string genAlNumNlSp(const int len) { + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz\n "; + std::string tmp_s; + tmp_s.reserve(len); + + for (int i = 0; i < len; ++i) { + tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + return tmp_s; +} + + +TEST_CASE("Benchmarking large file scrolling") { + setenv("EDITOR", "test" , 1); + writeStringToFile(genAlNumNlSp(100000), "tests/example.out"); + BENCHMARK("100k character file, load, scroll down 100x, scroll up 100x with 100x100 emulated terminal size") { + Browser* bPtr = new Browser {}; + Browser& b = *bPtr; + DrawState ds {}; + ds.bPtr = bPtr; + std::string st = "file:///home/andrew/gitRepos/gemini-browser/tests/example.out"; + + REQUIRE(mainLoop(ds, b, 'o',100,100)); + for(auto& ch: st) { + REQUIRE(mainLoop(ds, b, ch,100,100)); + } + REQUIRE(mainLoop(ds, b, '\n',100,100)); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "file:///home/andrew/gitRepos/gemini-browser/tests/example.out"); + + for(int i = 0; i < 100; ++i) { + REQUIRE(mainLoop(ds, b, KEY_DOWN,100,100)); + } + for(int i = 0; i < 100; ++i) { + REQUIRE(mainLoop(ds, b, KEY_UP,100,100)); + } + + delete bPtr; + }; +} + + +TEST_CASE("Benchmarking small file", "[.]") { setenv("EDITOR", "test" , 1); diff --git a/include/render.hpp b/include/render.hpp @@ -12,12 +12,15 @@ struct DrawState { std::vector<std::pair<std::string, TextRender>> prior; + std::vector<std::pair<std::string, TextRender>> broken; + bool mustReRender = true; Browser* bPtr; std::string header; int y; bool handleInput; bool handleOpenOther; bool handleRedirect; + bool reBreak = true; std::string issueText; uint64_t timeToClearIssueText; std::string redirInput; diff --git a/src/render.cpp b/src/render.cpp @@ -110,18 +110,26 @@ void draw(DrawState& ds) { auto* cs = ds.bPtr->getCurrentSite(); + // CS is null or has status 2X if (!(cs != nullptr && (cs->getStatusCode() < 20 || cs->getStatusCode() > 29))) { - ds.prior = ds.bPtr->renderSite(); + if(ds.mustReRender) { + ds.prior = ds.bPtr->renderSite(); + ds.mustReRender = false; + ds.reBreak = true; + } } - auto current = breakLines(ds.prior, std::min(ds.columns, maxWidth), ds.columns); - sanitizeCharactersToDraw(current); + if(ds.reBreak) { + ds.broken = breakLines(ds.prior, std::min(ds.columns, maxWidth), ds.columns); + ds.reBreak = false; + } + sanitizeCharactersToDraw(ds.broken); if(ds.toLowest) { - ds.y = lowestPos(current, ds); + ds.y = lowestPos(ds.broken, ds); ds.toLowest = false; } - ds.y = std::max(0,std::min(ds.y,lowestPos(current,ds))); + ds.y = std::max(0,std::min(ds.y,lowestPos(ds.broken,ds))); erase(); move(0,(ds.columns / 2) - ((int)ds.header.size() / 2) ); @@ -138,19 +146,19 @@ void draw(DrawState& ds) { addstr(std::string(ds.columns, ' ').c_str()); - for(int i = ds.y; i - ds.y < ds.lines - 2 && i < (int)current.size(); ++i) { + for(int i = ds.y; i - ds.y < ds.lines - 2 && i < (int)ds.broken.size(); ++i) { move(i - ds.y + 2, 0); - attron(COLOR_PAIR(current[i].second.color + 1)); - if(current[i].second.isBold) { + attron(COLOR_PAIR(ds.broken[i].second.color + 1)); + if(ds.broken[i].second.isBold) { attron(A_BOLD); - addstr(current[i].first.c_str()); + addstr(ds.broken[i].first.c_str()); attroff(A_BOLD); } else { - addstr(current[i].first.c_str()); + addstr(ds.broken[i].first.c_str()); } - attroff(COLOR_PAIR(current[i].second.color + 1)); + attroff(COLOR_PAIR(ds.broken[i].second.color + 1)); } if (ds.handleRedirect) { @@ -221,12 +229,14 @@ void handleRedir(DrawState& ds, int input, Browser& b) { draw(ds); ds.handleRedirect = false; tryVisitSite(ds, b, b.getCurrentSite()->getMeta()); + ds.mustReRender = true; } if(input == 'n') { ds.redirInput = "n"; draw(ds); ds.handleRedirect = false; b.goBack(); + ds.mustReRender = true; } draw(ds); } @@ -242,6 +252,7 @@ void handleUserInput(DrawState& ds, Browser& b, int sel) { tryVisitSite(ds, b, "?"+ds.userInput); } else { b.goBack(); + ds.mustReRender = true; } ds.handleInput = false; @@ -252,6 +263,7 @@ void handleUserInput(DrawState& ds, Browser& b, int sel) { ds.handleInput = false; draw(ds); b.goBack(); + ds.mustReRender = true; draw(ds); return; } @@ -272,6 +284,9 @@ void handleUserInput(DrawState& ds, Browser& b, int sel) { void tryVisitSite(DrawState& ds , Browser& b, std::string site) { bool visitSuccess = b.goToSite(site); + if(visitSuccess) { + ds.mustReRender = true; + } if(!visitSuccess) { ds.issueText = "Unable to access the requested site."; ds.timeToClearIssueText = getCurrentTime() + 1000; @@ -282,6 +297,10 @@ void tryVisitSite(DrawState& ds , Browser& b, std::string site) { bool mainLoop(DrawState& ds, Browser& b, int input, int cols, int lines) { + if(lines != ds.lines || cols != ds.columns) { + ds.reBreak = true; + } + ds.lines = lines; ds.columns = cols; @@ -298,6 +317,7 @@ bool mainLoop(DrawState& ds, Browser& b, int input, int cols, int lines) { if(res) { ds.y = 0; } + ds.mustReRender = true; break; case STRING_DESTINATION: tryVisitSite(ds, b, destination.destination); @@ -351,14 +371,17 @@ bool mainLoop(DrawState& ds, Browser& b, int input, int cols, int lines) { ds.y -= ds.lines / 2; } else if (input == 'r' || input == CTRL('r')){ b.refresh(); + ds.mustReRender = true; } else if(input == 'f') { b.goForward(); + ds.mustReRender = true; ds.y = 0; // todo: make this part of state somewhere. } else if(input == 'd') { // TODO: Handle outLocation == "" meaning failed std::string outLocation = b.tryDownloadPage(OUT_LOCATION); } else if(input == 'b') { b.goBack(); + ds.mustReRender = true; ds.y = 0; // todo: make this part of state somewhere. } else if(input == 'o') { ds.handleOpenOther = true;