client_test fixes

This commit is contained in:
arvidn 2018-02-25 11:01:17 +01:00 committed by Arvid Norberg
parent 9696082355
commit 0221f6e8a6
5 changed files with 73 additions and 32 deletions

View File

@ -881,6 +881,10 @@ bool handle_alert(torrent_view& view, session_view& ses_view
view.update_torrents(std::move(p->status)); view.update_torrents(std::move(p->status));
return true; return true;
} }
else if (torrent_removed_alert* p = alert_cast<torrent_removed_alert>(a))
{
view.remove_torrent(std::move(p->handle));
}
return false; return false;
#ifdef _MSC_VER #ifdef _MSC_VER
@ -1298,9 +1302,14 @@ MAGNETURL is a magnet link
int terminal_width = 80; int terminal_width = 80;
int terminal_height = 50; int terminal_height = 50;
terminal_size(&terminal_width, &terminal_height); std::tie(terminal_width, terminal_height) = terminal_size();
view.set_size(terminal_width, terminal_height / 3);
ses_view.set_pos(terminal_height / 3); // 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; int c = 0;
if (sleep_and_input(&c, refresh_delay)) if (sleep_and_input(&c, refresh_delay))

View File

@ -340,42 +340,45 @@ void clear_rows(int y1, int y2)
#endif #endif
} }
void terminal_size(int* terminal_width, int* terminal_height) std::pair<int, int> terminal_size()
{ {
int width = 80;
int height = 50;
#ifdef _WIN32 #ifdef _WIN32
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO coninfo; CONSOLE_SCREEN_BUFFER_INFO coninfo;
if (GetConsoleScreenBufferInfo(out, &coninfo)) if (GetConsoleScreenBufferInfo(out, &coninfo))
{ {
*terminal_width = coninfo.dwSize.X; width = coninfo.dwSize.X;
*terminal_height = coninfo.srWindow.Bottom - coninfo.srWindow.Top; height = coninfo.srWindow.Bottom - coninfo.srWindow.Top;
#else #else
int tty = open("/dev/tty", O_RDONLY); int tty = open("/dev/tty", O_RDONLY);
if (tty < 0) if (tty < 0)
{ {
*terminal_width = 190; width = 190;
*terminal_height = 100; height = 100;
return; return {width, height};
} }
winsize size; winsize size;
int ret = ioctl(tty, TIOCGWINSZ, reinterpret_cast<char*>(&size)); int ret = ioctl(tty, TIOCGWINSZ, reinterpret_cast<char*>(&size));
close(tty); close(tty);
if (ret == 0) if (ret == 0)
{ {
*terminal_width = size.ws_col; width = size.ws_col;
*terminal_height = size.ws_row; height = size.ws_row;
#endif #endif
if (*terminal_width < 64) if (width < 64)
*terminal_width = 64; width = 64;
if (*terminal_height < 25) if (height < 25)
*terminal_height = 25; height = 25;
} }
else else
{ {
*terminal_width = 190; width = 190;
*terminal_height = 100; height = 100;
} }
return {width, height};
} }
#ifdef _WIN32 #ifdef _WIN32

View File

@ -44,7 +44,7 @@ void clear_screen();
void clear_rows(int y1, int y2); void clear_rows(int y1, int y2);
void terminal_size(int* terminal_width, int* terminal_height); std::pair<int, int> terminal_size();
std::string piece_matrix(lt::bitfield const& p, int width, int* height); std::string piece_matrix(lt::bitfield const& p, int width, int* height);
void print(char const* str); void print(char const* str);

View File

@ -144,31 +144,51 @@ lt::torrent_handle torrent_view::get_active_handle() const
return m_filtered_handles[m_active_torrent]->handle; 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<lt::torrent_status> st) void torrent_view::update_torrents(std::vector<lt::torrent_status> st)
{ {
std::set<lt::torrent_handle> updates; std::set<lt::torrent_handle> updates;
bool need_filter_update = false; bool need_filter_update = false;
for (lt::torrent_status& t : st) 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 // add new entries here
if (j == m_all_handles.end()) if (j == m_all_handles.end())
{ {
j = m_all_handles.insert(std::move(t)).first; auto handle = t.handle;
if (show_torrent(*j)) 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; need_filter_update = true;
} }
} }
else else
{ {
bool const prev_show = show_torrent(*j); bool const prev_show = show_torrent(j->second);
const_cast<lt::torrent_status&>(*j) = std::move(t); j->second = std::move(t);
if (prev_show != show_torrent(*j)) if (prev_show != show_torrent(j->second))
need_filter_update = true; need_filter_update = true;
else else
updates.insert(j->handle); updates.insert(j->second.handle);
} }
} }
if (need_filter_update) if (need_filter_update)
@ -180,11 +200,12 @@ void torrent_view::update_torrents(std::vector<lt::torrent_status> st)
{ {
int torrent_index = 0; int torrent_index = 0;
for (auto i = m_filtered_handles.begin(); 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 if (torrent_index < m_scroll_position
|| torrent_index >= m_scroll_position + m_height - header_size) || torrent_index >= m_scroll_position + m_height - header_size)
{ {
++torrent_index;
continue; continue;
} }
@ -194,10 +215,14 @@ void torrent_view::update_torrents(std::vector<lt::torrent_status> st)
continue; continue;
if (updates.count(s.handle) == 0) if (updates.count(s.handle) == 0)
{
++torrent_index;
continue; continue;
}
set_cursor_pos(0, header_size + torrent_index - m_scroll_position); set_cursor_pos(0, header_size + torrent_index - m_scroll_position);
print_torrent(s, torrent_index == m_active_torrent); print_torrent(s, torrent_index == m_active_torrent);
++torrent_index;
} }
} }
} }
@ -265,11 +290,12 @@ void torrent_view::render()
int torrent_index = 0; int torrent_index = 0;
for (std::vector<lt::torrent_status const*>::iterator i = m_filtered_handles.begin(); for (std::vector<lt::torrent_status const*>::iterator i = m_filtered_handles.begin();
i != m_filtered_handles.end(); ++torrent_index) i != m_filtered_handles.end();)
{ {
if (torrent_index < m_scroll_position) if (torrent_index < m_scroll_position)
{ {
++i; ++i;
++torrent_index;
continue; continue;
} }
if (lines_printed >= m_height) if (lines_printed >= m_height)
@ -286,6 +312,7 @@ void torrent_view::render()
set_cursor_pos(0, torrent_index + header_size - m_scroll_position); set_cursor_pos(0, torrent_index + header_size - m_scroll_position);
print_torrent(s, torrent_index == m_active_torrent); print_torrent(s, torrent_index == m_active_torrent);
++lines_printed; ++lines_printed;
++torrent_index;
} }
clear_rows(torrent_index + header_size, m_height); clear_rows(torrent_index + header_size, m_height);
@ -419,8 +446,8 @@ void torrent_view::update_filtered_torrents()
m_filtered_handles.clear(); m_filtered_handles.clear();
for (auto const& h : m_all_handles) for (auto const& h : m_all_handles)
{ {
if (!show_torrent(h)) continue; if (!show_torrent(h.second)) continue;
m_filtered_handles.push_back(&h); 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 >= int(m_filtered_handles.size())) m_active_torrent = int(m_filtered_handles.size()) - 1;
if (m_active_torrent < 0) m_active_torrent = 0; if (m_active_torrent < 0) m_active_torrent = 0;

View File

@ -35,7 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <set> #include <set>
#include <vector> #include <vector>
#include <unordered_set> #include <unordered_map>
#include "libtorrent/fwd.hpp" #include "libtorrent/fwd.hpp"
#include "libtorrent/torrent_status.hpp" #include "libtorrent/torrent_status.hpp"
@ -67,7 +67,9 @@ struct torrent_view
lt::torrent_status const& get_active_torrent() const; lt::torrent_status const& get_active_torrent() const;
lt::torrent_handle get_active_handle() const; lt::torrent_handle get_active_handle() const;
void remove_torrent(lt::torrent_handle st);
void update_torrents(std::vector<lt::torrent_status> st); void update_torrents(std::vector<lt::torrent_status> st);
int num_visible_torrents() const { return int(m_filtered_handles.size()); }
int height() const; int height() const;
@ -93,7 +95,7 @@ private:
void update_filtered_torrents(); void update_filtered_torrents();
// all torrents // all torrents
std::unordered_set<lt::torrent_status> m_all_handles; std::unordered_map<lt::torrent_handle, lt::torrent_status> m_all_handles;
// pointers into m_all_handles of the remaining torrents after filtering // pointers into m_all_handles of the remaining torrents after filtering
std::vector<lt::torrent_status const*> m_filtered_handles; std::vector<lt::torrent_status const*> m_filtered_handles;