main.cpp (3595B)
1 #include <locale.h> 2 #include <ncurses.h> 3 #include <unistd.h> 4 5 #include <cassert> 6 #include <csignal> 7 #include <iostream> 8 #include <string> 9 10 #include "../include/browser.hpp" 11 #include "../include/render.hpp" 12 #include "../include/shared.hpp" 13 #include "../include/utils.hpp" 14 #include "../vendor/argparse.hpp" 15 16 void initColors() { 17 if (has_colors()) { 18 start_color(); 19 use_default_colors(); 20 for (int c = 0; c < COLORS && c + 1 < COLOR_PAIRS; ++c) { 21 init_pair(c + 1, c, -1); 22 } 23 } 24 } 25 26 int main(int argc, char** argv) { 27 argparse::ArgumentParser program("foreseen", "0.0.1"); 28 29 program.add_argument("-c", "--cache-pages") 30 .help("max number of pages for each cache (history, prefetch)") 31 .default_value(CACHE_SIZE) 32 .scan<'i', int>(); 33 34 program.add_argument("-p", "--prefetch-links") 35 .help( 36 "max number of links to prefetch per page (0 disables prefetching)") 37 .default_value(SITE_CACHE_LIMIT) 38 .scan<'i', int>(); 39 40 program.add_argument("-e", "--search-engine") 41 .help("default search engine") 42 .default_value(DEFAULT_SEARCH_ENGINE); 43 44 program.add_argument("destination") 45 .help("page to open") 46 .nargs(argparse::nargs_pattern::optional); 47 48 try { 49 program.parse_args(argc, argv); 50 } catch (const std::exception& err) { 51 std::cerr << err.what() << std::endl; 52 std::cerr << program; 53 std::exit(1); 54 } 55 56 SITE_CACHE_LIMIT = program.get<int>("--prefetch-links"); 57 CACHE_SIZE = program.get<int>("--cache-pages"); 58 DEFAULT_SEARCH_ENGINE = program.get<std::string>("--search-engine"); 59 60 if (DEFAULT_SEARCH_ENGINE.find("gemini://") != 0) { 61 std::cout << "Search engine must be prefixed with \"gemini://\": " + 62 DEFAULT_SEARCH_ENGINE 63 << std::endl; 64 return -1; 65 } 66 67 if (DEFAULT_SEARCH_ENGINE.substr(DEFAULT_SEARCH_ENGINE.size() - 1) != "?") { 68 DEFAULT_SEARCH_ENGINE += "?"; 69 } 70 71 std::string destination = 72 program.present<std::string>("destination").value_or(""); 73 74 std::signal(SIGPIPE, 75 SIG_IGN); // need this in case of swapping network connections 76 // bc that shouldn't kill the whole process. 77 78 Browser* bPtr = new Browser{}; 79 Browser& b = *bPtr; 80 81 DrawState ds{}; 82 ds.bPtr = bPtr; 83 84 setlocale(LC_CTYPE, ""); // emojis and such 85 86 if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) { 87 std::cout << "This program must be executed interactively" << std::endl; 88 return 1; 89 } 90 initscr(); 91 set_escdelay(25); 92 noecho(); // don't echo user inputs 93 cbreak(); // make C-c and C-z work 94 curs_set(0); // hide cursor 95 keypad(stdscr, TRUE); 96 97 initColors(); 98 99 Destination cliDestination = handleDestinationResolution(destination, true); 100 101 if (cliDestination.t != NO_DESTINATION) { 102 assert(cliDestination.t == 103 STRING_DESTINATION); // we don't allow numeric link following on 104 // startup. 105 106 // this guarantees invariants we expect about ds 107 // we could set cols and lines here, but it's better to do this. 108 109 mainLoop(ds, b, KEY_RESIZE, COLS, LINES); 110 tryVisitSite(ds, b, cliDestination.destination); 111 } 112 113 int input = 0; 114 115 while (true) { 116 bool continueExecution = mainLoop(ds, b, input, COLS, LINES); 117 if (!continueExecution) { 118 break; 119 } 120 input = getch(); 121 } 122 123 endwin(); 124 125 delete bPtr; 126 }