added a piece bar to client_test

This commit is contained in:
Arvid Norberg 2007-07-09 00:00:35 +00:00
parent 4771f323f6
commit 1381a008c4
1 changed files with 48 additions and 1 deletions

View File

@ -103,6 +103,7 @@ void clear_home()
#include <termios.h>
#include <string.h>
#include <sys/ttycom.h>
#define ANSI_TERMINAL_COLORS
@ -243,6 +244,38 @@ std::string const& add_suffix(float val)
return ret;
}
std::string const& piece_bar(std::vector<bool> const& p, int width)
{
static const char* lookup[] =
{
// black, blue, cyan, white
"40", "44", "46", "47"
};
double piece_per_char = p.size() / double(width);
static std::string bar;
bar.clear();
bar.reserve(width * 6);
bar += "[";
// the [piece, piece + pieces_per_char) range is the pieces that are represented by each character
double piece = 0;
for (int i = 0; i < width; ++i, piece += piece_per_char)
{
int num_pieces = 0;
int num_have = 0;
int end = std::max(int(piece + piece_per_char), int(piece) + 1);
for (int k = int(piece); k < end; ++k, ++num_pieces)
if (p[k]) ++num_have;
int color = int(std::ceil(num_have / float(num_pieces) * (sizeof(lookup) / sizeof(lookup[0]) - 1)));
bar += esc(lookup[color]);
bar += " ";
}
bar += esc("0");
bar += "]";
return bar;
}
std::string const& progress_bar(float progress, int width, char const* code = "33")
{
static std::string bar;
@ -789,6 +822,7 @@ int main(int ac, char* av[])
bool print_peers = false;
bool print_log = false;
bool print_downloads = false;
bool print_piece_bar = false;
bool print_file_progress = false;
for (;;)
@ -845,8 +879,19 @@ int main(int ac, char* av[])
if (c == 'l') print_log = !print_log;
if (c == 'd') print_downloads = !print_downloads;
if (c == 'f') print_file_progress = !print_file_progress;
if (c == 'a') print_piece_bar = !print_piece_bar;
}
int terminal_width = 80;
#ifndef _WIN32
{
winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, (char*)&size);
terminal_width = size.ws_col;
}
#endif
// loop through the alert queue to see if anything has happened.
std::auto_ptr<alert> a;
a = ses.pop_alert();
@ -981,7 +1026,7 @@ int main(int ac, char* av[])
out.width(5);
out.fill(' ');
out << (s.progress*100) << "% ";
out << progress_bar(s.progress, 49, progress_bar_color);
out << progress_bar(s.progress, terminal_width - 63, progress_bar_color);
out << "\n";
out << " total downloaded: " << esc("32") << s.total_done << esc("0") << " Bytes ";
out << "peers: " << s.num_peers << " "
@ -1005,6 +1050,8 @@ int main(int ac, char* av[])
<< to_string(t.minutes(),2) << ":"
<< to_string(t.seconds(), 2) << esc("0") << " ";
out << "tracker: " << s.current_tracker << "\n";
if (print_piece_bar && s.progress < 1.f && s.pieces)
out << piece_bar(*s.pieces, terminal_width - 3) << "\n";
}
if (print_peers && !peers.empty())