commit 26f53ef4b78028f5f0f807a4686532edef733896
parent 573311708e8cb19e0d6b02b733fcd23e229d6be8
Author: Andrew Laack <andrew@laack.co>
Date: Tue, 22 Sep 2026 23:40:54 -0500
Added code to do line splitting for rendering
Diffstat:
5 files changed, 119 insertions(+), 2 deletions(-)
diff --git a/README b/README
@@ -4,7 +4,7 @@ A gemini browser, written in C++.
Requirements
============
-C++ compiler, make
+C++ compiler, make, openssl
Installation
diff --git a/include/utils.hpp b/include/utils.hpp
@@ -14,3 +14,4 @@ Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount);
std::string urlEncode(const std::string& value);
std::string getNewTab();
void openUrl(const std::string& url);
+std::vector<std::pair<std::string, int>> breakLines(std::vector<std::pair<std::string, int>>& strLs, int width);
diff --git a/src/main.cpp b/src/main.cpp
@@ -13,6 +13,7 @@
// we do this because this is built against ncurses, would be nice to do away w/ this
// bc ppl use lots of emojis on gemini sites.
+
void removeNonAscii(std::vector<std::pair<std::string, int>>& strLs) {
for(std::size_t i = 0; i < strLs.size(); ++i) {
@@ -52,7 +53,6 @@ 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("-");
@@ -261,6 +261,8 @@ int main(int argc, char** argv) {
auto current = b.renderSite();
removeNonAscii(current);
+ current = breakLines(current,COLS);
+
int input = 0;
int y = 0;
@@ -288,12 +290,16 @@ int main(int argc, char** argv) {
current = b.renderSite();
removeNonAscii(current);
+ current = breakLines(current,COLS);
+
} else if(input == 'b') {
b.goBack();
current = b.renderSite();
removeNonAscii(current);
+ current = breakLines(current,COLS);
+
} else if(input == 'o') {
std::string locationToGo = openPageHandler(ds);
@@ -319,6 +325,7 @@ int main(int argc, char** argv) {
b.goToSite(inputQuery,true);
current = b.renderSite();
removeNonAscii(current);
+ current = breakLines(current,COLS);
}
} else if (b.getCurrentSite()->getStatusCode() >= 30 && b.getCurrentSite()->getStatusCode() <= 39){
@@ -332,6 +339,7 @@ int main(int argc, char** argv) {
} else {
current = b.renderSite();
removeNonAscii(current);
+ current = breakLines(current,COLS);
}
y = std::max(0,y);
diff --git a/src/utils.cpp b/src/utils.cpp
@@ -8,9 +8,11 @@
#include <fstream>
#include <iomanip>
#include <ios>
+#include <iostream>
#include <linux/limits.h>
#include <sstream>
#include <string>
+#include <utility>
#include <vector>
#include <spawn.h>
#include <sys/wait.h>
@@ -117,3 +119,46 @@ std::string getNewTab() {
;
return st;
}
+
+// WIDTH IS INCLUSIVE
+std::vector<std::pair<std::string, int>> breakLines(std::vector<std::pair<std::string, int>>& strLs, int width) {
+
+ std::vector<std::pair<std::string, int>> res {};
+
+ for(std::size_t i = 0; i < strLs.size(); ++i) {
+
+ std::string cstr = strLs[i].first;
+ std::string current = "";
+ int lastSpace = -1;
+
+ for(int x = 0; x < cstr.size(); ++x) {
+ if(current.size() < width) {
+ current.push_back(cstr[x]);
+ if(cstr[x] == ' ') {
+ lastSpace = current.size()-1;
+ }
+ } else {
+
+ std::string toPush = current;
+
+ if(lastSpace != -1) {
+ toPush = current.substr(0,lastSpace+1);
+ current = current.substr(lastSpace+1);
+ current.push_back(cstr[x]);
+ } else {
+ current = "";
+ current.push_back(cstr[x]);
+ }
+
+ res.push_back(std::pair<std::string,int> {toPush,strLs[i].second});
+ lastSpace = (cstr[x] == ' ') ? current.size() - 1 : -1;
+ }
+ }
+ if(current.size() > 0) {
+ res.push_back(std::pair<std::string,int> {current,strLs[i].second});
+ current = "";
+ }
+ }
+
+ return res;
+}
diff --git a/tests/browser-test.cpp b/tests/browser-test.cpp
@@ -1,4 +1,8 @@
#include <catch2/catch_test_macros.hpp>
+#include <cstdlib>
+#include <ctime>
+#include <iostream>
+#include <utility>
#include "../include/browser.hpp"
#include "../include/utils.hpp"
@@ -23,3 +27,62 @@ TEST_CASE( "Local filesystem relative navigation" ) {
}
+TEST_CASE("Test trivial line breaking") {
+ std::vector<std::pair<std::string, int>> strLs {};
+ for(int i = 0; i < 10; ++i) {
+ std::pair<std::string, int> current {"this is a simple test line", 10};
+ strLs.push_back(current);
+ }
+ auto result = breakLines(strLs, 10);
+ REQUIRE(result.size() == 30);
+}
+
+static std::string charset = "abc defghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
+
+TEST_CASE("Test width invariant") {
+
+ std::vector<std::pair<std::string, int>> strLs {};
+
+ srand(std::time(NULL));
+
+ for(int i = 0; i < 10000; ++i) {
+ std::string strRnd;
+ int ub = rand() % 10000;
+ for(int x = 0; x < ub; ++x) {
+ strRnd += charset[rand() % charset.length()];
+ }
+ std::pair<std::string, int> current {strRnd, 10};
+ strLs.push_back(current);
+ }
+
+ auto result = breakLines(strLs, 80);
+
+ for(auto& res: result) {
+ REQUIRE(res.first.size() <= 80);
+ }
+
+}
+
+TEST_CASE("Test lots of spaces") {
+
+ std::vector<std::pair<std::string, int>> strLs {};
+
+ srand(std::time(NULL));
+
+ for(int i = 0; i < 10; ++i) {
+ std::string strRnd;
+ int ub = rand() % 10000;
+ for(int x = 0; x < ub; ++x) {
+ strRnd += ' ';
+ }
+ std::pair<std::string, int> current {strRnd, 10};
+ strLs.push_back(current);
+ }
+
+ auto result = breakLines(strLs, 80);
+
+ for(auto& res: result) {
+ REQUIRE(res.first.size() <= 80);
+ }
+
+}