1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
/** \file cmdline_download_progress_display.cc */
// Copyright (C) 2010-2011 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// Local includes:
#include "cmdline_download_progress_display.h"
#include "terminal.h"
#include "transient_message.h"
#include <aptitude.h>
#include <generic/views/download_progress.h>
// System includes:
#include <apt-pkg/strutl.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/format.hpp>
#include <boost/make_shared.hpp>
#include <cwidget/generic/util/transcode.h>
#include <math.h>
using boost::algorithm::join;
using boost::format;
using boost::make_shared;
using boost::shared_ptr;
using boost::wformat;
using cwidget::util::transcode;
namespace aptitude
{
namespace cmdline
{
namespace
{
class download_progress : public views::download_progress
{
bool display_messages;
shared_ptr<transient_message> message;
shared_ptr<download_status_display> status_display;
shared_ptr<terminal_input> term_input;
download_progress(bool _display_messages,
const shared_ptr<transient_message> &_message,
const shared_ptr<download_status_display> &_status_display,
const shared_ptr<terminal_input> &_term_input);
friend shared_ptr<download_progress>
make_shared<download_progress>(const bool &,
const shared_ptr<transient_message> &,
const shared_ptr<download_status_display> &,
const shared_ptr<terminal_input> &);
public:
bool update_progress(const status ¤t_status);
void file_started(const std::string &description,
const boost::optional<unsigned long> &id,
const boost::optional<unsigned long long> &file_size);
void file_already_downloaded(const std::string &description,
const boost::optional<unsigned long> &id,
const boost::optional<unsigned long long> &file_size);
void error(bool ignored,
const std::string &error,
const std::string &description,
const boost::optional<unsigned long> &id);
void file_finished(const std::string &description,
const boost::optional<unsigned long> &id);
void done(double fetched_bytes,
unsigned long elapsed_time,
double latest_download_rate);
void media_change(const std::string &media,
const std::string &drive,
const sigc::slot1<void, bool> &k);
virtual void complete(double fetched_bytes,
unsigned long elapsed_time,
double latest_download_rate);
};
download_progress::download_progress(bool _display_messages,
const shared_ptr<transient_message> &_message,
const shared_ptr<download_status_display> &_status_display,
const shared_ptr<terminal_input> &_term_input)
: display_messages(_display_messages),
message(_message),
status_display(_status_display),
term_input(_term_input)
{
}
bool download_progress::update_progress(const status ¤t_status)
{
status_display->display_status(current_status);
return true;
}
void download_progress::file_started(const std::string &description,
const boost::optional<unsigned long> &id,
const boost::optional<unsigned long long> &file_size)
{
if(display_messages)
{
std::vector<std::string> entries;
entries.push_back(_("Get:"));
if(id)
entries.push_back( (format("%d") % *id).str() );
if(!description.empty())
entries.push_back(description);
if(file_size)
entries.push_back( (format("[%sB]") % SizeToStr(*file_size)).str() );
message->display_and_advance(transcode(join(entries, " ")));
}
}
void download_progress::file_already_downloaded(const std::string &description,
const boost::optional<unsigned long> &id,
const boost::optional<unsigned long long> &file_size)
{
if(display_messages)
{
std::vector<std::string> entries;
entries.push_back(_("Hit"));
if(id)
entries.push_back( (format("%d") % *id).str() );
if(!description.empty())
entries.push_back(description);
if(file_size)
entries.push_back( (format("[%sB]") % SizeToStr(*file_size)).str() );
message->display_and_advance(transcode(join(entries, " ")));
}
}
void download_progress::error(bool ignored,
const std::string &error,
const std::string &description,
const boost::optional<unsigned long> &id)
{
if(display_messages)
{
std::vector<std::string> entries;
if(ignored)
// ForTranslators: this stands for "ignored" and should be
// the same width as the translation of "Err".
entries.push_back(_("Ign"));
else
// ForTranslators: this stands for "error" and should be the
// same width as the translation of "Ign".
entries.push_back(_("Err"));
if(!description.empty())
entries.push_back(description);
message->display_and_advance(transcode(join(entries, " ")));
if(!ignored && !error.empty())
message->display_and_advance(transcode(" " + error));
}
}
void download_progress::file_finished(const std::string &description,
const boost::optional<unsigned long> &id)
{
}
void download_progress::done(double fetched_bytes,
unsigned long elapsed_time,
double latest_download_rate)
{
if(display_messages)
{
if(fetched_bytes != 0)
{
std::string text =
(format(_("Fetched %sB in %s (%sB/s)"))
% SizeToStr(fetched_bytes)
% TimeToStr(elapsed_time)
% SizeToStr(latest_download_rate)).str();
message->display_and_advance(transcode(text));
}
}
}
void download_progress::media_change(const std::string &media,
const std::string &drive,
const sigc::slot1<void, bool> &k)
{
// Clear any existing text to ensure that the prompt starts at
// the beginning of the line.
message->set_text(L"");
std::string prompt =
(format(_("Media change: Please insert the disc labeled '%s' into "
"the drive '%s' and press [Enter]."))
% media % drive).str();
// Note that the value the user enters is discarded.
try
{
term_input->prompt_for_input(transcode(prompt));
}
catch(StdinEOFException)
{
k(false);
return;
}
// Now say it's OK to continue.
k(true);
}
void download_progress::complete(double fetched_bytes,
unsigned long elapsed_time,
double latest_download_rate)
{
}
class dummy_status_display : public download_status_display
{
dummy_status_display();
friend shared_ptr<dummy_status_display>
make_shared<dummy_status_display>();
public:
void display_status(const download_progress::status &status);
};
dummy_status_display::dummy_status_display()
{
}
void dummy_status_display::display_status(const download_progress::status &)
{
}
class download_status_display_impl : public download_status_display
{
shared_ptr<transient_message> message;
shared_ptr<terminal_locale> term_locale;
shared_ptr<terminal_metrics> term_metrics;
download_status_display_impl(const shared_ptr<transient_message> &_message,
const shared_ptr<terminal_locale> &_term_locale,
const shared_ptr<terminal_metrics> &_term_metrics);
friend shared_ptr<download_status_display_impl>
make_shared<download_status_display_impl>(const shared_ptr<transient_message> &,
const shared_ptr<terminal_locale> &,
const shared_ptr<terminal_metrics> &);
public:
void display_status(const download_progress::status &status);
};
download_status_display_impl::download_status_display_impl(const shared_ptr<transient_message> &_message,
const shared_ptr<terminal_locale> &_term_locale,
const shared_ptr<terminal_metrics> &_term_metrics)
: message(_message),
term_locale(_term_locale),
term_metrics(_term_metrics)
{
}
// \todo This should be generic code:
int as_percent(double fraction)
{
const int result = static_cast<int>(round(fraction * 100));
if(result < 0)
return 0;
else if(result > 100)
return 100;
else
return result;
}
/** \brief Visitor for worker status objects that appends their
* rendering to a string.
*
* Not responsible for bracketing the rendering in [].
*/
class append_worker_status : public boost::static_visitor<>
{
std::wstring &output;
typedef views::download_progress::file_progress file_progress;
public:
append_worker_status(std::wstring &_output)
: output(_output)
{
}
void operator()(const std::string &progress) const
{
output += transcode(progress);
}
void operator()(const file_progress &progress) const
{
const unsigned long current_size = progress.get_current_size();
const unsigned long total_size = progress.get_total_size();
const bool complete = progress.get_complete();
const std::string &description = progress.get_description();
const boost::optional<unsigned long> &id = progress.get_id();
const std::string &mode = progress.get_mode();
std::vector<std::wstring> components;
if(id)
components.push_back((wformat(L"%lu") % *id).str());
if(!description.empty())
components.push_back(transcode(description));
if(!mode.empty())
components.push_back(transcode(mode));
if(total_size != 0 && !complete)
components.push_back((wformat(L"%lu/%sB %lu%%")
% current_size
% transcode(SizeToStr(total_size))
% as_percent( ((double) current_size) / total_size)).str());
else if(current_size != 0)
// The old download indicator displayed a size of 0 if
// current_size was 0. I figure if we have no total size
// and we haven't downloaded anything, there's no poing.
components.push_back((wformat(L"%lu") % current_size).str());
output += join(components, L" ");
}
};
/** \brief Find the index in the given string of a display
* column.
*
* \param s The string to process.
*
* \param target_column The column to locate, where the first
* character of s starts at column 0.
*
* \param term_locale Locale information that should be used when
* determining the column.
*
* \param result_index Set to the index of the first character
* which is partly or wholly at or beyond
* the given display column. If target_column
* is negative, this will always be 0.
*
* \param result_column Set to the first column in which the
* character indicated by result_index
* appears.
*
* \todo Could move to a common module and get a unit test.
*/
void find_column_index(const std::wstring &s,
int target_column,
const shared_ptr<terminal_locale> &term_locale,
int &result_index,
int &result_column)
{
result_index = 0;
result_column = 0;
std::wstring::const_iterator it = s.begin();
while(it != s.end() && result_column < target_column)
{
const int curr_width = term_locale->wcwidth(*it);
// Don't include this character if it starts before the
// target column and extends past it. Be careful not to
// return a character that ends just before the target
// column (>= vs >).
if(result_column + curr_width > target_column)
break;
++it;
++result_index;
result_column += curr_width;
}
}
void download_status_display_impl::display_status(const download_progress::status &status)
{
typedef views::download_progress::status::worker_status worker_status;
const double download_rate = status.get_download_rate();
const std::vector<worker_status> &active_downloads =
status.get_active_downloads();
const double fraction_complete = status.get_fraction_complete();
const unsigned long time_remaining = status.get_time_remaining();
const int percent = as_percent(fraction_complete);
std::wstring message_text;
message_text += (wformat(L"%d%% ") % percent).str();
if(active_downloads.empty())
message_text += transcode(_("[Working]"));
else
{
for(std::vector<worker_status>::const_iterator it =
active_downloads.begin(); it != active_downloads.end(); ++it)
{
if(it != active_downloads.begin())
message_text.push_back(L' ');
message_text.push_back(L'[');
boost::apply_visitor(append_worker_status(message_text), *it);
message_text.push_back(L']');
}
}
if(download_rate > 0 || time_remaining > 0)
{
std::wstring progress_str;
if(download_rate > 0)
progress_str = (wformat(L" %sB/s %s")
% transcode(SizeToStr(download_rate))
% transcode(TimeToStr(time_remaining))).str();
else
progress_str = (wformat(L" %s")
% transcode(TimeToStr(time_remaining))).str();
int progress_str_width = 0;
for(std::wstring::const_iterator it = progress_str.begin();
it != progress_str.end(); ++it)
progress_str_width += term_locale->wcwidth(*it);
int message_text_width = 0;
for(std::wstring::const_iterator it = message_text.begin();
it != message_text.end(); ++it)
message_text_width += term_locale->wcwidth(*it);
const int screen_width = term_metrics->get_screen_width();
// Format the progress string so that it overlaps the
// previous message text and is right-justified.
int progress_str_start_column = screen_width - progress_str_width;
// First, pad the message text with spaces so that there's
// a place to insert the progress string.
while(message_text_width < screen_width - progress_str_start_column)
{
message_text.push_back(L' ');
message_text_width += term_locale->wcwidth(L' ');
}
// Now, find the location in the message text where the
// progress string will begin and drop the rest of the
// string.
int first_overwritten_character_index;
int first_overwritten_character_column;
find_column_index(message_text,
progress_str_start_column,
term_locale,
first_overwritten_character_index,
first_overwritten_character_column);
// Drop the part of the string that's overwritten;
// everything from first_overwritten_character_index
// onwards.
message_text.erase(first_overwritten_character_index);
// We might have split a wide character; replace its first
// portion with spaces.
while(first_overwritten_character_column < progress_str_start_column)
{
message_text.push_back(L' ');
first_overwritten_character_column += term_locale->wcwidth(L' ');
}
// OK, now message_text is exactly long enough to
// concatenate it with the progress string. The progress
// string might running off the right side of the terminal
// if it replaced the whole message text and was still too
// long; I leave it to the transient message to deal with
// that case.
message_text += progress_str;
}
message->set_text(message_text);
}
}
download_status_display::~download_status_display()
{
}
shared_ptr<views::download_progress>
create_download_progress_display(const boost::shared_ptr<transient_message> &message,
const boost::shared_ptr<download_status_display> &status_display,
const boost::shared_ptr<terminal_input> &term_input,
bool display_messages)
{
return make_shared<download_progress>(display_messages,
message,
status_display,
term_input);
}
shared_ptr<download_status_display>
create_cmdline_download_status_display(const shared_ptr<transient_message> &message,
const shared_ptr<terminal_locale> &term_locale,
const shared_ptr<terminal_metrics> &term_metrics,
bool hide_status)
{
if(hide_status)
return make_shared<dummy_status_display>();
else
return make_shared<download_status_display_impl>(message,
term_locale,
term_metrics);
}
}
}
|