commit 1ffce7aa5aa6063435ce408558a0ea431f8c22a9
parent a3a7e23c3d93524e025e8109f60363ca3b1ed1f0
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 22 Sep 2026 11:34:45 -0500
Janky link following
Diffstat:
3 files changed, 62 insertions(+), 3 deletions(-)
diff --git a/include/browser.hpp b/include/browser.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include <cstddef>
#include <vector>
#include "line.hpp"
#include "site.hpp"
@@ -11,6 +12,8 @@ class Browser {
int previousIdx = -1;
Site* currentSite;
std::vector<Line*> lines;
+ std::vector<std::size_t> links; // these point to line indices
+ void setLinksOfCurrentLines();
public:
Browser();
void goToSite(std::string url);
@@ -18,4 +21,5 @@ class Browser {
std::optional<uri> getPriorUri();
std::vector<std::pair<std::string, int>> renderSite();
std::vector<Line*> toLines(Site* site);
+ void followLinkNumber(int linkToFollow);
};
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -2,7 +2,9 @@
#include "../include/site.hpp"
#include "../include/gemini-client.hpp"
#include "../include/utils.hpp"
+#include <cstddef>
#include <iostream>
+#include <iterator>
#include <optional>
#include <utility>
#include <vector>
@@ -36,8 +38,19 @@ void Browser::goToSite(std::string url) {
}
currentSite = site;
lines = toLines(site);
+ setLinksOfCurrentLines();
}
+void Browser::setLinksOfCurrentLines() {
+ links = std::vector<std::size_t> {};
+ for(std::size_t i = 0; i < lines.size(); ++i) {
+ if(lines[i]->type() == LINK) {
+ links.push_back(i);
+ }
+ }
+}
+
+
// TODO: This is a pure function.
std::vector<Line*> Browser::toLines(Site* site) {
@@ -83,3 +96,9 @@ std::optional<uri> Browser::getPriorUri() {
}
+void Browser::followLinkNumber(int linkToFollow) {
+ Line* ptr = lines[links[linkToFollow-1]];
+ Link* ptrLnk = dynamic_cast<Link*>(ptr);
+ goToSite(ptrLnk->getLinkDestination().to_string());
+}
+
diff --git a/src/main.cpp b/src/main.cpp
@@ -1,5 +1,6 @@
#include "../include/browser.hpp"
#include <algorithm>
+#include <cmath>
#include <cstddef>
#include <ncurses.h>
#include <utility>
@@ -32,7 +33,7 @@ void initColors() {
void draw(int y, std::vector<std::pair<std::string, int>>& strLs) {
move(0,0);
-
+ clear();
for(int i = y;i-y+1 < LINES && i < strLs.size(); ++i) {
move(i - y, 0);
attron(COLOR_PAIR(strLs[i].second + 1));
@@ -45,6 +46,32 @@ int lowestPos(std::vector<std::pair<std::string, int>>& strLs) {
return strLs.size() - LINES;
}
+int linkHandler() {
+
+ int acc = 0;
+ int num = 0;
+ int itr = 0;
+
+ while(num!=-1) {
+ int sel = getch();
+ num = (sel - 0x30);
+
+ if(sel == '\n' || sel == KEY_ENTER) {
+ num = -1;
+ continue;
+ }
+
+ if(num > 9 || num < 0) {
+ continue;
+ }
+ acc += num * (std::pow(10,itr));
+ itr += 1;
+ }
+
+ return acc;
+}
+
+
int main() {
Browser b{};
@@ -60,7 +87,8 @@ int main() {
use_default_colors();
initColors();
- b.goToSite("gemini://laack.co");
+ endwin();
+ b.goToSite("gemini://tlgs.one/known-hosts");
auto current = b.renderSite();
removeNonAscii(current);
@@ -82,9 +110,17 @@ int main() {
y += LINES / 2;
} else if (input == 0x15) {
y -= LINES / 2;
- }
+ } else if(input == ' ') {
+ int linkToFollow = linkHandler();
+ if(linkToFollow != -1) {
+ b.followLinkNumber(linkToFollow);
+ current = b.renderSite();
+ removeNonAscii(current);
+ }
+ }
+
y = std::max(0,y);
y = std::max(0,std::min(y,lowestPos(current)));