commit 3bf784d60b4ad9f7cf65f0ff838a0a09029644d4
parent c6c05661575b8e31c4b9caca8fc51767967008ec
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 23 Sep 2026 15:07:14 -0500
Handle line formats
Diffstat:
13 files changed, 189 insertions(+), 14 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
build:
- g++ -g src/main.cpp src/cache.cpp src/link.cpp src/plaintext.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp src/browser.cpp -lssl -lcrypto -lncursesw -o browser.out
+ g++ -g src/main.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 -lssl -lcrypto -lncursesw -o browser.out
install:
echo "Not implemented"
@@ -8,12 +8,12 @@ clean:
echo "Not implemented"
browser-test:
- g++ tests/browser-test.cpp src/cache.cpp src/plaintext.cpp src/browser.cpp src/link.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out
+ g++ tests/browser-test.cpp src/preformatted.cpp src/format-switch.cpp src/heading.cpp src/cache.cpp src/plaintext.cpp src/browser.cpp src/link.cpp src/site.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out
./test.out
rm test.out
fetch-test:
- g++ tests/fetch-test.cpp src/link.cpp src/cache.cpp src/site.cpp src/plaintext.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out
+ g++ tests/fetch-test.cpp src/link.cpp src/preformatted.cpp src/format-switch.cpp src/heading.cpp src/cache.cpp src/site.cpp src/plaintext.cpp src/utils.cpp src/gemini-client.cpp -lCatch2Main -lCatch2 -lssl -lcrypto -o test.out
./test.out
rm test.out
diff --git a/include/browser.hpp b/include/browser.hpp
@@ -26,6 +26,7 @@ class Browser {
void goToSite(std::string url, bool addToHistory, bool refresh = false);
void refresh();
Site* getCurrentSite();
+ Link* getCurrentLink();
std::optional<uri> getPriorUri();
std::vector<std::pair<std::string, int>> renderSite();
std::vector<Line*> toLines(Site* site);
diff --git a/include/format-switch.hpp b/include/format-switch.hpp
@@ -0,0 +1,12 @@
+#pragma once
+#include "line.hpp"
+
+class FormatSwitch : public Line {
+ private:
+ std::string text = "";
+ public:
+ FormatSwitch(std::string text);
+ std::string textToDraw() override;
+ int getColor() override;
+ LineType type() override;
+};
diff --git a/include/heading.hpp b/include/heading.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <optional>
+#include <string>
+#include "../vendor/uri.hpp"
+#include "line.hpp"
+
+class Heading : public Line {
+ int headingLevel = 1;
+ std::string actualText;
+ public:
+ Heading(std::string text);
+ std::string textToDraw() override;
+ int getColor() override;
+ LineType type() override;
+};
diff --git a/include/line.hpp b/include/line.hpp
@@ -5,7 +5,12 @@
enum LineType {
LINK,
- PLAINTEXT
+ PLAINTEXT,
+ H1,
+ H2,
+ H3,
+ FORMAT_SWITCH,
+ PREFORMATTED
};
class Line {
diff --git a/include/preformatted.hpp b/include/preformatted.hpp
@@ -0,0 +1,12 @@
+#pragma once
+#include "line.hpp"
+
+class Preformatted : public Line {
+ private:
+ std::string text = "";
+ public:
+ Preformatted(std::string text);
+ std::string textToDraw() override;
+ int getColor() override;
+ LineType type() override;
+};
diff --git a/include/utils.hpp b/include/utils.hpp
@@ -10,7 +10,7 @@ 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, int linkCount);
+Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount, bool isPreformatted);
std::string urlEncode(const std::string& value);
std::string getNewTab();
void openUrl(const std::string& url);
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -153,8 +153,12 @@ std::vector<Line*> Browser::toLines(Site* site) {
int lc = 1;
+ bool isPreformatted = false;
for(auto line: lines) {
- res.push_back(lineToLine(line, getPriorUri(), lc));
+ res.push_back(lineToLine(line, getPriorUri(), lc, isPreformatted));
+ if (res[res.size()-1]->type() == FORMAT_SWITCH) {
+ isPreformatted = !isPreformatted;
+ }
if(res[res.size() - 1]->type() == LINK) {
lc += 1;
}
@@ -278,3 +282,11 @@ void Browser::goForward() {
previousIdx = original;
}
}
+
+Link* Browser::getCurrentLink() {
+ if(previousIdx >= 0 && previousIdx < siteHistory.size()) {
+ return this->siteHistory[previousIdx];
+ }
+ return nullptr;
+}
+
diff --git a/src/format-switch.cpp b/src/format-switch.cpp
@@ -0,0 +1,19 @@
+#include "../include/format-switch.hpp"
+#include <ncurses.h>
+
+FormatSwitch::FormatSwitch(std::string input) {
+ text = input;
+}
+
+std::string FormatSwitch::textToDraw() {
+ return text + "\n";
+}
+
+int FormatSwitch::getColor() {
+ return COLOR_WHITE;
+}
+
+LineType FormatSwitch::type() {
+ return FORMAT_SWITCH;
+}
+
diff --git a/src/heading.cpp b/src/heading.cpp
@@ -0,0 +1,51 @@
+#include "../include/heading.hpp"
+#include "../include/errors.hpp"
+#include <ncurses.h>
+
+Heading::Heading(std::string text) {
+
+ actualText = text;
+ // TODO: Actually handle whitespace correctly here.
+ if(text.substr(0,3) == "###") {
+ headingLevel = 3;
+ return;
+ }
+
+ if(text.substr(0,2) == "##") {
+ headingLevel = 2;
+ return;
+ }
+
+ if(text.substr(0,1) == "#") {
+ headingLevel = 1;
+ return;
+ }
+}
+
+std::string Heading::textToDraw() {
+ return actualText;
+}
+int Heading::getColor() {
+ switch (headingLevel) {
+ case 3:
+ return COLOR_CYAN;
+ case 2:
+ return COLOR_GREEN;
+ case 1:
+ return COLOR_BLUE;
+ default:
+ throw NotImplemented();
+ }
+}
+LineType Heading::type() {
+ switch (headingLevel) {
+ case 3:
+ return H3;
+ case 2:
+ return H2;
+ case 1:
+ return H1;
+ default:
+ throw NotImplemented();
+ }
+}
diff --git a/src/main.cpp b/src/main.cpp
@@ -55,6 +55,7 @@ void initColors() {
}
struct DrawState {
+ std::string header;
int y;
std::vector<std::pair<std::string, int>> strLs;
bool handleInput;
@@ -108,11 +109,12 @@ void drawInputBox(std::string text, std::string userInput) {
void draw(DrawState ds) {
- move(0,0);
clear();
+ move(0,(COLS / 2) - (ds.header.size() / 2) );
+ addstr(ds.header.c_str());
for(int i = ds.y;i-ds.y+1 < LINES && i < ds.strLs.size(); ++i) {
- move(i - ds.y, 0);
+ move(i - ds.y + 1, 0);
attron(COLOR_PAIR(ds.strLs[i].second + 1));
addstr(ds.strLs[i].first.c_str());
attroff(COLOR_PAIR(ds.strLs[i].second + 1));
@@ -322,7 +324,7 @@ int main(int argc, char** argv) {
} else if(input == 'o') {
std::string locationToGo = openPageHandler(ds);
-
+
// TODO: Check if this is an int and if it is try to use that link.
// if it's not, then try to go to domain (should consider having a default search engine too.)
@@ -330,7 +332,7 @@ int main(int argc, char** argv) {
b.followLinkNumber(std::stoi(locationToGo));
} catch (...) {
if(locationToGo != "") {
- if(locationToGo.find("gemini://") == -1) {
+ if(locationToGo.find(":") == -1) { // TODO: Is this how we which scheme was specified?
locationToGo = "gemini://" + locationToGo;
}
b.goToSite(locationToGo, true);
@@ -374,6 +376,13 @@ int main(int argc, char** argv) {
y = std::max(0,std::min(y,lowestPos(current)));
ds.y = y;
ds.strLs = current;
+ auto* clk = b.getCurrentLink();
+ if(clk != nullptr) {
+ ds.header = clk->getLinkDestination().to_string();
+ } else {
+ ds.header = "Welcome!";
+ }
+
draw(ds);
refresh();
diff --git a/src/preformatted.cpp b/src/preformatted.cpp
@@ -0,0 +1,19 @@
+#include "../include/preformatted.hpp"
+#include <ncurses.h>
+
+Preformatted::Preformatted(std::string input) {
+ text = input;
+}
+
+std::string Preformatted::textToDraw() {
+ return text + "\n";
+}
+
+int Preformatted::getColor() {
+ return COLOR_MAGENTA;
+}
+
+LineType Preformatted::type() {
+ return PLAINTEXT;
+}
+
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -1,7 +1,10 @@
#include "../include/utils.hpp"
+#include "../include/heading.hpp"
#include "../include/link.hpp"
+#include "../include/format-switch.hpp"
#include "../include/errors.hpp"
#include "../include/plaintext.hpp"
+#include "../include/preformatted.hpp"
#include <cstddef>
#include <cstdint>
#include <cstdlib>
@@ -75,12 +78,28 @@ std::vector<std::string> stringToList(std::string input)
return res;
}
-Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount) {
- if(input.substr(0,2) == "=>") {
- Link* ln = new Link{input, prior, linkCount};
- return ln;
+Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount, bool isPreformatted) {
+
+ if(input.substr(0,3) == "```") {
+ FormatSwitch* fs = new FormatSwitch{input};
+ return fs;
+ }
+
+ if(!isPreformatted) {
+ if(input.substr(0,2) == "=>") {
+ Link* ln = new Link{input, prior, linkCount};
+ return ln;
+ }
+ if(input.substr(0,1) == "#") {
+ Heading* hd = new Heading{input};
+ return hd;
+ return new Plaintext{input};
+ }
+ } else {
+ return new Preformatted{input};
}
return new Plaintext{input};
+
}