1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/*
* Ricochet - A minimalist CLI web browser for fast, distraction-free navigation.
* Copyright (C) 2026 Richard Qin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ricochet/core/browser.hh"
#include "ricochet/net/http_client.hh"
#include "ricochet/parser/html_lexer.hh"
#include "ricochet/parser/tree_builder.hh"
#include "ricochet/render/renderer.hh"
#include "ricochet/tui/terminal.hh"
#include <cstddef>
#include <exception>
#include <iostream>
#include <ranges>
namespace ricochet::core {
struct PageData
{
std::vector<std::string> lines;
std::vector<std::string> links;
std::vector<std::string> inputs;
std::string title;
};
namespace {
std::string extract_title( const parser::DomNode& root_node )
{
std::vector<const parser::DomNode*> stack;
stack.push_back( &root_node );
while ( !stack.empty() ) {
const parser::DomNode* current = stack.back();
stack.pop_back();
std::string base_tag = current->tag_name;
const std::size_t space_pos = base_tag.find( ' ' );
if ( space_pos != std::string::npos ) {
base_tag = base_tag.substr( 0, space_pos );
}
for ( char& c : base_tag ) {
if ( c >= 'A' && c <= 'Z' ) {
c = static_cast<char>( c + 32 );
}
}
if ( base_tag == "title" ) {
std::string full_title;
for ( const auto& child : current->children ) {
if ( child.tag_name.empty() ) {
full_title += child.text_content;
}
}
if ( !full_title.empty() ) {
return full_title;
}
}
for ( const auto& child : std::ranges::reverse_view( current->children ) ) {
stack.push_back( &child );
}
}
return "";
}
PageData load_page( std::string_view url )
{
const net::HttpClient client;
auto response_result = client.fetch( url );
if ( !response_result.has_value() ) {
return {
.lines = { "[!] Failed to load: " + std::string( url ) }, .links = {}, .inputs = {}, .title = "Error" };
}
const parser::HtmlLexer lexer;
const parser::TreeBuilder builder;
const render::Renderer renderer;
const auto dom_root = builder.build( lexer.tokenize( response_result.value().body ) );
const render::RenderResult result = renderer.render( dom_root );
std::vector<std::string> lines;
std::size_t start = 0;
while ( start < result.text.length() ) {
const std::size_t end = result.text.find( '\n', start );
if ( end == std::string::npos ) {
lines.push_back( result.text.substr( start ) );
break;
}
lines.push_back( result.text.substr( start, end - start ) );
start = end + 1;
}
const std::string title = extract_title( dom_root );
return { .lines = lines, .links = result.links, .inputs = result.inputs, .title = title };
}
void draw_view( const tui::Terminal& terminal,
const std::vector<std::string>& lines,
std::size_t scroll_y,
std::string_view url, // NOLINT(bugprone-easily-swappable-parameters)
std::string_view title ) // NOLINT(bugprone-easily-swappable-parameters)
{
terminal.clear_screen();
auto [width, height] = terminal.get_size();
const std::size_t usable_height = ( height > 2 ) ? ( height - 2 ) : 1;
std::string clean_title( title );
if ( clean_title.empty() ) {
const std::size_t start = url.find( "://" );
const std::size_t host_start = ( start == std::string::npos ) ? 0 : start + 3;
const std::size_t host_end = url.find( '/', host_start );
clean_title = std::string( url.substr( host_start, host_end - host_start ) );
}
for ( char& c : clean_title ) {
if ( c == '\n' || c == '\r' ) {
c = ' ';
}
}
std::string header = " PAGE: " + clean_title;
if ( header.size() < width ) {
header.append( width - header.size(), ' ' );
} else if ( header.size() > width && width > 3 ) {
header = header.substr( 0, width - 3 ) + "...";
}
std::cout << "\033[7m" << header << "\033[0m\r\n";
for ( std::size_t i = 0; i < usable_height; ++i ) {
if ( scroll_y + i < lines.size() ) {
std::cout << lines[scroll_y + i] << "\r\n";
} else {
std::cout << "~\r\n";
}
}
std::string footer
= " URL: " + std::string( url ) + " | [j/k]Scroll [/]Jump [f]Follow [i]Input [H/L]History [q]Quit ";
if ( footer.size() < width ) {
footer.append( width - footer.size(), ' ' );
} else if ( footer.size() > width && width > 3 ) {
footer = footer.substr( 0, width - 3 ) + "...";
}
std::cout << "\033[7m" << footer << "\033[0m";
std::cout.flush();
}
std::string format_url( std::string input )
{
if ( input.empty() ) {
return input;
}
if ( input.starts_with( "http://" ) || input.starts_with( "https://" ) ) {
return input;
}
const bool is_domain = input.contains( '.' ) && !input.contains( ' ' );
if ( is_domain ) {
input.insert( 0, "https://" );
return input;
}
for ( char& ch : input ) {
if ( ch == ' ' ) {
ch = '+';
}
}
return "https://lite.duckduckgo.com/lite/?q=" + input;
}
std::string get_url_input( const tui::Terminal& terminal )
{
auto [width, height] = terminal.get_size();
terminal.move_cursor( height, 1 );
std::cout << "\x1b[2K\033[7m URL: \033[0m ";
std::cout.flush();
std::string input;
terminal.show_cursor( true );
while ( true ) {
const char ch = terminal.read_key();
if ( ch == '\r' || ch == '\n' ) {
break;
}
if ( ch == 27 ) {
input.clear();
break;
}
if ( ch == 127 || ch == 8 ) {
if ( !input.empty() ) {
input.pop_back();
std::cout << "\b \b";
std::cout.flush();
}
} else if ( std::isprint( static_cast<unsigned char>( ch ) ) ) {
input += ch;
std::cout << ch;
std::cout.flush();
}
}
terminal.show_cursor( false );
return format_url( std::move( input ) );
}
std::string get_link_input( const tui::Terminal& terminal )
{
auto [width, height] = terminal.get_size();
terminal.move_cursor( height, 1 );
std::cout << "\x1b[2K\033[7m Follow Link [#]: \033[0m ";
std::cout.flush();
std::string num_input;
terminal.show_cursor( true );
while ( true ) {
const char ch = terminal.read_key();
if ( ch == '\r' || ch == '\n' ) {
break;
}
if ( ch == 27 ) {
num_input.clear();
break;
}
if ( ch == 127 || ch == 8 ) {
if ( !num_input.empty() ) {
num_input.pop_back();
std::cout << "\b \b";
std::cout.flush();
}
} else if ( std::isdigit( static_cast<unsigned char>( ch ) ) ) {
num_input += ch;
std::cout << ch;
std::cout.flush();
}
}
terminal.show_cursor( false );
return num_input;
}
std::string normalize_url( const std::string& target, const std::string& current_url )
{
if ( target.starts_with( "/" ) ) {
if ( target.starts_with( "//" ) ) {
return "https:" + target;
}
const std::size_t slash_pos = current_url.find( '/', 8 );
const std::string domain
= ( slash_pos == std::string::npos ) ? current_url : current_url.substr( 0, slash_pos );
return domain + target;
}
if ( !target.starts_with( "http" ) ) {
const std::size_t last_slash = current_url.rfind( '/' );
if ( last_slash > 7 ) {
return current_url.substr( 0, last_slash + 1 ) + target;
}
return current_url + "/" + target;
}
return target;
}
std::string strip_ddg_tracker( std::string target )
{
const std::size_t uddg_pos = target.find( "uddg=" );
if ( uddg_pos != std::string::npos ) {
const std::size_t end_pos = target.find( '&', uddg_pos );
const std::string encoded = target.substr( uddg_pos + 5, end_pos - ( uddg_pos + 5 ) );
std::string decoded;
for ( std::size_t i = 0; i < encoded.length(); ++i ) {
if ( encoded[i] == '%' && i + 2 < encoded.length() ) {
const std::string hex = encoded.substr( i + 1, 2 );
try {
decoded += static_cast<char>( std::stoi( hex, nullptr, 16 ) );
} catch ( const std::exception& ) {
(void)0;
}
i += 2;
} else {
decoded += encoded[i];
}
}
return decoded;
}
return target;
}
void handle_follow_link( const tui::Terminal& terminal,
const std::vector<std::string>& links,
std::string& current_url,
bool& navigate )
{
const std::string num_input = get_link_input( terminal );
if ( !num_input.empty() ) {
try {
const std::size_t index = std::stoul( num_input );
if ( index > 0 && index <= links.size() ) {
std::string target = links[index - 1];
target = normalize_url( target, current_url );
target = strip_ddg_tracker( target );
current_url = target;
navigate = true;
}
} catch ( const std::exception& e ) {
(void)e; // NOLINT
}
}
}
enum class InputAction : std::uint8_t
{
None,
Quit,
Navigate
};
std::string get_terminal_input( const tui::Terminal& terminal, const std::string& prompt )
{
auto [width, height] = terminal.get_size();
terminal.move_cursor( height, 1 );
std::cout << "\x1b[2K\033[7m " << prompt << " \033[0m ";
std::cout.flush();
std::string input;
terminal.show_cursor( true );
while ( true ) {
const char ch = terminal.read_key();
if ( ch == '\r' || ch == '\n' ) {
break;
}
if ( ch == 27 ) {
input.clear();
break;
}
if ( ch == 127 || ch == 8 ) {
if ( !input.empty() ) {
input.pop_back();
std::cout << "\b \b";
std::cout.flush();
}
} else if ( std::isprint( static_cast<unsigned char>( ch ) ) ) {
input += ch;
std::cout << ch;
std::cout.flush();
}
}
terminal.show_cursor( false );
return input;
}
InputAction handle_form_input( const tui::Terminal& terminal,
const PageData& page_data,
std::string& current_url,
std::vector<std::string>& history,
std::size_t& history_idx )
{
const std::string num_input = get_terminal_input( terminal, "Select Input [#]:" );
if ( num_input.empty() ) {
return InputAction::None;
}
std::size_t index = 0;
try {
index = std::stoul( num_input );
} catch ( ... ) {
(void)0;
}
if ( index == 0 || index > page_data.inputs.size() ) {
return InputAction::None;
}
const std::string query = get_terminal_input( terminal, "Enter Text [I" + std::to_string( index ) + "]:" );
if ( query.empty() ) {
return InputAction::None;
}
std::string target_url;
std::string encoded_query = query;
for ( char& ch : encoded_query ) {
if ( ch == ' ' ) {
ch = '+';
}
}
if ( current_url.contains( "wikipedia.org" ) ) {
target_url = "https://en.wikipedia.org/w/index.php?search=" + encoded_query;
} else {
target_url = "https://lite.duckduckgo.com/lite/?q=" + encoded_query;
}
history.resize( history_idx + 1 );
history.push_back( target_url );
history_idx++;
return InputAction::Navigate;
}
InputAction process_key( char c,
const tui::Terminal& terminal,
const PageData& page_data,
std::string& current_url,
std::vector<std::string>& history,
std::size_t& history_idx, // NOLINT(bugprone-easily-swappable-parameters)
std::size_t& scroll_y ) // NOLINT(bugprone-easily-swappable-parameters)
{
auto [width, height] = terminal.get_size();
const std::size_t view_h = ( height > 1 ) ? ( height - 1 ) : 1;
if ( c == 'q' ) {
return InputAction::Quit;
}
if ( c == 'j' && scroll_y + view_h < page_data.lines.size() ) {
scroll_y++;
}
if ( c == 'k' && scroll_y > 0 ) {
scroll_y--;
}
if ( c == 'H' && history_idx > 0 ) {
history_idx--;
return InputAction::Navigate;
}
if ( c == 'L' && history_idx + 1 < history.size() ) {
history_idx++;
return InputAction::Navigate;
}
if ( c == '/' ) {
const std::string next_url = get_url_input( terminal );
if ( !next_url.empty() ) {
history.resize( history_idx + 1 );
history.push_back( next_url );
history_idx++;
return InputAction::Navigate;
}
}
if ( c == 'f' ) {
const std::string old_url = current_url;
bool nav = false;
handle_follow_link( terminal, page_data.links, current_url, nav );
if ( nav && current_url != old_url ) {
history.resize( history_idx + 1 );
history.push_back( current_url );
history_idx++;
return InputAction::Navigate;
}
}
if ( c == 'i' && !page_data.inputs.empty() ) {
return handle_form_input( terminal, page_data, current_url, history, history_idx );
}
return InputAction::None;
}
std::vector<std::string> wrap_lines( const std::vector<std::string>& original_lines, std::size_t wrap_width )
{
std::vector<std::string> wrapped_lines;
for ( const auto& line : original_lines ) {
std::string current_line;
std::size_t vis_count = 0;
bool in_ansi = false;
for ( const char c : line ) {
if ( c == '\x1b' ) {
in_ansi = true;
}
if ( in_ansi ) {
current_line += c;
if ( c == 'm' ) {
in_ansi = false;
}
} else {
if ( vis_count >= wrap_width ) {
wrapped_lines.push_back( current_line + "\033[0m" );
current_line.clear();
vis_count = 0;
}
current_line += c;
vis_count++;
}
}
if ( !current_line.empty() || line.empty() ) {
wrapped_lines.push_back( current_line + "\033[0m" );
}
}
return wrapped_lines;
}
} // namespace
int Browser::run( std::string_view initial_url )
{
std::vector<std::string> history;
history.emplace_back( initial_url );
std::size_t history_idx = 0;
const tui::Terminal terminal;
terminal.show_cursor( false );
while ( true ) {
std::string current_url = history[history_idx];
terminal.clear_screen();
std::cout << "-> Fetching: " << current_url << "...\r\n";
std::cout.flush();
auto page_data = load_page( current_url );
auto [term_width, term_height] = terminal.get_size();
const std::size_t wrap_width = ( term_width > 1 ) ? ( term_width - 1 ) : 80;
page_data.lines = wrap_lines( page_data.lines, wrap_width );
std::size_t scroll_y = 0;
bool navigate = false;
while ( !navigate ) {
draw_view( terminal, page_data.lines, scroll_y, current_url, page_data.title );
const char c = terminal.read_key();
const InputAction action = process_key( c, terminal, page_data, current_url, history, history_idx, scroll_y );
if ( action == InputAction::Quit ) {
terminal.clear_screen();
return 0;
}
if ( action == InputAction::Navigate ) {
navigate = true;
}
}
}
terminal.clear_screen();
return 0;
}
} // namespace ricochet::core
|