From 0221f6e8a6e0f5ec7a9e34f21870ded0f67a2cc4 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sun, 25 Feb 2018 11:01:17 +0100 Subject: [PATCH] client_test fixes --- examples/client_test.cpp | 15 +++++++++--- examples/print.cpp | 31 +++++++++++++----------- examples/print.hpp | 2 +- examples/torrent_view.cpp | 51 ++++++++++++++++++++++++++++++--------- examples/torrent_view.hpp | 6 +++-- 5 files changed, 73 insertions(+), 32 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index e6655209b..671416a7c 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -881,6 +881,10 @@ bool handle_alert(torrent_view& view, session_view& ses_view view.update_torrents(std::move(p->status)); return true; } + else if (torrent_removed_alert* p = alert_cast(a)) + { + view.remove_torrent(std::move(p->handle)); + } return false; #ifdef _MSC_VER @@ -1298,9 +1302,14 @@ MAGNETURL is a magnet link int terminal_width = 80; int terminal_height = 50; - terminal_size(&terminal_width, &terminal_height); - view.set_size(terminal_width, terminal_height / 3); - ses_view.set_pos(terminal_height / 3); + std::tie(terminal_width, terminal_height) = terminal_size(); + + // the ratio of torrent-list and details below depend on the number of + // torrents we have in the session + int const height = std::min(terminal_height / 2 + , std::max(5, view.num_visible_torrents() + 2)); + view.set_size(terminal_width, height); + ses_view.set_pos(height); int c = 0; if (sleep_and_input(&c, refresh_delay)) diff --git a/examples/print.cpp b/examples/print.cpp index f7ec21e2c..7858f45a0 100644 --- a/examples/print.cpp +++ b/examples/print.cpp @@ -340,42 +340,45 @@ void clear_rows(int y1, int y2) #endif } -void terminal_size(int* terminal_width, int* terminal_height) +std::pair terminal_size() { + int width = 80; + int height = 50; #ifdef _WIN32 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO coninfo; if (GetConsoleScreenBufferInfo(out, &coninfo)) { - *terminal_width = coninfo.dwSize.X; - *terminal_height = coninfo.srWindow.Bottom - coninfo.srWindow.Top; + width = coninfo.dwSize.X; + height = coninfo.srWindow.Bottom - coninfo.srWindow.Top; #else int tty = open("/dev/tty", O_RDONLY); if (tty < 0) { - *terminal_width = 190; - *terminal_height = 100; - return; + width = 190; + height = 100; + return {width, height}; } winsize size; int ret = ioctl(tty, TIOCGWINSZ, reinterpret_cast(&size)); close(tty); if (ret == 0) { - *terminal_width = size.ws_col; - *terminal_height = size.ws_row; + width = size.ws_col; + height = size.ws_row; #endif - if (*terminal_width < 64) - *terminal_width = 64; - if (*terminal_height < 25) - *terminal_height = 25; + if (width < 64) + width = 64; + if (height < 25) + height = 25; } else { - *terminal_width = 190; - *terminal_height = 100; + width = 190; + height = 100; } + return {width, height}; } #ifdef _WIN32 diff --git a/examples/print.hpp b/examples/print.hpp index a2104a7e6..352e3124d 100644 --- a/examples/print.hpp +++ b/examples/print.hpp @@ -44,7 +44,7 @@ void clear_screen(); void clear_rows(int y1, int y2); -void terminal_size(int* terminal_width, int* terminal_height); +std::pair terminal_size(); std::string piece_matrix(lt::bitfield const& p, int width, int* height); void print(char const* str); diff --git a/examples/torrent_view.cpp b/examples/torrent_view.cpp index a29ad7f4c..5b6cc5e91 100644 --- a/examples/torrent_view.cpp +++ b/examples/torrent_view.cpp @@ -144,31 +144,51 @@ lt::torrent_handle torrent_view::get_active_handle() const return m_filtered_handles[m_active_torrent]->handle; } +void torrent_view::remove_torrent(lt::torrent_handle h) +{ + auto i = m_all_handles.find(h); + if (i == m_all_handles.end()) return; + bool need_rerender = false; + if (show_torrent(i->second)) + { + auto j = std::find(m_filtered_handles.begin(), m_filtered_handles.end() + , &i->second); + if (j != m_filtered_handles.end()) + { + m_filtered_handles.erase(j); + need_rerender = true; + } + } + m_all_handles.erase(i); + if (need_rerender) render(); +} + void torrent_view::update_torrents(std::vector st) { std::set updates; bool need_filter_update = false; for (lt::torrent_status& t : st) { - auto j = m_all_handles.find(t); + auto j = m_all_handles.find(t.handle); // add new entries here if (j == m_all_handles.end()) { - j = m_all_handles.insert(std::move(t)).first; - if (show_torrent(*j)) + auto handle = t.handle; + j = m_all_handles.emplace(handle, std::move(t)).first; + if (show_torrent(j->second)) { - m_filtered_handles.push_back(&*j); + m_filtered_handles.push_back(&j->second); need_filter_update = true; } } else { - bool const prev_show = show_torrent(*j); - const_cast(*j) = std::move(t); - if (prev_show != show_torrent(*j)) + bool const prev_show = show_torrent(j->second); + j->second = std::move(t); + if (prev_show != show_torrent(j->second)) need_filter_update = true; else - updates.insert(j->handle); + updates.insert(j->second.handle); } } if (need_filter_update) @@ -180,11 +200,12 @@ void torrent_view::update_torrents(std::vector st) { int torrent_index = 0; for (auto i = m_filtered_handles.begin(); - i != m_filtered_handles.end(); ++torrent_index, ++i) + i != m_filtered_handles.end(); ++i) { if (torrent_index < m_scroll_position || torrent_index >= m_scroll_position + m_height - header_size) { + ++torrent_index; continue; } @@ -194,10 +215,14 @@ void torrent_view::update_torrents(std::vector st) continue; if (updates.count(s.handle) == 0) + { + ++torrent_index; continue; + } set_cursor_pos(0, header_size + torrent_index - m_scroll_position); print_torrent(s, torrent_index == m_active_torrent); + ++torrent_index; } } } @@ -265,11 +290,12 @@ void torrent_view::render() int torrent_index = 0; for (std::vector::iterator i = m_filtered_handles.begin(); - i != m_filtered_handles.end(); ++torrent_index) + i != m_filtered_handles.end();) { if (torrent_index < m_scroll_position) { ++i; + ++torrent_index; continue; } if (lines_printed >= m_height) @@ -286,6 +312,7 @@ void torrent_view::render() set_cursor_pos(0, torrent_index + header_size - m_scroll_position); print_torrent(s, torrent_index == m_active_torrent); ++lines_printed; + ++torrent_index; } clear_rows(torrent_index + header_size, m_height); @@ -419,8 +446,8 @@ void torrent_view::update_filtered_torrents() m_filtered_handles.clear(); for (auto const& h : m_all_handles) { - if (!show_torrent(h)) continue; - m_filtered_handles.push_back(&h); + if (!show_torrent(h.second)) continue; + m_filtered_handles.push_back(&h.second); } if (m_active_torrent >= int(m_filtered_handles.size())) m_active_torrent = int(m_filtered_handles.size()) - 1; if (m_active_torrent < 0) m_active_torrent = 0; diff --git a/examples/torrent_view.hpp b/examples/torrent_view.hpp index 14a60242e..f1248acd5 100644 --- a/examples/torrent_view.hpp +++ b/examples/torrent_view.hpp @@ -35,7 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include +#include #include "libtorrent/fwd.hpp" #include "libtorrent/torrent_status.hpp" @@ -67,7 +67,9 @@ struct torrent_view lt::torrent_status const& get_active_torrent() const; lt::torrent_handle get_active_handle() const; + void remove_torrent(lt::torrent_handle st); void update_torrents(std::vector st); + int num_visible_torrents() const { return int(m_filtered_handles.size()); } int height() const; @@ -93,7 +95,7 @@ private: void update_filtered_torrents(); // all torrents - std::unordered_set m_all_handles; + std::unordered_map m_all_handles; // pointers into m_all_handles of the remaining torrents after filtering std::vector m_filtered_handles;