commit a3a7e23c3d93524e025e8109f60363ca3b1ed1f0
parent f7e99b30c1439e5d57628873f6ee7b0745a0aebf
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 22 Sep 2026 11:04:52 -0500
Track link number on screen and render it. Render additional colors on the basis of lines.
Diffstat:
11 files changed, 98 insertions(+), 34 deletions(-)
diff --git a/include/browser.hpp b/include/browser.hpp
@@ -16,6 +16,6 @@ class Browser {
void goToSite(std::string url);
Site* getCurrentSite();
std::optional<uri> getPriorUri();
- std::string renderSite();
+ std::vector<std::pair<std::string, int>> renderSite();
std::vector<Line*> toLines(Site* site);
};
diff --git a/include/line.hpp b/include/line.hpp
@@ -2,7 +2,15 @@
#include <string>
+
+enum LineType {
+ LINK,
+ PLAINTEXT
+};
+
class Line {
public:
virtual std::string textToDraw() = 0;
+ virtual int getColor() = 0;
+ virtual LineType type() = 0;
};
diff --git a/include/link.hpp b/include/link.hpp
@@ -10,10 +10,13 @@ class Link : public Line {
uri linkDestination = uri("gemini://example.com");
std::optional<std::string> linkText;
bool invalid;
+ int linkNumber;
uri parseDestination(std::string destination, std::optional<uri> prior);
public:
- Link(std::string text, std::optional<uri> prior = std::nullopt);
+ Link(std::string text, std::optional<uri> prior = std::nullopt, int linkNumber = -1);
uri getLinkDestination();
std::optional<std::string> getLinkText();
std::string textToDraw() override;
+ int getColor() override;
+ LineType type() override;
};
diff --git a/include/plaintext.hpp b/include/plaintext.hpp
@@ -6,5 +6,7 @@ class Plaintext : public Line {
std::string text = "";
public:
Plaintext(std::string text);
- std::string textToDraw();
+ std::string textToDraw() override;
+ int getColor() override;
+ LineType type() override;
};
diff --git a/include/utils.hpp b/include/utils.hpp
@@ -10,4 +10,4 @@ std::string readFileToString(std::string filePath);
bool isWhiteSpace(std::string& line, int idx);
std::string truncateAfter(std::string input, char truncate);
std::vector<std::string> stringToList(std::string input);
-Line* lineToLine(std::string input, std::optional<uri> prior);
+Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount);
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -4,6 +4,8 @@
#include "../include/utils.hpp"
#include <iostream>
#include <optional>
+#include <utility>
+#include <vector>
void Browser::goToSite(std::string url) {
@@ -38,12 +40,18 @@ void Browser::goToSite(std::string url) {
// TODO: This is a pure function.
std::vector<Line*> Browser::toLines(Site* site) {
+
auto lines = stringToList(site->getBody());
std::vector<Line*> res{};
+ int lc = 1;
+
for(auto line: lines) {
- res.push_back(lineToLine(line, getPriorUri()));
+ res.push_back(lineToLine(line, getPriorUri(), lc));
+ if(res[res.size() - 1]->type() == LINK) {
+ lc += 1;
+ }
}
return res;
}
@@ -57,10 +65,11 @@ Site* Browser::getCurrentSite() {
return currentSite;
}
-std::string Browser::renderSite() {
- std::string res = "";
+std::vector<std::pair<std::string, int>> Browser::renderSite() {
+ std::vector<std::pair<std::string, int>> res{};
for(auto* line: lines) {
- res += line->textToDraw();
+ std::pair<std::string,int> cp {line->textToDraw(), line->getColor()};
+ res.push_back(cp);
}
return res;
}
diff --git a/src/link.cpp b/src/link.cpp
@@ -1,12 +1,14 @@
#include "../include/link.hpp"
-#include <iostream>
#include <optional>
#include <string>
#include "../include/utils.hpp"
#include "../vendor/uri.hpp"
+#include <ncurses.h>
-Link::Link(std::string text, std::optional<uri> prior) {
+Link::Link(std::string text, std::optional<uri> prior, int linkNumber) {
+
+ this->linkNumber = linkNumber;
std::string afterPrefix = text;
@@ -71,7 +73,16 @@ std::optional<std::string> Link::getLinkText() {
std::string Link::textToDraw() {
if(linkText != std::nullopt) {
- return linkText.value() + "\n";
+ return "[" + std::to_string(linkNumber) + "] - " + linkText.value() + "\n";
+
}
- return linkDestination.to_string() + "\n";
+ return "[" + std::to_string(linkNumber) + "] - " + linkDestination.to_string() + "\n";
+}
+
+int Link::getColor() {
+ return COLOR_RED;
+}
+
+LineType Link::type() {
+ return LINK;
}
diff --git a/src/main.cpp b/src/main.cpp
@@ -1,29 +1,47 @@
#include "../include/browser.hpp"
-#include "../include/utils.hpp"
#include <algorithm>
-#include <iostream>
+#include <cstddef>
#include <ncurses.h>
+#include <utility>
+#include <vector>
// we do this because this is buiult against ncurses, would be nice to do away w/ this
// bc ppl use lots of emojis on gemini sites.
-std::string removeNonAscii(const std::string& s) {
- std::string out;
- for (int c: s)
- if ((c >= 0x20 && c < 0x7E) || (c == '\t' || c == '\n')) {
- out += c;
- }
- return out;
+
+void removeNonAscii(std::vector<std::pair<std::string, int>>& strLs) {
+
+ for(std::size_t i = 0; i < strLs.size(); ++i) {
+
+ std::string& s = strLs[i].first;
+
+ std::string out;
+ for (int c: s)
+ if ((c >= 0x20 && c < 0x7E) || (c == '\t' || c == '\n')) {
+ out += c;
+ }
+ strLs[i].first = out;
+ }
+ return;
}
-void draw(int y, std::vector<std::string>& strLs) {
+void initColors() {
+ for (int c = 0; c < COLORS; ++c) {
+ init_pair(c + 1, c, -1);
+ }
+}
+
+void draw(int y, std::vector<std::pair<std::string, int>>& strLs) {
move(0,0);
+
for(int i = y;i-y+1 < LINES && i < strLs.size(); ++i) {
- addstr((strLs[i] + '\n').c_str());
- move(i-y+1,0);
+ move(i - y, 0);
+ attron(COLOR_PAIR(strLs[i].second + 1));
+ addstr(strLs[i].first.c_str());
+ attroff(COLOR_PAIR(strLs[i].second + 1));
}
}
-int lowestPos(std::vector<std::string>& strLs) {
+int lowestPos(std::vector<std::pair<std::string, int>>& strLs) {
return strLs.size() - LINES;
}
@@ -37,10 +55,15 @@ int main() {
curs_set(0); // hide cursor
keypad(stdscr,TRUE);
- b.goToSite("gemini://tlgs.one/");
+ // TODO: Check colors available first
+ start_color();
+ use_default_colors();
+ initColors();
+
+ b.goToSite("gemini://laack.co");
+
auto current = b.renderSite();
- current = removeNonAscii(current);
- auto strLs = stringToList(current);
+ removeNonAscii(current);
int input;
int y = 0;
@@ -54,7 +77,7 @@ int main() {
} else if (input == 'g'){
y = 0;
} else if (input == 'G'){
- y = lowestPos(strLs);
+ y = lowestPos(current);
} else if (input == 0x04){
y += LINES / 2;
} else if (input == 0x15) {
@@ -63,9 +86,9 @@ int main() {
y = std::max(0,y);
- y = std::max(0,std::min(y,lowestPos(strLs)));
+ y = std::max(0,std::min(y,lowestPos(current)));
- draw(y, strLs);
+ draw(y, current);
refresh();
diff --git a/src/plaintext.cpp b/src/plaintext.cpp
@@ -7,3 +7,12 @@ Plaintext::Plaintext(std::string input) {
std::string Plaintext::textToDraw() {
return text + "\n";
}
+
+int Plaintext::getColor() {
+ return 15;
+}
+
+LineType Plaintext::type() {
+ return PLAINTEXT;
+}
+
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -58,9 +58,9 @@ std::vector<std::string> stringToList(std::string input)
return res;
}
-Line* lineToLine(std::string input, std::optional<uri> prior) {
+Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount) {
if(input.substr(0,2) == "=>") {
- Link* ln = new Link{input, prior};
+ Link* ln = new Link{input, prior, linkCount};
return ln;
}
return new Plaintext{input};
diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp
@@ -1,5 +1,4 @@
#include <catch2/catch_test_macros.hpp>
-#include "../include/link.hpp"
#include "../include/browser.hpp"
#include "../include/utils.hpp"