foreseen

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

commit b4809580027b1ed231490822149cc453d733fd40
parent 5f5ab74e0b9fb8d175893b99e9a819b9393cebc9
Author: Andrew Laack <andrew@laack.co>
Date:   Wed, 30 Sep 2026 19:17:43 -0500

Use uint8_t to represent colors

Diffstat:
Mbenchmarking/render-bench.cpp | 32+++++++++++++++++++++++++++++++-
Mconfig.mk | 2+-
Minclude/format-switch.hpp | 3++-
Minclude/heading.hpp | 3++-
Minclude/line.hpp | 3++-
Minclude/link.hpp | 3++-
Minclude/list-item.hpp | 3++-
Minclude/plaintext.hpp | 3++-
Minclude/preformatted.hpp | 3++-
Minclude/quote.hpp | 3++-
Minclude/shared.hpp | 5+++--
Minclude/site.hpp | 2--
Minclude/utils.hpp | 7++++---
Msrc/browser.cpp | 3+++
Msrc/format-switch.cpp | 3++-
Msrc/heading.cpp | 3++-
Msrc/link.cpp | 3++-
Msrc/list-item.cpp | 3++-
Msrc/plaintext.cpp | 3++-
Msrc/preformatted.cpp | 3++-
Msrc/quote.cpp | 3++-
Msrc/utils.cpp | 27+++++++++++++++------------
22 files changed, 87 insertions(+), 36 deletions(-)

