@@ -34,7 +34,8 @@ bool DownloadProgress::Connect(const std::string& host, int port) {
3434 return true ;
3535}
3636
37- bool DownloadProgress::Handle (const DownloadType& event_type) {
37+ bool DownloadProgress::Handle (
38+ const std::unordered_set<DownloadType>& event_type) {
3839 assert (!!ws_);
3940#if defined(_WIN32)
4041 HANDLE h_out = GetStdHandle (STD_OUTPUT_HANDLE);
@@ -50,10 +51,14 @@ bool DownloadProgress::Handle(const DownloadType& event_type) {
5051 }
5152 }
5253#endif
53- status_ = DownloadStatus::DownloadStarted;
54+ for (auto et : event_type) {
55+ status_[et] = DownloadStatus::DownloadStarted;
56+ }
5457 std::unique_ptr<indicators::DynamicProgress<indicators::ProgressBar>> bars;
5558
56- std::vector<std::unique_ptr<indicators::ProgressBar>> items;
59+ std::unordered_map<std::string,
60+ std::pair<int , std::unique_ptr<indicators::ProgressBar>>>
61+ items;
5762 indicators::show_console_cursor (false );
5863 auto start = std::chrono::steady_clock::now ();
5964 auto handle_message = [this , &bars, &items, event_type,
@@ -78,22 +83,28 @@ bool DownloadProgress::Handle(const DownloadType& event_type) {
7883 auto ev = cortex::event::GetDownloadEventFromJson (
7984 json_helper::ParseJsonString (message));
8085 // Ignore other task type
81- if (ev.download_task_ .type != event_type) {
86+ if (event_type. find ( ev.download_task_ .type ) == event_type. end () ) {
8287 return ;
8388 }
8489 auto now = std::chrono::steady_clock::now ();
8590 if (!bars) {
8691 bars = std::make_unique<
8792 indicators::DynamicProgress<indicators::ProgressBar>>();
88- for (auto & i : ev.download_task_ .items ) {
89- items.emplace_back (std::make_unique<indicators::ProgressBar>(
90- indicators::option::BarWidth{50 }, indicators::option::Start{" [" },
91- indicators::option::Fill{" =" }, indicators::option::Lead{" >" },
92- indicators::option::End{" ]" },
93- indicators::option::PrefixText{pad_string (Repo2Engine (i.id ))},
94- indicators::option::ForegroundColor{indicators::Color::white},
95- indicators::option::ShowRemainingTime{false }));
96- bars->push_back (*(items.back ()));
93+ }
94+ for (auto & i : ev.download_task_ .items ) {
95+ if (items.find (i.id ) == items.end ()) {
96+ auto idx = items.size ();
97+ items[i.id ] = std::pair (
98+ idx,
99+ std::make_unique<indicators::ProgressBar>(
100+ indicators::option::BarWidth{50 },
101+ indicators::option::Start{" [" }, indicators::option::Fill{" =" },
102+ indicators::option::Lead{" >" }, indicators::option::End{" ]" },
103+ indicators::option::PrefixText{pad_string (Repo2Engine (i.id ))},
104+ indicators::option::ForegroundColor{indicators::Color::white},
105+ indicators::option::ShowRemainingTime{false }));
106+
107+ bars->push_back (*(items.at (i.id ).second ));
97108 }
98109 }
99110 for (int i = 0 ; i < ev.download_task_ .items .size (); i++) {
@@ -113,32 +124,36 @@ bool DownloadProgress::Handle(const DownloadType& event_type) {
113124 (total - downloaded) / bytes_per_sec);
114125 }
115126
116- (*bars)[i].set_option (indicators::option::PrefixText{
117- pad_string (Repo2Engine (it.id )) +
118- std::to_string (int (static_cast <double >(downloaded) / total * 100 )) +
119- ' %' });
120- (*bars)[i].set_progress (
127+ (*bars)[items.at (it.id ).first ].set_option (
128+ indicators::option::PrefixText{
129+ pad_string (Repo2Engine (it.id )) +
130+ std::to_string (
131+ int (static_cast <double >(downloaded) / total * 100 )) +
132+ ' %' });
133+ (*bars)[items.at (it.id ).first ].set_progress (
121134 int (static_cast <double >(downloaded) / total * 100 ));
122- (*bars)[i].set_option (indicators::option::PostfixText{
123- time_remaining + " " +
124- format_utils::BytesToHumanReadable (downloaded) + " /" +
125- format_utils::BytesToHumanReadable (total)});
135+ (*bars)[items.at (it.id ).first ].set_option (
136+ indicators::option::PostfixText{
137+ time_remaining + " " +
138+ format_utils::BytesToHumanReadable (downloaded) + " /" +
139+ format_utils::BytesToHumanReadable (total)});
126140 } else if (ev.type_ == DownloadStatus::DownloadSuccess) {
127141 uint64_t total =
128142 it.bytes .value_or (std::numeric_limits<uint64_t >::max ());
129- (*bars)[i ].set_progress (100 );
143+ (*bars)[items. at (it. id ). first ].set_progress (100 );
130144 auto total_str = format_utils::BytesToHumanReadable (total);
131- (*bars)[i].set_option (indicators::option::PostfixText{
132- " 00m:00s " + total_str + " /" + total_str});
133- (*bars)[i].set_option (indicators::option::PrefixText{
134- pad_string (Repo2Engine (it.id )) + " 100%" });
135- (*bars)[i].set_progress (100 );
145+ (*bars)[items.at (it.id ).first ].set_option (
146+ indicators::option::PostfixText{" 00m:00s " + total_str + " /" +
147+ total_str});
148+ (*bars)[items.at (it.id ).first ].set_option (
149+ indicators::option::PrefixText{pad_string (Repo2Engine (it.id )) +
150+ " 100%" });
151+ (*bars)[items.at (it.id ).first ].set_progress (100 );
136152
137153 CTL_INF (" Download success" );
138154 }
155+ status_[ev.download_task_ .type ] = ev.type_ ;
139156 }
140-
141- status_ = ev.type_ ;
142157 };
143158
144159 while (ws_->getReadyState () != easywsclient::WebSocket::CLOSED &&
@@ -152,7 +167,9 @@ bool DownloadProgress::Handle(const DownloadType& event_type) {
152167 SetConsoleMode (h_out, dw_original_out_mode);
153168 }
154169#endif
155- if (status_ == DownloadStatus::DownloadError)
156- return false ;
170+ for (auto const & [_, v] : status_) {
171+ if (v == DownloadStatus::DownloadError)
172+ return false ;
173+ }
157174 return true ;
158175}
0 commit comments