6
6
#include " buildinfo.h"
7
7
8
8
#include < cstdint>
9
+ #include < cstdio>
9
10
#include < cstdlib>
10
11
#include < inttypes.h>
11
12
#include < iostream>
43
44
#include " rpc/command_scheduler_item.h"
44
45
#include " rpc/parse_commands.h"
45
46
#include " utils/directory.h"
47
+ #include " utils/indicators.h"
46
48
47
49
#include " command_helpers.h"
48
50
#include " control.h"
@@ -123,36 +125,25 @@ parse_options(int argc,
123
125
}
124
126
}
125
127
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
-
145
128
void
146
- load_session_torrents () {
129
+ load_session_torrents (indicators::BlockProgressBar*& progress_bar ) {
147
130
utils::Directory entries =
148
131
control->core ()->download_store ()->get_formated_entries ();
149
132
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
152
137
<< " 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
+ }
156
147
}
157
148
158
149
for (utils::Directory::const_iterator first = entries.begin (),
@@ -163,16 +154,23 @@ load_session_torrents() {
163
154
// would be overwritten anyway on exit, and thus not really be
164
155
// useful.
165
156
if (!first->is_file ()) {
166
- print_progress ();
157
+ if (progress_bar != nullptr ) {
158
+ progress_bar->tick ();
159
+ }
167
160
continue ;
168
161
}
169
162
170
163
core::DownloadFactory* f = new core::DownloadFactory (control->core ());
171
164
172
165
// Replace with session torrent flag.
173
166
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
+ }
176
174
delete f;
177
175
});
178
176
f->load (entries.path () + first->d_name );
@@ -611,7 +609,8 @@ main(int argc, char** argv) {
611
609
// Load session torrents and perform scheduled tasks to ensure
612
610
// session torrents are loaded before arg torrents.
613
611
control->dht_manager ()->load_dht_cache ();
614
- load_session_torrents ();
612
+ indicators::BlockProgressBar* progress_bar = nullptr ;
613
+ load_session_torrents (progress_bar);
615
614
torrent::utils::priority_queue_perform (&taskScheduler, cachedTime);
616
615
617
616
load_arg_torrents (argv + firstArg, argv + argc);
@@ -629,6 +628,11 @@ main(int argc, char** argv) {
629
628
" startup_done" ,
630
629
" System startup_done event action failed: " );
631
630
631
+ if (progress_bar != nullptr ) {
632
+ progress_bar->mark_as_completed ();
633
+ delete progress_bar;
634
+ }
635
+
632
636
if (!display::Canvas::isInitialized ()) {
633
637
std::cout << " rTorrent: started, "
634
638
<< control->core ()->download_list ()->size ()
0 commit comments