From c6d7e532c4e010f55ab8c6b7cfd8b36d44779725 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Sun, 12 Apr 2026 04:22:01 +0800 Subject: feat: implement link extraction and interactive navigation via numbered tags in the browser --- src/core/browser.cc | 104 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 10 deletions(-) (limited to 'src/core') diff --git a/src/core/browser.cc b/src/core/browser.cc index 4225986..8bd921c 100644 --- a/src/core/browser.cc +++ b/src/core/browser.cc @@ -23,17 +23,24 @@ #include "ricochet/render/renderer.hh" #include "ricochet/tui/terminal.hh" #include +#include #include namespace ricochet::core { +struct PageData +{ + std::vector lines; + std::vector links; +}; + namespace { -std::vector load_page( std::string_view url ) +PageData load_page( std::string_view url ) { const net::HttpClient client; auto response_result = client.fetch( url ); if ( !response_result.has_value() ) { - return { "[!] Failed to load: " + std::string( url ) }; + return { .lines = { "[!] Failed to load: " + std::string( url ) }, .links = {} }; } const parser::HtmlLexer lexer; @@ -41,20 +48,20 @@ std::vector load_page( std::string_view url ) const render::Renderer renderer; const auto dom_root = builder.build( lexer.tokenize( response_result.value().body ) ); - const std::string text = renderer.render( dom_root ); + const render::RenderResult result = renderer.render( dom_root ); std::vector lines; std::size_t start = 0; - while ( start < text.length() ) { - const std::size_t end = text.find( '\n', start ); + while ( start < result.text.length() ) { + const std::size_t end = result.text.find( '\n', start ); if ( end == std::string::npos ) { - lines.push_back( text.substr( start ) ); + lines.push_back( result.text.substr( start ) ); break; } - lines.push_back( text.substr( start, end - start ) ); + lines.push_back( result.text.substr( start, end - start ) ); start = end + 1; } - return lines; + return { .lines = lines, .links = result.links }; } void draw_view( const tui::Terminal& terminal, @@ -73,7 +80,7 @@ void draw_view( const tui::Terminal& terminal, std::cout << "~\r\n"; } } - std::cout << "\033[7m URL: " << url << " | [j/k] Scroll [/] Jump [q] Quit \033[0m"; + std::cout << "\033[7m URL: " << url << " | [j/k] Scroll [/] Jump [f] Follow [q] Quit \033[0m"; std::cout.flush(); } @@ -115,6 +122,79 @@ std::string get_url_input( const tui::Terminal& terminal ) return 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( ch ) ) ) { + num_input += ch; + std::cout << ch; + std::cout.flush(); + } + } + terminal.show_cursor( false ); + return num_input; +} + +void handle_follow_link( const tui::Terminal& terminal, + const std::vector& 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]; + + if ( target.starts_with( "/" ) ) { + if ( target.starts_with( "//" ) ) { + target = "https:" + target; // "//example.com" + } else { + const std::size_t slash_pos = current_url.find( '/', 8 ); // 跳过 https:// + const std::string domain + = ( slash_pos == std::string::npos ) ? current_url : current_url.substr( 0, slash_pos ); + target = domain + target; // "/about" -> "https://gnu.org/about" + } + } else if ( !target.starts_with( "http" ) ) { + const std::size_t last_slash = current_url.rfind( '/' ); + if ( last_slash > 7 ) { + target = current_url.substr( 0, last_slash + 1 ) + target; + } else { + target = current_url + "/" + target; + } + } + + current_url = target; + navigate = true; + } + } catch ( const std::exception& e ) { + (void)e; // NOLINT + } + } +} + } // namespace int Browser::run( std::string_view initial_url ) @@ -126,7 +206,9 @@ int Browser::run( std::string_view initial_url ) terminal.clear_screen(); while ( !current_url.empty() ) { - auto lines = load_page( current_url ); + auto page_data = load_page( current_url ); + const auto& lines = page_data.lines; + const auto& links = page_data.links; std::size_t scroll_y = 0; bool navigate = false; @@ -151,6 +233,8 @@ int Browser::run( std::string_view initial_url ) current_url = std::move( next_url ); navigate = true; } + } else if ( c == 'f' ) { + handle_follow_link( terminal, links, current_url, navigate ); } } } -- cgit v1.2.3