Skip to content

Commit

Permalink
Canvas customization: add colorised ratio column to custom columns (See
Browse files Browse the repository at this point in the history
  • Loading branch information
chros authored and chros committed Apr 1, 2018
1 parent c776e8d commit 050ab16
Showing 1 changed file with 53 additions and 21 deletions.
74 changes: 53 additions & 21 deletions patches/ui_pyroscope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern std::string get_active_tracker_domain(torrent::Download* item);


#define TRACKER_LABEL_WIDTH 20U
#define YING_YANG_STEPS 11

// definition from display/window_download_list.cc that is not in the header file
typedef std::pair<core::View::iterator, core::View::iterator> Range;
Expand All @@ -67,6 +68,14 @@ int ratio_col[] = {
ps::COL_PROGRESS100, ps::COL_PROGRESS120,
};

// ying-yang ratio indicators
static const char* ying_yang[4][YING_YANG_STEPS] = {
{},
{"☹ ", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", ""},
};

// basic color names
static const char* color_names[] = {
"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"
Expand Down Expand Up @@ -549,12 +558,13 @@ bool ui_pyroscope_download_list_redraw(Window* window, display::Canvas* canvas,
const torrent::Object::map_type& column_defs = control->object_storage()->get_str("ui.column.render").as_map();
// x_base value depends on the static headers below! (x_base = 1 + number of chars in header)
// int pos = 1, x_base = 31, column = x_base;
int pos = 1, x_base = 18, column = x_base;
// int pos = 1, x_base = 18, column = x_base;
int pos = 1, x_base = 16, column = x_base;

// clear all column positions before they will be updated (columns can be removed)
column_pos.clear();
// canvas->print(2, pos, " ⣿ ☯ ⌚ ≀∆ ⌚ ≀∇ ");
canvas->print(2, pos, " ⌬ ≀∆ ⌬ ≀∇ ");
canvas->print(2, pos, " ⣿ ⌬ ≀∆ ⌬ ≀∇ ");
column += render_columns(true, rpc::make_target(), canvas, column, pos, column_defs);
canvas->print(column, pos, " Name "); column += 6;
if (canvas->width() - column > TRACKER_LABEL_WIDTH) {
Expand Down Expand Up @@ -583,14 +593,6 @@ bool ui_pyroscope_download_list_redraw(Window* window, display::Canvas* canvas,
{"", "", "", "", "", "", "", "", ""},
};
unsigned int progress_style = std::min<unsigned int>(rpc::call_command_value("ui.style.progress"), 2);
#define YING_YANG_STEPS 11
const char* ying_yang[4][YING_YANG_STEPS] = {
{},
{"☹ ", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", ""},
{"", "", "", "", "", "", "", "", "", "", ""},
};
unsigned int ying_yang_style = std::min<unsigned int>(rpc::call_command_value("ui.style.ratio"), 3);

// define iterator range
Range range = rak::advance_bidirectional(
Expand Down Expand Up @@ -672,19 +674,15 @@ bool ui_pyroscope_download_list_redraw(Window* window, display::Canvas* canvas,
print_download_title(buffer, last, d);

char progress_str[6] = "##";
char ying_yang_str[6] = "##";
if (progress_style == 0) {
sprintf(progress_str, item->file_list()->completed_chunks() ? "%2.2d" : "--",
item->file_list()->completed_chunks() * 100 / item->file_list()->size_chunks());
}
if (ying_yang_style == 0 && ratio < 9949) {
sprintf(ying_yang_str, ratio ? "%2.2d" : "--", ratio / 100);
}

// canvas->print(0, pos, "%s %s%s%s%s%s%s%s%s%s%s%s %s %s %s %s %s%s %s%s%s %s%s %s%s%s",
// canvas->print(0, pos, "%s %s%s%s%s%s%s %s %s %s %s%s %s%s ",
// canvas->print(0, pos, "%s %s%s%s%s %s %s %s %s%s %s%s ",
canvas->print(0, pos, "%s %s%s%s%s %s%s ",
canvas->print(0, pos, "%s %s%s%s %s%s ",
range.first == view->focus() ? "»" : " ",
// item->is_open() ? item->is_active() ? "▹ " : "╍ " : "▪ ",
// rpc::call_command_string("d.tied_to_file", rpc::make_target(d)).empty() ? "  " : "⚯ ",
Expand All @@ -699,8 +697,8 @@ bool ui_pyroscope_download_list_redraw(Window* window, display::Canvas* canvas,
// D_INFO(item)->down_rate()->rate() ?
// (D_INFO(item)->up_rate()->rate() ? "⇅ " : "↡ ") :
// (D_INFO(item)->up_rate()->rate() ? "↟ " : "  "),
ying_yang_style == 0 ? ying_yang_str :
ratio >= YING_YANG_STEPS * 1000 ? "" : ying_yang[ying_yang_style][ratio / 1000],
// ying_yang_style == 0 ? ying_yang_str :
// ratio >= YING_YANG_STEPS * 1000 ? "⊛ " : ying_yang[ying_yang_style][ratio / 1000],
// has_msg ? has_alert ? alert : "♺ " : is_tagged ? "⚑ " : "  ",
// tracker ? num2(tracker->scrape_downloaded()).c_str() : "  ",
// tracker ? num2(tracker->scrape_complete()).c_str() : "  ",
Expand Down Expand Up @@ -747,7 +745,7 @@ bool ui_pyroscope_download_list_redraw(Window* window, display::Canvas* canvas,
canvas->width() - x_name - 1).c_str());

// int x_scrape = 3 + 11*2 + 1; // lead, 11 status columns, gap
int x_scrape = 3 + 2*2 + 1; // lead, 2 status columns, gap
int x_scrape = 3 + 1*2 + 1; // lead, 1 status columns, gap
// int x_rate = x_scrape + 4*3; // skip 4 scrape columns
int x_rate = x_scrape; // skip 0 scrape columns
//// int x_name = x_rate + 2*5 + 4 + 6 + 4; // skip 4 rate/size columns, gaps
Expand All @@ -763,9 +761,11 @@ bool ui_pyroscope_download_list_redraw(Window* window, display::Canvas* canvas,
int pcol = ratio_color(item->file_list()->completed_chunks() * 1000 / item->file_list()->size_chunks());
canvas->set_attr(x_scrape-9, pos, 2, attr_map[pcol + offset], pcol + offset);

// show ratio progress by color
int rcol = ratio_color(ratio);
canvas->set_attr(x_scrape-5, pos, 2, attr_map[rcol + offset], rcol + offset);
// show ratio progress by color if ratio column exists
if (column_pos.find("") != column_pos.end()) {
int rcol = ratio_color(ratio);
canvas->set_attr(column_pos[""], pos, 2, attr_map[rcol + offset], rcol + offset);
}

// color up rates / time
if (!up_rate) {
Expand Down Expand Up @@ -866,6 +866,29 @@ std::string get_ui_message(core::Download* item) {
}


// return the colorized icon of ratio of the given download item
std::string get_ui_ratio(core::Download* item) {
std::string ratio_str = " ";
int ratio = rpc::call_command_value("d.ratio", rpc::make_target(item));
unsigned int ying_yang_style = std::min<unsigned int>(rpc::call_command_value("ui.style.ratio"), 3);

if (ying_yang_style == 0) {
char ying_yang_chr[6] = "##";

if (ratio < 9949)
sprintf(ying_yang_chr, ratio ? "%2.2d" : "--", ratio / 100);

ratio_str = std::string(ying_yang_chr);
} else if (ratio >= YING_YANG_STEPS * 1000) {
ratio_str = "";
} else {
ratio_str = std::string(ying_yang[ying_yang_style][ratio / 1000]);
}

return ratio_str;
}


} // namespace


Expand Down Expand Up @@ -1013,6 +1036,11 @@ torrent::Object cmd_d_ui_message(core::Download* download) {
}


torrent::Object cmd_d_ui_ratio(core::Download* download) {
return display::get_ui_ratio(download);
}


// register our commands
void initialize_command_ui_pyroscope() {
#define PS_VARIABLE_COLOR(key, value) \
Expand Down Expand Up @@ -1069,6 +1097,7 @@ void initialize_command_ui_pyroscope() {
CMD2_ANY_LIST("convert.magnitude", _cxxstd_::bind(&apply_magnitude, _cxxstd_::placeholders::_2));

CMD2_DL("d.ui.message", _cxxstd_::bind(&cmd_d_ui_message, _cxxstd_::placeholders::_1));
CMD2_DL("d.ui.ratio", _cxxstd_::bind(&cmd_d_ui_ratio, _cxxstd_::placeholders::_1));

rpc::parse_command_multiple(
rpc::make_target(),
Expand All @@ -1092,6 +1121,9 @@ void initialize_command_ui_pyroscope() {
// Transfer direction (⋮)
"method.set_key = ui.column.render, \"190:1:⋮\", ((if, ((d.down.rate)), ((if,((d.up.rate)),((cat, \"⇅ \")),((cat, \"↡ \")))), ((if,((d.up.rate)),((cat, \"↟ \")),((cat, \"\")))) ))\n"

// Ratio (☯)
"method.set_key = ui.column.render, \"200:2:☯ \", ((d.ui.ratio))\n"

// Message (⚑)
"method.set_key = ui.column.render, \"210:2:⚑ \", ((d.ui.message))\n"

Expand Down

0 comments on commit 050ab16

Please sign in to comment.