foreseen

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

commit c8f72c9f77dd3ce1bf25837d2729fc78d57d9cfd
parent 7740e4225641f18a224077e819779c15b0c39312
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 29 Sep 2026 14:07:14 -0500

Pulled out rendering code into dedicated file + header

Diffstat:
Mconfig.mk | 2+-
Ainclude/render.hpp | 32++++++++++++++++++++++++++++++++
Msrc/main.cpp | 406+------------------------------------------------------------------------------
Asrc/render.cpp | 402+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 436 insertions(+), 406 deletions(-)

diff --git a/config.mk b/config.mk @@ -13,7 +13,7 @@ DFLAGS = -O0 -fsanitize=address,undefined -g -std=c++23 -Wpedantic -Wall -Wextra CTFLAGS = -O0 -fsanitize=undefined -g -std=c++23 -Wpedantic -Wall -Wextra -Wno-deprecated-declarations -fprofile-arcs -ftest-coverage -fPIC -BASE_FILES = src/identity-manager.cpp src/quote.cpp src/list-item.cpp src/preformatted.cpp src/format-switch.cpp src/cache.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp src/heading.cpp +BASE_FILES = src/identity-manager.cpp src/quote.cpp src/list-item.cpp src/preformatted.cpp src/format-switch.cpp src/cache.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp src/heading.cpp src/render.cpp COMMAND_P = ${CC} ${CFLAGS} -D NDEBUG COMMAND_S = ${BASE_FILES} ${LIBS} diff --git a/include/render.hpp b/include/render.hpp @@ -0,0 +1,32 @@ +#include "../include/browser.hpp" +#include "../include/utils.hpp" +#include <cassert> +#include <cstdint> +#include <ncurses.h> +#include <string> +#include <unctrl.h> +#include <utility> +#include <vector> + +#pragma once + +struct DrawState { + std::vector<std::pair<std::string, TextRender>> prior; + Browser* bPtr; + std::string header; + int y; + bool handleInput; + bool handleOpenOther; + bool handleRedirect; + std::string issueText; + uint64_t timeToClearIssueText; + std::string redirInput; + std::string openOtherInput; + std::string userInput; + std::string metaLine; + bool toLowest = false; +}; + +bool mainLoop(DrawState& ds, Browser& b, int input); +void tryVisitSite(DrawState& ds , Browser& b, std::string site); +void initColors(); diff --git a/src/main.cpp b/src/main.cpp @@ -1,5 +1,6 @@ #include "../include/browser.hpp" #include "../include/utils.hpp" +#include "../include/render.hpp" #include <algorithm> #include <cassert> #include <chrono> @@ -12,411 +13,6 @@ #include <utility> #include <vector> -#ifndef CTRL -#define CTRL(c) ((c) & 037) -#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. -} - -bool isValidUserInput(int uinput) { - if ((uinput >= 0x20 && uinput <= 0x7E)) { - return true; - } - return false; -} -void initColors() { - if(has_colors()) { - start_color(); - use_default_colors(); - for (int c = 0; c < COLORS && c+1 < COLOR_PAIRS; ++c) { - init_pair(c + 1, c, -1); - } - } -} - -struct DrawState { - std::vector<std::pair<std::string, TextRender>> prior; - Browser* bPtr; - std::string header; - int y; - bool handleInput; - bool handleOpenOther; - bool handleRedirect; - std::string issueText; - int64_t timeToClearIssueText; - std::string redirInput; - std::string openOtherInput; - std::string userInput; - std::string metaLine; - bool toLowest = false; -}; - -void drawInputBox(std::string text, std::string userInput) { - - move(LINES/2-1, COLS/4); - - attron(COLOR_PAIR(COLOR_CYAN+1)); - for(int i = 0; i < COLS/2; ++i) { - addstr("-"); - } - - move(LINES/2 + 1, COLS/4); - for(int i = 0; i < COLS/2; ++i) { - addstr("-"); - } - attroff(COLOR_PAIR(COLOR_CYAN+1)); - - int userInputSize = userInput.size(); - int width = COLS/2; - - if((int)text.size() >= width) { - text = text.substr(0,width-6) + "...: "; - } - - int textSize = text.size(); - - assert(textSize < width); - - std::string userTextToRender = userInput; - - int delta = width - (textSize + userInputSize); - - if(delta < 0) { - userTextToRender = userInput.substr(delta*-1, userInput.size()); - } else { - while(delta != 0) { - userTextToRender.append(" "); // this makes sure the background doesn't leak through. - delta -= 1; - } - } - - move(LINES/2, COLS/4); - addstr(text.c_str()); - addstr(userTextToRender.c_str()); - -} -uint64_t getCurrentTime() { - return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); -} - -void draw(DrawState& ds) { - if(COLS < 20) { - erase(); - addstr("Screen width too small."); - return; - } - if(LINES < 3) { - erase(); - addstr("Screen height too small."); - return; - } - - auto* cs = ds.bPtr->getCurrentSite(); - - if (!(cs != nullptr && (cs->getStatusCode() < 20 || cs->getStatusCode() > 29))) { - ds.prior = ds.bPtr->renderSite(); - } - - auto current = breakLines(ds.prior, std::min(COLS, maxWidth), COLS); - sanitizeCharactersToDraw(current); - if(ds.toLowest) { - ds.y = lowestPos(current); - ds.toLowest = false; - } - - ds.y = std::max(0,std::min(ds.y,lowestPos(current))); - - erase(); - move(0,(COLS / 2) - ((int)ds.header.size() / 2) ); - - attron(A_BOLD); - - if((int)ds.header.size() < COLS) { - addstr(ds.header.c_str()); - } else { - addstr((ds.header.substr(0,COLS-3) + "...").c_str()); - } - - attroff(A_BOLD); - - addstr(std::string(COLS, ' ').c_str()); - - for(int i = ds.y; i - ds.y < LINES - 2 && i < (int)current.size(); ++i) { - move(i - ds.y + 2, 0); - - attron(COLOR_PAIR(current[i].second.color + 1)); - if(current[i].second.isBold) { - attron(A_BOLD); - addstr(current[i].first.c_str()); - attroff(A_BOLD); - } - else { - addstr(current[i].first.c_str()); - } - attroff(COLOR_PAIR(current[i].second.color + 1)); - } - - if (ds.handleRedirect) { - std::string toShow = "Redirect to \"" + ds.metaLine + "\" (y/n): "; - drawInputBox(toShow, ds.redirInput); - } - - if(ds.handleInput) { - if(ds.metaLine != "") { - drawInputBox(ds.metaLine + ": ", ds.userInput); - } else { - drawInputBox("Input: ", ds.userInput); - - } - } - - if (ds.handleOpenOther) { - drawInputBox("Destination / Link Number: ", ds.openOtherInput); - } - - if(ds.issueText != "") { - uint64_t now = getCurrentTime(); - if(ds.timeToClearIssueText <= now) { - ds.issueText = ""; - } else { - move(0,0); - attron(A_BOLD); - attron(COLOR_PAIR(COLOR_RED+1)); - addstr(ds.issueText.c_str()); - attroff(COLOR_PAIR(COLOR_RED+1)); - attroff(A_BOLD); - } - - } - - refresh(); - -} - -std::string openPageHandler(DrawState ds) { - - ds.handleOpenOther = true; - draw(ds); - std::string acc = ""; - - while(true) { - int sel = getch(); - - if(sel == KEY_BACKSPACE) { - if(acc.size() > 0) { - acc = acc.substr(0,acc.size() - 1); - } - ds.openOtherInput = acc; - draw(ds); - continue; - } - - if(sel == 27) { - acc = ""; - break; - } - - if(sel == '\n' || sel == KEY_ENTER) { - break; - } - - if(isValidUserInput(sel)) { - acc += std::string {(char)sel}; - } - - ds.openOtherInput = acc; - draw(ds); - - } - - ds.openOtherInput = acc; - ds.handleOpenOther = false; - draw(ds); - - - - return acc; -} - -enum Direction { - FORWARD, - BACKWARD -}; - -Direction handleRedir(DrawState ds) { - - ds.handleRedirect = true; - ds.redirInput = ""; - draw(ds); - - while(true) { - int input = getch(); - if(input == 'y') { - ds.redirInput = "y"; - draw(ds); - ds.handleRedirect = false; - return FORWARD; - } - if(input == 'n') { - ds.redirInput = "n"; - draw(ds); - ds.handleRedirect = false; - return BACKWARD; - } - draw(ds); - } -} - -std::string handleUserInput(DrawState ds) { - - ds.handleInput = true; - draw(ds); - std::string acc = ""; - - while(true) { - - int sel = getch(); - if(sel == '\n' || sel == KEY_ENTER) { - break; - } - if(sel == 27) { - acc = ""; - ds.userInput = acc; - break; - } - if(sel == KEY_BACKSPACE) { - if(acc.size() > 0) { - acc = acc.substr(0,acc.size() - 1); - } - ds.userInput = acc; - draw(ds); - continue; - } - - if(isValidUserInput(sel)) { - acc += std::string {(char)sel}; - } - ds.userInput = acc; - draw(ds); - } - - ds.handleInput = false; - draw(ds); - return "?" + acc; -} - -void tryVisitSite(DrawState& ds , Browser& b, std::string site) { - bool visitSuccess = b.goToSite(site); - if(!visitSuccess) { - ds.issueText = "Unable to access the requested site."; - ds.timeToClearIssueText = getCurrentTime() + 1000; - } else { - ds.y = 0; - } -} - -bool mainLoop(DrawState& ds, Browser& b, int input) { - if(input == 'q') { - return false; - } - - if(input == KEY_DOWN) { - ds.y += 1; - } else if (input == KEY_UP){ - ds.y -= 1; - } else if (input == 'g'){ - ds.y = 0; - } else if (input == 'G'){ - ds.toLowest = true; - } else if (input == CTRL('d')){ - ds.y += LINES / 2; - } else if (input == CTRL('u')) { - ds.y -= LINES / 2; - } else if (input == 'r' || input == CTRL('r')){ - b.refresh(); - } else if(input == 'f') { - b.goForward(); - ds.y = 0; // todo: make this part of state somewhere. - } else if(input == 'd') { - // TODO: Handle outLocation == "" meaning failed - std::string outLocation = b.tryDownloadPage(); - } else if(input == 'b') { - b.goBack(); - ds.y = 0; // todo: make this part of state somewhere. - - } else if(input == 'o') { - std::string locationToGo = openPageHandler(ds); - Destination destination = handleDestinationResolution(locationToGo, false); - - bool res; - - switch(destination.t) { - case NUMBER_DESTINATION: - res = b.followLinkNumber(destination.linkNumber); - if(res) { - ds.y = 0; - } - break; - case STRING_DESTINATION: - tryVisitSite(ds, b, destination.destination); - break; - case NO_DESTINATION: - break; - } - } - - // this is the loop where we deal with redirects and stuff like that. - while(b.getCurrentSite()->getStatusCode() < 20 || b.getCurrentSite()->getStatusCode() > 29) { - if(b.getCurrentSite()->getStatusCode() >= 10 && b.getCurrentSite()->getStatusCode() <= 19) { - auto* st = b.getCurrentSite(); - if(st != nullptr) { - ds.metaLine = st->getMeta(); - } - - std::string inputQuery = handleUserInput(ds); - if(inputQuery != "?") { // TODO: Better handling - tryVisitSite(ds, b,inputQuery); - } else { - b.goBack(); - } - - } else if (b.getCurrentSite()->getStatusCode() >= 30 && b.getCurrentSite()->getStatusCode() <= 39){ - - auto* st = b.getCurrentSite(); - if(st != nullptr) { - ds.metaLine = st->getMeta(); - } - - Direction dir = handleRedir(ds); - if(dir == BACKWARD) { - b.goBack(); - } else { - tryVisitSite(ds, b, b.getCurrentSite()->getMeta()); - } - } else { // this should handle invalid status codes - b.goBack(); - } - } - - auto* clk = b.getCurrentLink(); - if(clk != nullptr) { - ds.header = clk->getLinkDestination().to_string(); - } else { - ds.header = "foreseen"; - } - - auto* st = b.getCurrentSite(); - if(st != nullptr) { - ds.metaLine = st->getMeta(); - } - - draw(ds); - refresh(); - return true; -} - int main(int argc, char** argv) { std::signal(SIGPIPE, SIG_IGN); // need this in case of swapping network connections bc that shouldn't kill the whole process. diff --git a/src/render.cpp b/src/render.cpp @@ -0,0 +1,402 @@ +#include "../include/browser.hpp" +#include "../include/utils.hpp" +#include "../include/render.hpp" +#include <algorithm> +#include <cassert> +#include <chrono> +#include <csignal> +#include <cstdint> +#include <ncurses.h> +#include <locale.h> +#include <string> +#include <unctrl.h> +#include <utility> +#include <vector> + +#ifndef CTRL +#define CTRL(c) ((c) & 037) +#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. +} + +bool isValidUserInput(int uinput) { + if ((uinput >= 0x20 && uinput <= 0x7E)) { + return true; + } + return false; +} +void initColors() { + if(has_colors()) { + start_color(); + use_default_colors(); + for (int c = 0; c < COLORS && c+1 < COLOR_PAIRS; ++c) { + init_pair(c + 1, c, -1); + } + } +} + +void drawInputBox(std::string text, std::string userInput) { + + move(LINES/2-1, COLS/4); + + attron(COLOR_PAIR(COLOR_CYAN+1)); + for(int i = 0; i < COLS/2; ++i) { + addstr("-"); + } + + move(LINES/2 + 1, COLS/4); + for(int i = 0; i < COLS/2; ++i) { + addstr("-"); + } + attroff(COLOR_PAIR(COLOR_CYAN+1)); + + int userInputSize = userInput.size(); + int width = COLS/2; + + if((int)text.size() >= width) { + text = text.substr(0,width-6) + "...: "; + } + + int textSize = text.size(); + + assert(textSize < width); + + std::string userTextToRender = userInput; + + int delta = width - (textSize + userInputSize); + + if(delta < 0) { + userTextToRender = userInput.substr(delta*-1, userInput.size()); + } else { + while(delta != 0) { + userTextToRender.append(" "); // this makes sure the background doesn't leak through. + delta -= 1; + } + } + + move(LINES/2, COLS/4); + addstr(text.c_str()); + addstr(userTextToRender.c_str()); + +} +uint64_t getCurrentTime() { + return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); +} + +void draw(DrawState& ds) { + if(COLS < 20) { + erase(); + addstr("Screen width too small."); + return; + } + if(LINES < 3) { + erase(); + addstr("Screen height too small."); + return; + } + + auto* cs = ds.bPtr->getCurrentSite(); + + if (!(cs != nullptr && (cs->getStatusCode() < 20 || cs->getStatusCode() > 29))) { + ds.prior = ds.bPtr->renderSite(); + } + + auto current = breakLines(ds.prior, std::min(COLS, maxWidth), COLS); + sanitizeCharactersToDraw(current); + if(ds.toLowest) { + ds.y = lowestPos(current); + ds.toLowest = false; + } + + ds.y = std::max(0,std::min(ds.y,lowestPos(current))); + + erase(); + move(0,(COLS / 2) - ((int)ds.header.size() / 2) ); + + attron(A_BOLD); + + if((int)ds.header.size() < COLS) { + addstr(ds.header.c_str()); + } else { + addstr((ds.header.substr(0,COLS-3) + "...").c_str()); + } + + attroff(A_BOLD); + + addstr(std::string(COLS, ' ').c_str()); + + for(int i = ds.y; i - ds.y < LINES - 2 && i < (int)current.size(); ++i) { + move(i - ds.y + 2, 0); + + attron(COLOR_PAIR(current[i].second.color + 1)); + if(current[i].second.isBold) { + attron(A_BOLD); + addstr(current[i].first.c_str()); + attroff(A_BOLD); + } + else { + addstr(current[i].first.c_str()); + } + attroff(COLOR_PAIR(current[i].second.color + 1)); + } + + if (ds.handleRedirect) { + std::string toShow = "Redirect to \"" + ds.metaLine + "\" (y/n): "; + drawInputBox(toShow, ds.redirInput); + } + + if(ds.handleInput) { + if(ds.metaLine != "") { + drawInputBox(ds.metaLine + ": ", ds.userInput); + } else { + drawInputBox("Input: ", ds.userInput); + + } + } + + if (ds.handleOpenOther) { + drawInputBox("Destination / Link Number: ", ds.openOtherInput); + } + + if(ds.issueText != "") { + uint64_t now = getCurrentTime(); + if(ds.timeToClearIssueText <= now) { + ds.issueText = ""; + } else { + move(0,0); + attron(A_BOLD); + attron(COLOR_PAIR(COLOR_RED+1)); + addstr(ds.issueText.c_str()); + attroff(COLOR_PAIR(COLOR_RED+1)); + attroff(A_BOLD); + } + + } + + refresh(); + +} + +std::string openPageHandler(DrawState ds) { + + ds.handleOpenOther = true; + draw(ds); + std::string acc = ""; + + while(true) { + int sel = getch(); + + if(sel == KEY_BACKSPACE) { + if(acc.size() > 0) { + acc = acc.substr(0,acc.size() - 1); + } + ds.openOtherInput = acc; + draw(ds); + continue; + } + + if(sel == 27) { + acc = ""; + break; + } + + if(sel == '\n' || sel == KEY_ENTER) { + break; + } + + if(isValidUserInput(sel)) { + acc += std::string {(char)sel}; + } + + ds.openOtherInput = acc; + draw(ds); + + } + + ds.openOtherInput = acc; + ds.handleOpenOther = false; + draw(ds); + + + + return acc; +} + +enum Direction { + FORWARD, + BACKWARD +}; + +Direction handleRedir(DrawState ds) { + + ds.handleRedirect = true; + ds.redirInput = ""; + draw(ds); + + while(true) { + int input = getch(); + if(input == 'y') { + ds.redirInput = "y"; + draw(ds); + ds.handleRedirect = false; + return FORWARD; + } + if(input == 'n') { + ds.redirInput = "n"; + draw(ds); + ds.handleRedirect = false; + return BACKWARD; + } + draw(ds); + } +} + +std::string handleUserInput(DrawState ds) { + + ds.handleInput = true; + draw(ds); + std::string acc = ""; + + while(true) { + + int sel = getch(); + if(sel == '\n' || sel == KEY_ENTER) { + break; + } + if(sel == 27) { + acc = ""; + ds.userInput = acc; + break; + } + if(sel == KEY_BACKSPACE) { + if(acc.size() > 0) { + acc = acc.substr(0,acc.size() - 1); + } + ds.userInput = acc; + draw(ds); + continue; + } + + if(isValidUserInput(sel)) { + acc += std::string {(char)sel}; + } + ds.userInput = acc; + draw(ds); + } + + ds.handleInput = false; + draw(ds); + return "?" + acc; +} + +void tryVisitSite(DrawState& ds , Browser& b, std::string site) { + bool visitSuccess = b.goToSite(site); + if(!visitSuccess) { + ds.issueText = "Unable to access the requested site."; + ds.timeToClearIssueText = getCurrentTime() + 1000; + } else { + ds.y = 0; + } +} + +bool mainLoop(DrawState& ds, Browser& b, int input) { + if(input == 'q') { + return false; + } + + if(input == KEY_DOWN) { + ds.y += 1; + } else if (input == KEY_UP){ + ds.y -= 1; + } else if (input == 'g'){ + ds.y = 0; + } else if (input == 'G'){ + ds.toLowest = true; + } else if (input == CTRL('d')){ + ds.y += LINES / 2; + } else if (input == CTRL('u')) { + ds.y -= LINES / 2; + } else if (input == 'r' || input == CTRL('r')){ + b.refresh(); + } else if(input == 'f') { + b.goForward(); + ds.y = 0; // todo: make this part of state somewhere. + } else if(input == 'd') { + // TODO: Handle outLocation == "" meaning failed + std::string outLocation = b.tryDownloadPage(); + } else if(input == 'b') { + b.goBack(); + ds.y = 0; // todo: make this part of state somewhere. + + } else if(input == 'o') { + std::string locationToGo = openPageHandler(ds); + Destination destination = handleDestinationResolution(locationToGo, false); + + bool res; + + switch(destination.t) { + case NUMBER_DESTINATION: + res = b.followLinkNumber(destination.linkNumber); + if(res) { + ds.y = 0; + } + break; + case STRING_DESTINATION: + tryVisitSite(ds, b, destination.destination); + break; + case NO_DESTINATION: + break; + } + } + + // this is the loop where we deal with redirects and stuff like that. + while(b.getCurrentSite()->getStatusCode() < 20 || b.getCurrentSite()->getStatusCode() > 29) { + if(b.getCurrentSite()->getStatusCode() >= 10 && b.getCurrentSite()->getStatusCode() <= 19) { + auto* st = b.getCurrentSite(); + if(st != nullptr) { + ds.metaLine = st->getMeta(); + } + + std::string inputQuery = handleUserInput(ds); + if(inputQuery != "?") { // TODO: Better handling + tryVisitSite(ds, b,inputQuery); + } else { + b.goBack(); + } + + } else if (b.getCurrentSite()->getStatusCode() >= 30 && b.getCurrentSite()->getStatusCode() <= 39){ + + auto* st = b.getCurrentSite(); + if(st != nullptr) { + ds.metaLine = st->getMeta(); + } + + Direction dir = handleRedir(ds); + if(dir == BACKWARD) { + b.goBack(); + } else { + tryVisitSite(ds, b, b.getCurrentSite()->getMeta()); + } + } else { // this should handle invalid status codes + b.goBack(); + } + } + + auto* clk = b.getCurrentLink(); + if(clk != nullptr) { + ds.header = clk->getLinkDestination().to_string(); + } else { + ds.header = "foreseen"; + } + + auto* st = b.getCurrentSite(); + if(st != nullptr) { + ds.metaLine = st->getMeta(); + } + + draw(ds); + refresh(); + return true; +}