foreseen

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

commit 83e3ac80d967353229a75450a6fe600c21d34b31
parent 68aff8bb8e8a94c93c9cbf87493217e023b5acdb
Author: Andrew Laack <andrew@laack.co>
Date:   Wed, 30 Sep 2026 10:30:01 -0500

Added feature to open current page in editor.

Diffstat:
MTODO.txt | 4++++
Mconfig.mk | 2+-
Minclude/browser.hpp | 2+-
Minclude/shared.hpp | 1-
Minclude/utils.hpp | 1+
Msrc/browser.cpp | 12+++++++-----
Msrc/render.cpp | 22++++++++++++++++++++--
Msrc/utils.cpp | 11+++++++++++
Mtests/puppet-test.cpp | 37+++++++++++++++++++++++++++++++++++++
9 files changed, 82 insertions(+), 10 deletions(-)

diff --git a/TODO.txt b/TODO.txt @@ -137,3 +137,7 @@ - perf metrics / benchmarks - handle downloading in tests... + +--- + +- refactoring so that everything can be changed via function inputs (no macros / globals / limit env) diff --git a/config.mk b/config.mk @@ -9,7 +9,7 @@ TLIBS = -lCatch2Main -lCatch2 -lrapidcheck ${LIBS} CC = g++ IFLAGS = -Ofast -std=c++23 -DFLAGS = -O0 -fsanitize=address,undefined -g -std=c++23 -Wpedantic -Wall -Wextra -Wno-deprecated-declarations +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/browser.hpp b/include/browser.hpp @@ -37,7 +37,7 @@ class Browser { void setDone(int threadIdx); void refresh(); Site* getCurrentSite(); - std::string tryDownloadPage(std::string destinationPath = "") noexcept; + std::string tryDownloadPage(std::string destinationDir = "") noexcept; Link* getCurrentLink(); std::optional<uri> getPriorUri(); std::vector<std::pair<std::string, TextRender>> renderSite(); diff --git a/include/shared.hpp b/include/shared.hpp @@ -3,7 +3,6 @@ #include <string> const std::string DEFAULT_SEARCH_ENGINE = "gemini://tlgs.one/search?"; - 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) diff --git a/include/utils.hpp b/include/utils.hpp @@ -47,3 +47,4 @@ Destination handleDestinationResolution(std::string destination, bool isCli); void sanitizeCharactersToDraw(std::vector<std::pair<std::string, TextRender>>& strLs); bool isSendableIfGeminiUrl(const uri& u); +std::string getEditor(); diff --git a/src/browser.cpp b/src/browser.cpp @@ -1,5 +1,6 @@ #include "../include/browser.hpp" #include <cassert> +#include <filesystem> #include <string> #include <thread> #include <unistd.h> @@ -15,18 +16,19 @@ #include <vector> -std::string Browser::tryDownloadPage(std::string downloadPath) noexcept { +std::string Browser::tryDownloadPage(std::string downloadDir) noexcept { std::string body = currentSite->getBody(); Link* current = getCurrentLink(); assert(current != nullptr); // calling download page should always happen from a page... std::string destination = encodeAsFilename(current->getLinkDestination()); - if(downloadPath != "") { - destination = downloadPath; - } - try { + if(downloadDir != "") { + std::filesystem::path pth = std::filesystem::path(downloadDir); + std::filesystem::create_directories(pth); + destination = pth / destination; + } writeStringToFile(body, destination); } catch (...) { return ""; diff --git a/src/render.cpp b/src/render.cpp @@ -6,6 +6,8 @@ #include <chrono> #include <csignal> #include <cstdint> +#include <cstdio> +#include <cstdlib> #include <iostream> #include <ncurses.h> #include <locale.h> @@ -18,6 +20,14 @@ #define CTRL(c) ((c) & 037) #endif +#ifdef DEBUG_MODE + const char* OUT_LOCATION = "/tmp/foreseen"; // TODO: This is bad. Pass these into fn +#else + const char* OUT_LOCATION = ""; +#endif + + + int lowestPos(std::vector<std::pair<std::string, TextRender>>& strLs) { return (strLs.size() - (LINES - 2)) + 1; // this gives us two new lines at the end because the last line should contain a \n. } @@ -343,14 +353,22 @@ bool mainLoop(DrawState& ds, Browser& b, int input) { ds.y = 0; // todo: make this part of state somewhere. } else if(input == 'd') { // TODO: Handle outLocation == "" meaning failed - std::string outLocation = b.tryDownloadPage(); + std::string outLocation = b.tryDownloadPage(OUT_LOCATION); } else if(input == 'b') { b.goBack(); ds.y = 0; // todo: make this part of state somewhere. - } else if(input == 'o') { ds.handleOpenOther = true; mainLoop(ds, b, KEY_RESIZE); + } else if (input == 'e'){ + std::string editor = getEditor(); + std::string dl = b.tryDownloadPage(OUT_LOCATION); + if(dl != "") { + def_prog_mode(); + endwin(); + system((editor + " " + dl).c_str()); + refresh(); + } } // this is the loop where we deal with redirects and stuff like that. diff --git a/src/utils.cpp b/src/utils.cpp @@ -167,6 +167,7 @@ std::string getNewTab() { "* b -> back a page\n" "* f -> forward a page\n" "* o -> show url entry / link selection\n" + "* e -> open current page in your preferred text editor\n" "* (r | C-r) -> refresh page\n" ; return st; @@ -419,3 +420,13 @@ void sanitizeCharactersToDraw(std::vector<std::pair<std::string, TextRender>>& s bool isSendableIfGeminiUrl(const uri& u) { return u.get_scheme() != "gemini" || u.to_string().size() <= 1024; } + +std::string getEditor() { + const char* editor = std::getenv("EDITOR"); + if(editor) { + return std::string{editor}; + } else { + // if editor isn't set, default to nano + return "nano"; + } +} diff --git a/tests/puppet-test.cpp b/tests/puppet-test.cpp @@ -1,4 +1,6 @@ #include <catch2/catch_test_macros.hpp> +#include <cstdlib> +#include <filesystem> #include <rapidcheck.h> #include "../include/render.hpp" #include <ncurses.h> @@ -240,6 +242,8 @@ TEST_CASE("Enter an input and then cancel") { TEST_CASE("Only return false on 'q' entry when not using input boxes, never crash, junk inputs") { + setenv("EDITOR", "test" , 1); + rc::check("Never return false / fail except with input 'q'", [] (std::string st) { Browser b {}; DrawState ds {}; @@ -259,6 +263,8 @@ TEST_CASE("Only return false on 'q' entry when not using input boxes, never cras } TEST_CASE("Only return false on 'q' entry when not using input boxes, alphanumeric inputs") { + setenv("EDITOR", "test" , 1); + for(int i = 0; i < 5; ++i) { Browser b {}; DrawState ds {}; @@ -278,3 +284,34 @@ TEST_CASE("Only return false on 'q' entry when not using input boxes, alphanumer } } + +TEST_CASE("Open file in editor creates file") { + + setenv("EDITOR", "test" , 1); + + std::filesystem::remove("/tmp/foreseen/laack.co"); + + Browser b {}; + DrawState ds {}; + + REQUIRE(mainLoop(ds, b, 'o')); + REQUIRE(mainLoop(ds, b, 'l')); + REQUIRE(mainLoop(ds, b, 'a')); + REQUIRE(mainLoop(ds, b, 'a')); + REQUIRE(mainLoop(ds, b, 'c')); + REQUIRE(mainLoop(ds, b, 'k')); + REQUIRE(mainLoop(ds, b, '.')); + REQUIRE(mainLoop(ds, b, 'c')); + REQUIRE(mainLoop(ds, b, 'o')); + REQUIRE(mainLoop(ds, b, '\n')); + + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://laack.co"); + + REQUIRE(mainLoop(ds,b,'e')); + REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == "gemini://laack.co"); + + REQUIRE(std::filesystem::exists("/tmp/foreseen/laack.co")); + REQUIRE(std::filesystem::remove("/tmp/foreseen/laack.co")); + +} +