foreseen

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

commit d81e7ad12a94b4f03355515a75e091d902251a1c
parent 9fa286ca3ece17a898b8ed7d3a72896851e58bf2
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 27 Sep 2026 19:33:08 -0500

Revisions to formatting stuff

Diffstat:
MMakefile | 20++++++++++----------
MTODO.txt | 6+++++-
Minclude/heading.hpp | 1+
Minclude/link.hpp | 1+
Minclude/list-item.hpp | 2+-
Minclude/quote.hpp | 2+-
Minclude/utils.hpp | 2++
Msrc/heading.cpp | 25++++++++++++++++++-------
Msrc/link.cpp | 17++++++++++++-----
Msrc/list-item.cpp | 9+++++++--
Msrc/quote.cpp | 9+++++++--
Msrc/utils.cpp | 9+++++++++
Mtests/browser-test.cpp | 8++++++++
Mtests/pure-test.cpp | 40++++++++++++++++++++++++++++++++++++++++
14 files changed, 122 insertions(+), 29 deletions(-)

diff --git a/Makefile b/Makefile @@ -10,22 +10,22 @@ install: build cp gem-browser.out ${PREFIX}/bin/gem-browser clean: - rm -rf test.out gem-browser.out + rm -rf test*.out gem-browser.out browser-test: - ${TCOMMAND_P} tests/browser-test.cpp ${TCOMMAND_S} -o test.out - ./test.out - rm test.out + ${TCOMMAND_P} tests/browser-test.cpp ${TCOMMAND_S} -o test1.out + ./test1.out + rm test1.out fetch-test: - ${TCOMMAND_P} tests/fetch-test.cpp ${TCOMMAND_S} -o test.out - ./test.out - rm test.out + ${TCOMMAND_P} tests/fetch-test.cpp ${TCOMMAND_S} -o test2.out + ./test2.out + rm test2.out pure-test: - ${TCOMMAND_P} tests/pure-test.cpp ${TCOMMAND_S} -o test.out - ./test.out - rm test.out + ${TCOMMAND_P} tests/pure-test.cpp ${TCOMMAND_S} -o test3.out + ./test3.out + rm test3.out test: pure-test browser-test fetch-test diff --git a/TODO.txt b/TODO.txt @@ -6,7 +6,6 @@ - lynx just yoinks the final part of the path, it seems - way to show where links go - text search -- character encoding options - should reset y value when switching between some tabs - persistent memory sometimes though. - perhaps on the basis of a page? sort of wonder about that though; like should this be stored in browser and then perhaps @@ -79,3 +78,8 @@ - enter information into a status code 10 input - status code 10 sends me back to where I just was - basically, it's weird that pressing back would ever send you to the same page you were on before; same with forwards + +--- + +- bookmarks: + - diff --git a/include/heading.hpp b/include/heading.hpp @@ -8,6 +8,7 @@ class Heading : public Line { int headingLevel = 1; std::string actualText; + std::string toDraw; public: Heading(std::string text); std::string textToDraw() override; diff --git a/include/link.hpp b/include/link.hpp @@ -9,6 +9,7 @@ class Link : public Line { private: bool invalid = false; int linkNumber = -1; + std::string renderedText; uri linkDestination = uri("gemini://example.com"); std::optional<std::string> linkText; uri parseDestination(std::string destination, std::optional<uri> prior); diff --git a/include/list-item.hpp b/include/list-item.hpp @@ -3,7 +3,7 @@ class ListItem : public Line { private: - std::string text = ""; + std::string textToRender; public: ListItem(std::string text); std::string textToDraw() override; diff --git a/include/quote.hpp b/include/quote.hpp @@ -3,7 +3,7 @@ class Quote : public Line { private: - std::string text = ""; + std::string textToRender; public: Quote(std::string text); std::string textToDraw() override; diff --git a/include/utils.hpp b/include/utils.hpp @@ -22,6 +22,8 @@ std::string urlEncode(const std::string& value); std::string getNewTab(); void openUrl(const std::string& url); std::vector<std::pair<std::string, TextRender>> breakLines(std::vector<std::pair<std::string, TextRender>>& strLs, int width, int cols); +std::string stripLeadingWhiteSpace(std::string& input); + // This will over-write the existing file, if relevant. This will not create directories for you. void writeStringToFile(std::string toWrite, std::string filePath); diff --git a/src/heading.cpp b/src/heading.cpp @@ -1,5 +1,6 @@ #include "../include/heading.hpp" #include "../include/errors.hpp" +#include "../include/utils.hpp" #include <ncurses.h> Heading::Heading(std::string text) : actualText(text) { @@ -7,22 +8,32 @@ Heading::Heading(std::string text) : actualText(text) { // TODO: Actually handle whitespace correctly here. if(text.substr(0,3) == "###") { headingLevel = 3; - return; + toDraw = actualText.substr(3); } - if(text.substr(0,2) == "##") { + else if(text.substr(0,2) == "##") { headingLevel = 2; - return; + toDraw = actualText.substr(2); } - - if(text.substr(0,1) == "#") { + else if(text.substr(0,1) == "#") { headingLevel = 1; - return; + toDraw = actualText.substr(1); } + + toDraw = stripLeadingWhiteSpace(toDraw); } std::string Heading::textToDraw() { - return actualText; + switch (headingLevel) { + case 3: + return "### " + toDraw; + case 2: + return "## " + toDraw; + case 1: + return "# " + toDraw; + default: + throw NotImplemented(); + } } int Heading::getColor() { switch (headingLevel) { diff --git a/src/link.cpp b/src/link.cpp @@ -44,6 +44,17 @@ Link::Link(std::string text, std::optional<uri> prior, int linkNumber) { } else { linkDestination = parseDestination(afterWhite, prior); } + + if(linkText != std::nullopt) { + std::string tmp = linkText.value(); + std::string rdlt = stripLeadingWhiteSpace(tmp); + + renderedText = "[" + std::to_string(linkNumber) + "] " + rdlt; + } else { + std::string tmp = linkDestination.to_string(); + std::string rdlt = stripLeadingWhiteSpace(tmp); + renderedText = "[" + std::to_string(linkNumber) + "] " + linkDestination.to_string(); + } } uri Link::parseDestination(std::string destination, std::optional<uri> prior) { @@ -87,11 +98,7 @@ std::optional<std::string> Link::getLinkText() { } std::string Link::textToDraw() { - if(linkText != std::nullopt) { - return "[" + std::to_string(linkNumber) + "] " + linkText.value() + "\n"; - - } - return "[" + std::to_string(linkNumber) + "] " + linkDestination.to_string() + "\n"; + return renderedText; } int Link::getColor() { diff --git a/src/list-item.cpp b/src/list-item.cpp @@ -1,9 +1,14 @@ #include "../include/list-item.hpp" +#include "../include/utils.hpp" -ListItem::ListItem(std::string input) : text(input) {} +ListItem::ListItem(std::string input) { + textToRender = input.substr(1); + textToRender = stripLeadingWhiteSpace(textToRender); + textToRender = "* " + textToRender; +} std::string ListItem::textToDraw() { - return text + "\n"; + return textToRender; } int ListItem::getColor() { diff --git a/src/quote.cpp b/src/quote.cpp @@ -1,10 +1,15 @@ #include "../include/quote.hpp" +#include "../include/utils.hpp" #include <ncurses.h> -Quote::Quote(std::string input) : text(input) {} +Quote::Quote(std::string input) { + textToRender = input.substr(1); + textToRender = stripLeadingWhiteSpace(textToRender); + textToRender = "> " + textToRender; +} std::string Quote::textToDraw() { - return text + "\n"; + return textToRender; } int Quote::getColor() { diff --git a/src/utils.cpp b/src/utils.cpp @@ -59,6 +59,15 @@ std::string readFileToString(std::string filePath) { return sstr.str(); } +std::string stripLeadingWhiteSpace(std::string& input) { + int x = 0; + + while(x < input.size() && isWhiteSpace(input, x)) { + x += 1; + } + return input.substr(x); +} + bool isWhiteSpace(std::string& line, int idx) { return line[idx] == ' ' || line[idx] == '\t'; } diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp @@ -26,6 +26,14 @@ TEST_CASE( "Local filesystem relative navigation" ) { REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/basic_2.gmi")); } +TEST_CASE ("Local filesystems navigation via links") { + Browser b{}; + b.goToSite("file:///home/andrew/gitRepos/gemini-browser/tests/sites/basic.gmi", true); + REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/basic.gmi")); + b.followLinkNumber(1); + REQUIRE(b.getCurrentSite()->getBody() == readFileToString("tests/sites/basic_2.gmi")); +} + TEST_CASE("Test browser doesn't crash on invalid sites.") { Browser b{}; b.goToSite("gemini://this_site_doesn_t_exsist"); diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp @@ -157,3 +157,43 @@ TEST_CASE("Test normal line classification") { } +TEST_CASE("Test line parsing handles whitespace correctly") { + + std::unordered_map<std::string, LineType> prefixes {}; + + prefixes["*"] = LIST_ITEM; + prefixes[">"] = QUOTE; + prefixes["#"] = H1; + prefixes["##"] = H2; + prefixes["###"] = H3; + + std::vector<std::string> options{" ", "\t"}; + + for(auto& prefix: prefixes) { + for(int x = 0; x < 100; ++x) { + + std::string acc = options[rand() % 2]; + + for(int i = 0; i < 1000; ++i) { + std::string check = prefix.first + acc + "test line content"; + auto ln = lineToLine(check, std::nullopt, 1, false); + REQUIRE(ln->type() == prefix.second); + REQUIRE(ln->textToDraw() == prefix.first + " test line content"); + acc += options[rand() % 2]; + } + } + } + + std::string acc = options[rand() % 2]; + + // TODO: Should add tests for non-human readable links as well. + for(int x = 0; x < 10; ++x) { + for(int i = 0; i < 10; ++i) { + std::string check = "=>" + acc + "gemini://laack.co" + acc + "link human text"; + auto ln = lineToLine(check, std::nullopt, 1, false); + REQUIRE(ln->type() == LINK); + REQUIRE(ln->textToDraw() == "[1] link human text"); + acc += options[rand() % 2]; + } + } +}