diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-04-13 02:33:09 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-04-13 02:33:09 +0800 |
| commit | 24c36f5250e4c2b4232186ba3d17f949d70f5de2 (patch) | |
| tree | 7a6be8b1ae8985375484bb315561bf28c88b0984 /src/core/browser.cc | |
| parent | f640448109ca233d215ec1c07b795bd4313f428d (diff) | |
| download | rwebbrowser-24c36f5250e4c2b4232186ba3d17f949d70f5de2.tar.gz rwebbrowser-24c36f5250e4c2b4232186ba3d17f949d70f5de2.tar.xz | |
feat: improve error reporting, implement responsive footer layout, and refine HTTP response processing for non-text content types
Diffstat (limited to 'src/core/browser.cc')
| -rw-r--r-- | src/core/browser.cc | 49 |
1 files changed, 41 insertions, 8 deletions
diff --git a/src/core/browser.cc b/src/core/browser.cc index 57ed496..e9f1fce 100644 --- a/src/core/browser.cc +++ b/src/core/browser.cc @@ -191,7 +191,11 @@ PageData load_page( const HttpRequest& req ) const net::HttpClient client; auto response_result = client.fetch( req.url, req.method, req.body ); if ( !response_result.has_value() ) { - return { .lines = { "[!] Failed to load: " + req.url }, .links = {}, .inputs = {}, .title = "Error" }; + return { .lines + = { "\033[1;31m[!] Failed to load:\033[0m " + req.url, "", "Reason: " + response_result.error() }, + .links = {}, + .inputs = {}, + .title = "Error" }; } const auto& resp = response_result.value(); @@ -223,6 +227,41 @@ PageData load_page( const HttpRequest& req ) .lines = split_into_lines( result.text ), .links = result.links, .inputs = result.inputs, .title = title }; } +// --- 🔪 智能底部排版:优先截断 URL,誓死保卫快捷键 --- +std::string build_footer( std::string_view url, std::size_t width ) +{ + const std::string_view keys = " | [j/k]Scroll [/]Nav [f]Link [i]In [r]Ref [H/L]Hist [b/B]Mark [q]Quit "; + const std::string_view prefix = " URL: "; + + std::string footer; + if ( width <= keys.length() + prefix.length() + 3 ) { + footer.reserve( prefix.length() + url.length() + keys.length() ); + footer += prefix; + footer += url; + footer += keys; + if ( footer.length() > width && width > 3 ) { + footer.erase( width - 3 ); + footer += "..."; + } + } else { + const std::size_t max_url_len = width - keys.length() - prefix.length(); + footer.reserve( width ); + footer += prefix; + if ( url.length() > max_url_len ) { + footer += url.substr( 0, max_url_len - 3 ); + footer += "..."; + } else { + footer += url; + } + footer += keys; + } + + if ( footer.length() < width ) { + footer.append( width - footer.length(), ' ' ); + } + return footer; +} + void draw_view( const tui::Terminal& terminal, const std::vector<std::string>& lines, std::size_t scroll_y, @@ -262,13 +301,7 @@ void draw_view( const tui::Terminal& terminal, } } - std::string footer - = " URL: " + std::string( url ) + " | [j/k]Scroll [/]Jump [f]Follow [i]Input [b]Mark [B]Marks [q]Quit "; - if ( footer.size() < width ) { - footer.append( width - footer.size(), ' ' ); - } else if ( footer.size() > width && width > 3 ) { - footer = footer.substr( 0, width - 3 ) + "..."; - } + const std::string footer = build_footer( url, width ); std::cout << "\033[7m" << footer << "\033[0m"; std::cout.flush(); } |
