foreseen

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

commit 1a7ab523a42130f06bb01bd5b36dab625a1fe6c5
parent 5b88bddb66e030f239872645ac86b5a1181e6e1b
Author: Andrew Laack <andrew@laack.co>
Date:   Thu, 24 Sep 2026 18:04:29 -0500

Basic support for downloading sites

Diffstat:
ATODO.txt | 19+++++++++++++++++++
Minclude/browser.hpp | 1+
Minclude/utils.hpp | 4++++
Msrc/browser.cpp | 9+++++++++
Msrc/main.cpp | 6++++--
Msrc/utils.cpp | 23+++++++++++++++++++++++
6 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/TODO.txt b/TODO.txt @@ -0,0 +1,19 @@ +- Certificate support? + - Be able to: + - use a certificate + - detect cert changes w/ tofu +- text boxing + - Amfora does a nice job adding extra padding to make text look pleasing; perhaps worth adding. + - not sure what algo they are using / if it's just constant padding +- multi-color lines + - current object model only supports text + color + bold, but not specifying any of this on the basis of characters + - this would be useful for link lines, which look nice to have the asterick colored differently than the text +- show redirect destination + - "do you want to follow redirect to (xyz)?" +- saving files + - footprints mode + - download all files to disk for sites you access + - should show a message after download works + - should make the filename better + - lynx just yoinks the final part of the path, it seems +- way to show where links go diff --git a/include/browser.hpp b/include/browser.hpp @@ -37,6 +37,7 @@ class Browser { void setDone(int threadIdx); void refresh(); Site* getCurrentSite(); + void downloadPage(); Link* getCurrentLink(); std::optional<uri> getPriorUri(); std::vector<std::pair<std::string, TextRender>> renderSite(); diff --git a/include/utils.hpp b/include/utils.hpp @@ -21,3 +21,7 @@ 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); + +// This will over-write the existing file, if relevant. This will not create directories for you. +void writeStringToFile(std::string toWrite, std::string filePath); +std::string encodeAsFilename(uri link); diff --git a/src/browser.cpp b/src/browser.cpp @@ -1,4 +1,5 @@ #include "../include/browser.hpp" +#include <cassert> #include <string> #include <thread> #include <unistd.h> @@ -12,6 +13,14 @@ #include <vector> +void Browser::downloadPage() { + std::string body = currentSite->getBody(); + Link* current = getCurrentLink(); + assert(current != nullptr); // calling download page should always happen from a page... + writeStringToFile(body, encodeAsFilename(current->getLinkDestination())); +} + + 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) { diff --git a/src/main.cpp b/src/main.cpp @@ -308,15 +308,17 @@ int main(int argc, char** argv) { } else if (input == 'G'){ y = lowestPos(current); } else if (input == 0x04){ + // ctrl+d y += LINES / 2; } else if (input == 0x15) { + // ctrl+u y -= LINES / 2; } else if (input == 'r' || input == CTRL('r')){ b.refresh(); } else if(input == 'f') { b.goForward(); - - + } else if(input == 'd') { + b.downloadPage(); } else if(input == 'b') { b.goBack(); diff --git a/src/utils.cpp b/src/utils.cpp @@ -7,6 +7,7 @@ #include "../include/errors.hpp" #include "../include/plaintext.hpp" #include "../include/preformatted.hpp" +#include <cassert> #include <cstddef> #include <cstdint> #include <cstdlib> @@ -15,9 +16,11 @@ #include <ios> #include <iostream> #include <linux/limits.h> +#include <regex> #include <sstream> #include <string> #include <utility> +#include <filesystem> #include <vector> #include <spawn.h> #include <sys/wait.h> @@ -200,3 +203,23 @@ std::vector<std::pair<std::string, TextRender>> breakLines(std::vector<std::pair return res; } + + +void writeStringToFile(std::string toWrite, std::string filePath) { + std::filesystem::path path{filePath}; + std::ofstream ofs(path); + ofs << toWrite; +} +std::string encodeAsFilename(uri link) { + + std::string base = link.to_string(); + assert(base.find(':') != -1); + base = base.substr(base.find(':')+1); // works for file:/// and gemini:/// + + while(base.size() > 0 && base[0] == '/') { + base = base.substr(1); + } + + std::string cleaned = std::regex_replace(base, std::regex("[^[:alnum:]._-]"), "_"); + return cleaned; +}