diff --git a/benchmarking/render-bench.cpp b/benchmarking/render-bench.cpp @@ -22,7 +22,37 @@ std::string genAlNumNlSp(const int len) { return tmp_s; } -TEST_CASE("Benchmarking large file scrolling") { +TEST_CASE("Benchmarking huge file scrolling") { + setenv("EDITOR", "test" , 1); + writeStringToFile(genAlNumNlSp(10000000), "tests/example.out"); + BENCHMARK("5 passes per iteration, 10m character file, load, scroll down 1000x, scroll up 1000x with 100x100 emulated terminal size") { + for(int i = 0; i < 5; ++i) { + 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 < 1000; ++i) { + REQUIRE(mainLoop(ds, b, KEY_DOWN,100,100)); + } + for(int i = 0; i < 1000; ++i) { + REQUIRE(mainLoop(ds, b, KEY_UP,100,100)); + } + delete bPtr; + } + }; +} + + +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") { diff --git a/config.mk b/config.mk @@ -9,7 +9,7 @@ TLIBS = -lCatch2Main -lCatch2 -lrapidcheck ${LIBS} CC = g++ IFLAGS = -Ofast -std=c++23 -BFLAGS = -O0 -std=c++23 -g +BFLAGS = -Ofast -std=c++23 -g DFLAGS = -O0 -fsanitize=address,undefined -g -std=c++23 -Wpedantic -Wall -Wextra -Wno-deprecated-declarations -D DEBUG_MODE CTFLAGS = -O0 -fsanitize=undefined -g -std=c++23 -Wpedantic -Wall -Wextra -Wno-deprecated-declarations -fprofile-arcs -ftest-coverage -fPIC -D DEBUG_MODE diff --git a/include/format-switch.hpp b/include/format-switch.hpp @@ -1,5 +1,6 @@ #pragma once #include "line.hpp" +#include <cstdint> class FormatSwitch : public Line { private: @@ -7,7 +8,7 @@ class FormatSwitch : public Line { public: FormatSwitch(std::string text); std::string textToDraw() override; - int getColor() override; + uint8_t getColor() override; LineType type() override; bool isBold() override; }; diff --git a/include/heading.hpp b/include/heading.hpp @@ -1,5 +1,6 @@ #pragma once +#include <cstdint> #include <optional> #include <string> #include "../vendor/uri.hpp" @@ -12,7 +13,7 @@ class Heading : public Line { public: Heading(std::string text); std::string textToDraw() override; - int getColor() override; + uint8_t getColor() override; LineType type() override; bool isBold() override; }; diff --git a/include/line.hpp b/include/line.hpp @@ -1,5 +1,6 @@ #pragma once +#include <cstdint> #include <string> @@ -18,7 +19,7 @@ enum LineType { class Line { public: virtual std::string textToDraw() = 0; - virtual int getColor() = 0; + virtual uint8_t getColor() = 0; virtual LineType type() = 0; virtual ~Line() = default; virtual bool isBold() = 0; diff --git a/include/link.hpp b/include/link.hpp @@ -1,5 +1,6 @@ #pragma once +#include <cstdint> #include <optional> #include <string> #include "../vendor/uri.hpp" @@ -18,7 +19,7 @@ class Link : public Line { uri getLinkDestination(); std::optional<std::string> getLinkText(); std::string textToDraw() override; - int getColor() override; + uint8_t getColor() override; bool isBold() override; LineType type() override; }; diff --git a/include/list-item.hpp b/include/list-item.hpp @@ -1,5 +1,6 @@ #pragma once #include "line.hpp" +#include <cstdint> class ListItem : public Line { private: @@ -7,7 +8,7 @@ class ListItem : public Line { public: ListItem(std::string text); std::string textToDraw() override; - int getColor() override; + uint8_t getColor() override; LineType type() override; bool isBold() override; }; diff --git a/include/plaintext.hpp b/include/plaintext.hpp @@ -1,5 +1,6 @@ #pragma once #include "line.hpp" +#include <cstdint> class Plaintext : public Line { private: @@ -7,7 +8,7 @@ class Plaintext : public Line { public: Plaintext(std::string text); std::string textToDraw() override; - int getColor() override; + uint8_t getColor() override; LineType type() override; bool isBold() override; }; diff --git a/include/preformatted.hpp b/include/preformatted.hpp @@ -1,5 +1,6 @@ #pragma once #include "line.hpp" +#include <cstdint> class Preformatted : public Line { private: @@ -7,7 +8,7 @@ class Preformatted : public Line { public: Preformatted(std::string text); std::string textToDraw() override; - int getColor() override; + uint8_t getColor() override; LineType type() override; bool isBold() override; }; diff --git a/include/quote.hpp b/include/quote.hpp @@ -1,5 +1,6 @@ #pragma once #include "line.hpp" +#include <cstdint> class Quote : public Line { private: @@ -7,7 +8,7 @@ class Quote : public Line { public: Quote(std::string text); std::string textToDraw() override; - int getColor() override; + uint8_t getColor() override; LineType type() override; bool isBold() override; }; diff --git a/include/shared.hpp b/include/shared.hpp @@ -1,5 +1,6 @@ #pragma once +#include <cstdint> #include <string> const std::string DEFAULT_SEARCH_ENGINE = "gemini://tlgs.one/search?"; @@ -7,8 +8,8 @@ const int SITE_CACHE_LIMIT = 10; // number of links to prefetch per page const int THREAD_NUM = 4; // thread count in thread pool for pre-fetching const int maxWidth = 80; // max text width (left and right will be padded if COLS > maxWidth) const int CACHE_SIZE = 500; // max number of elements in history cache and prefetch cache (500 for both) -const int COLOR_LINK = 159; // color used for links -const int COLOR_PREFORMATTED = 201; // color used for preformatted text regions +const uint8_t COLOR_LINK = 159; // color used for links +const uint8_t COLOR_PREFORMATTED = 201; // color used for preformatted text regions // We guarantee responses can be at least the size limit, and at most 4096 additional bytes const int RESPONSE_SIZE_LIMIT_MB = 5; diff --git a/include/site.hpp b/include/site.hpp @@ -2,8 +2,6 @@ #include <cstdint> #include <string> -#include <vector> -#include "link.hpp" class Site{ private: diff --git a/include/utils.hpp b/include/utils.hpp @@ -1,4 +1,5 @@ #pragma once +#include <cstdint> #include <optional> #include <filesystem> #include <string> @@ -7,9 +8,9 @@ #include "../vendor/uri.hpp" struct TextRender { - int color; - bool isBold; - bool shouldFold = true; // preformatted text doesn't need to be folded. + uint8_t color; + bool isBold:1; + bool shouldFold:1 = true; // preformatted text doesn't need to be folded. }; bool isPrefixed(std::string input, std::string prefix); diff --git a/src/browser.cpp b/src/browser.cpp @@ -309,7 +309,10 @@ Site* Browser::getCurrentSite() { } std::vector<std::pair<std::string, TextRender>> Browser::renderSite() { + std::vector<std::pair<std::string, TextRender>> res{}; + res.reserve(lines.size()); + for(auto* line: lines) { // TODO: Don't special case this; define an interface. if(line->type() == PREFORMATTED) { diff --git a/src/format-switch.cpp b/src/format-switch.cpp @@ -1,4 +1,5 @@ #include "../include/format-switch.hpp" +#include <cstdint> #include <ncurses.h> FormatSwitch::FormatSwitch(std::string input) : text(input) {} @@ -7,7 +8,7 @@ std::string FormatSwitch::textToDraw() { return ""; } -int FormatSwitch::getColor() { +uint8_t FormatSwitch::getColor() { return COLOR_WHITE; } diff --git a/src/heading.cpp b/src/heading.cpp @@ -1,6 +1,7 @@ #include "../include/heading.hpp" #include "../include/errors.hpp" #include "../include/utils.hpp" +#include <cstdint> #include <ncurses.h> Heading::Heading(std::string text) : actualText(text) { @@ -34,7 +35,7 @@ std::string Heading::textToDraw() { } throw NotImplemented(); } -int Heading::getColor() { +uint8_t Heading::getColor() { switch (headingLevel) { case 3: return COLOR_CYAN; diff --git a/src/link.cpp b/src/link.cpp @@ -1,4 +1,5 @@ #include "../include/link.hpp" +#include <cstdint> #include <optional> #include <string> #include "../include/utils.hpp" @@ -105,7 +106,7 @@ std::string Link::textToDraw() { return renderedText + "\n"; } -int Link::getColor() { +uint8_t Link::getColor() { return COLOR_LINK; } diff --git a/src/list-item.cpp b/src/list-item.cpp @@ -1,5 +1,6 @@ #include "../include/list-item.hpp" #include "../include/utils.hpp" +#include <cstdint> ListItem::ListItem(std::string input) { textToRender = input.substr(1); @@ -11,7 +12,7 @@ std::string ListItem::textToDraw() { return textToRender + "\n"; } -int ListItem::getColor() { +uint8_t ListItem::getColor() { return 15; } diff --git a/src/plaintext.cpp b/src/plaintext.cpp @@ -1,4 +1,5 @@ #include "../include/plaintext.hpp" +#include <cstdint> Plaintext::Plaintext(std::string input) : text(input) {} @@ -6,7 +7,7 @@ std::string Plaintext::textToDraw() { return text + "\n"; } -int Plaintext::getColor() { +uint8_t Plaintext::getColor() { return 15; } diff --git a/src/preformatted.cpp b/src/preformatted.cpp @@ -1,5 +1,6 @@ #include "../include/preformatted.hpp" #include "../include/shared.hpp" +#include <cstdint> #include <ncurses.h> Preformatted::Preformatted(std::string input) : text(input) {} @@ -8,7 +9,7 @@ std::string Preformatted::textToDraw() { return text + "\n"; } -int Preformatted::getColor() { +uint8_t Preformatted::getColor() { return COLOR_PREFORMATTED; } diff --git a/src/quote.cpp b/src/quote.cpp @@ -1,5 +1,6 @@ #include "../include/quote.hpp" #include "../include/utils.hpp" +#include <cstdint> #include <ncurses.h> Quote::Quote(std::string input) { @@ -12,7 +13,7 @@ std::string Quote::textToDraw() { return textToRender + "\n"; } -int Quote::getColor() { +uint8_t Quote::getColor() { return COLOR_MAGENTA; } diff --git a/src/utils.cpp b/src/utils.cpp @@ -8,6 +8,8 @@ #include "../include/errors.hpp" #include "../include/plaintext.hpp" #include "../include/preformatted.hpp" +#include <thread> +#include <threads.h> #include <algorithm> #include <cassert> #include <cstddef> @@ -211,21 +213,18 @@ std::vector<std::pair<std::string, TextRender>> breakLines(std::vector<std::pair std::vector<std::pair<std::string, TextRender>> res {}; - // DO NOT CHANGE THIS CODE. - // If this code is changed the entire JS ecosystem will crash. - // This is "load-bearing" code. Only the most sophisticated can implement this from - // scratch, hence why it's left to the professionals. - int leftPadAmount = std::max(0, (cols - width) / 2); std::string leftPadStr (leftPadAmount, ' '); if(width <= 0) { return res; } - + + res.reserve(strLs.size()); + for(std::size_t i = 0; i < strLs.size(); ++i) { - std::string cstr = strLs[i].first; + const std::string& cstr = strLs[i].first; if(!strLs[i].second.shouldFold) { int limit = cols - leftPadAmount; @@ -240,8 +239,8 @@ std::vector<std::pair<std::string, TextRender>> breakLines(std::vector<std::pair w += cw; end += len; } - cstr = cstr.substr(0, end); // otherwise there's some funkiness at the end due to how ncurses renders stuff. - res.push_back(std::pair<std::string,TextRender> {leftPadStr + cstr, strLs[i].second}); + std::string rs = cstr.substr(0, end); // otherwise there's some funkiness at the end due to how ncurses renders stuff. + res.push_back(std::pair<std::string,TextRender> {leftPadStr + rs, strLs[i].second}); continue; } @@ -280,7 +279,8 @@ std::vector<std::pair<std::string, TextRender>> breakLines(std::vector<std::pair lastSpace = -1; } - current.append(cstr, x, len); + std::string cp = cstr; + current.append(cp, x, len); curWidth += w; if(cstr[x] == ' ') { lastSpace = current.size() - 1; @@ -397,12 +397,15 @@ Destination handleDestinationResolution(std::string destination, bool isCli) { } void sanitizeCharactersToDraw(std::vector<std::pair<std::string, TextRender>>& strLs) { + std::vector<std::thread> threads{}; + for(std::size_t i = 0; i < strLs.size(); ++i) { std::string& s = strLs[i].first; - std::string out; - for (int c: s) + out.reserve(s.size()); + + for (int c : s) if (c == '\r' || c == '\v' || c == '\b' || c == '\f' || c == '\a' || c == '\0') { continue; } else if (c == '\t') {