foreseen

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

commit 40c2b73fd7d6548c6de95ccece41cdecfb447179
parent f106f75aa53c7ed41fed7e595fdfc7196b823209
Author: Andrew Laack <andrew@laack.co>
Date:   Tue, 22 Sep 2026 19:59:28 -0500

Reuse box logic and make it look nicer

Diffstat:
Msrc/main.cpp | 91++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
1 file changed, 58 insertions(+), 33 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp @@ -27,6 +27,8 @@ void removeNonAscii(std::vector<std::pair<std::string, int>>& strLs) { return; } + + void initColors() { for (int c = 0; c < COLORS; ++c) { init_pair(c + 1, c, -1); @@ -44,52 +46,70 @@ struct DrawState { std::string userInput; }; +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 textSize = text.size(); + int userInputSize = userInput.size(); + int width = COLS/2; + + 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()); + +} + void draw(DrawState ds) { move(0,0); clear(); + for(int i = ds.y;i-ds.y+1 < LINES && i < ds.strLs.size(); ++i) { + move(i - ds.y, 0); + attron(COLOR_PAIR(ds.strLs[i].second + 1)); + addstr(ds.strLs[i].first.c_str()); + attroff(COLOR_PAIR(ds.strLs[i].second + 1)); + } + if (ds.handleRedirect) { - addstr("Follow redirect (y/n): "); - addstr(ds.redirInput.c_str()); - } else { - for(int i = ds.y;i-ds.y+1 < LINES && i < ds.strLs.size(); ++i) { - move(i - ds.y, 0); - attron(COLOR_PAIR(ds.strLs[i].second + 1)); - addstr(ds.strLs[i].first.c_str()); - attroff(COLOR_PAIR(ds.strLs[i].second + 1)); - } + drawInputBox("Follow redirect (y/n): ", ds.redirInput); } if(ds.handleInput) { - move(LINES/2-1, COLS/4); - for(int i = 0; i < COLS/2; ++i) { - addstr("-"); - } - move(LINES/2, COLS/4); - addstr("input: "); - addstr(ds.userInput.c_str()); - - move(LINES/2 + 1, COLS/4); - for(int i = 0; i < COLS/2; ++i) { - addstr("-"); - } + drawInputBox("input: ", ds.userInput); } if (ds.handleOpenOther) { - move(LINES/2-1, COLS/4); - for(int i = 0; i < COLS/2; ++i) { - addstr("-"); - } - move(LINES/2, COLS/4); - addstr("Destination / Link Number: "); - addstr(ds.openOtherInput.c_str()); - - move(LINES/2 + 1, COLS/4); - for(int i = 0; i < COLS/2; ++i) { - addstr("-"); - } + drawInputBox("Destination / Link Number: ", ds.openOtherInput); } refresh(); @@ -183,6 +203,11 @@ std::string handleUserInput(DrawState ds) { 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);