Skip to content

Commit 5e32b4c

Browse files
committed
main: implement a fancy session loading progress bar
1 parent 3dae421 commit 5e32b4c

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/main.cc

+33-29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "buildinfo.h"
77

88
#include <cstdint>
9+
#include <cstdio>
910
#include <cstdlib>
1011
#include <inttypes.h>
1112
#include <iostream>
@@ -43,6 +44,7 @@
4344
#include "rpc/command_scheduler_item.h"
4445
#include "rpc/parse_commands.h"
4546
#include "utils/directory.h"
47+
#include "utils/indicators.h"
4648

4749
#include "command_helpers.h"
4850
#include "control.h"
@@ -123,36 +125,25 @@ parse_options(int argc,
123125
}
124126
}
125127

126-
static unsigned long progress_count = 0;
127-
static unsigned long progress_total = 0;
128-
static uint8_t progress_printed = 0;
129-
130-
static void
131-
print_progress() {
132-
if (progress_total != 0) {
133-
++progress_count;
134-
if (progress_count < progress_total) {
135-
double percentage = static_cast<double>(progress_count) / progress_total;
136-
if (percentage >= 0.1 && progress_printed < percentage * 10) {
137-
std::cout << "rTorrent: " << progress_count << " torrents ("
138-
<< (int)(percentage * 100) << "%) loaded" << std::endl;
139-
progress_printed += 2;
140-
}
141-
}
142-
}
143-
}
144-
145128
void
146-
load_session_torrents() {
129+
load_session_torrents(indicators::BlockProgressBar*& progress_bar) {
147130
utils::Directory entries =
148131
control->core()->download_store()->get_formated_entries();
149132

150-
if (!display::Canvas::isInitialized() && entries.size()) {
151-
std::cout << "rTorrent: loading " << entries.size()
133+
const auto entries_size = entries.size();
134+
135+
if (!display::Canvas::isInitialized() && entries_size) {
136+
std::cout << "rTorrent: loading " << entries_size
152137
<< " entries from session directory" << std::endl;
153-
progress_count = 0;
154-
progress_total = entries.size();
155-
progress_printed = 0;
138+
if (isatty(fileno(stdin)) && isatty(fileno(stdout))) {
139+
using namespace indicators;
140+
progress_bar = new BlockProgressBar{
141+
option::BarWidth{ 50 },
142+
option::ForegroundColor{ Color::white },
143+
option::FontStyles{ std::vector<FontStyle>{ FontStyle::bold } },
144+
option::MaxProgress{ entries_size }
145+
};
146+
}
156147
}
157148

158149
for (utils::Directory::const_iterator first = entries.begin(),
@@ -163,16 +154,23 @@ load_session_torrents() {
163154
// would be overwritten anyway on exit, and thus not really be
164155
// useful.
165156
if (!first->is_file()) {
166-
print_progress();
157+
if (progress_bar != nullptr) {
158+
progress_bar->tick();
159+
}
167160
continue;
168161
}
169162

170163
core::DownloadFactory* f = new core::DownloadFactory(control->core());
171164

172165
// Replace with session torrent flag.
173166
f->set_session(true);
174-
f->slot_finished([f]() {
175-
print_progress();
167+
f->slot_finished([f, &progress_bar, entries_size]() {
168+
if (progress_bar != nullptr) {
169+
progress_bar->tick();
170+
progress_bar->set_option(indicators::option::PostfixText{
171+
std::to_string(progress_bar->current()) + "/" +
172+
std::to_string(entries_size) });
173+
}
176174
delete f;
177175
});
178176
f->load(entries.path() + first->d_name);
@@ -611,7 +609,8 @@ main(int argc, char** argv) {
611609
// Load session torrents and perform scheduled tasks to ensure
612610
// session torrents are loaded before arg torrents.
613611
control->dht_manager()->load_dht_cache();
614-
load_session_torrents();
612+
indicators::BlockProgressBar* progress_bar = nullptr;
613+
load_session_torrents(progress_bar);
615614
torrent::utils::priority_queue_perform(&taskScheduler, cachedTime);
616615

617616
load_arg_torrents(argv + firstArg, argv + argc);
@@ -629,6 +628,11 @@ main(int argc, char** argv) {
629628
"startup_done",
630629
"System startup_done event action failed: ");
631630

631+
if (progress_bar != nullptr) {
632+
progress_bar->mark_as_completed();
633+
delete progress_bar;
634+
}
635+
632636
if (!display::Canvas::isInitialized()) {
633637
std::cout << "rTorrent: started, "
634638
<< control->core()->download_list()->size()

0 commit comments

Comments
 (0)