]>
git.saurik.com Git - apt-legacy.git/blob - cmdline/acqprogress.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acqprogress.cc,v 1.24 2003/04/27 01:56:48 doogie Exp $
4 /* ######################################################################
6 Acquire Progress - Command line progress meter
8 ##################################################################### */
10 // Include files /*{{{*/
11 #include "acqprogress.h"
12 #include <apt-pkg/acquire-item.h>
13 #include <apt-pkg/acquire-worker.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/error.h>
26 // AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
29 AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth
,unsigned int Quiet
) :
30 ScreenWidth(ScreenWidth
), Quiet(Quiet
)
34 // AcqTextStatus::Start - Downloading has started /*{{{*/
35 // ---------------------------------------------------------------------
37 void AcqTextStatus::Start()
39 pkgAcquireStatus::Start();
44 // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
45 // ---------------------------------------------------------------------
47 void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc
&Itm
)
53 cout
<< '\r' << BlankLine
<< '\r';
55 cout
<< _("Hit ") << Itm
.Description
;
56 if (Itm
.Owner
->FileSize
!= 0)
57 cout
<< " [" << SizeToStr(Itm
.Owner
->FileSize
) << "B]";
62 // AcqTextStatus::Fetch - An item has started to download /*{{{*/
63 // ---------------------------------------------------------------------
64 /* This prints out the short description and the expected size */
65 void AcqTextStatus::Fetch(pkgAcquire::ItemDesc
&Itm
)
68 if (Itm
.Owner
->Complete
== true)
77 cout
<< '\r' << BlankLine
<< '\r';
79 cout
<< _("Get:") << Itm
.Owner
->ID
<< ' ' << Itm
.Description
;
80 if (Itm
.Owner
->FileSize
!= 0)
81 cout
<< " [" << SizeToStr(Itm
.Owner
->FileSize
) << "B]";
85 // AcqTextStatus::Done - Completed a download /*{{{*/
86 // ---------------------------------------------------------------------
87 /* We don't display anything... */
88 void AcqTextStatus::Done(pkgAcquire::ItemDesc
&Itm
)
93 // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
94 // ---------------------------------------------------------------------
95 /* We print out the error text */
96 void AcqTextStatus::Fail(pkgAcquire::ItemDesc
&Itm
)
101 // Ignore certain kinds of transient failures (bad code)
102 if (Itm
.Owner
->Status
== pkgAcquire::Item::StatIdle
)
106 cout
<< '\r' << BlankLine
<< '\r';
108 if (Itm
.Owner
->Status
== pkgAcquire::Item::StatDone
)
110 cout
<< _("Ign ") << Itm
.Description
<< endl
;
114 cout
<< _("Err ") << Itm
.Description
<< endl
;
115 cout
<< " " << Itm
.Owner
->ErrorText
<< endl
;
121 // AcqTextStatus::Stop - Finished downloading /*{{{*/
122 // ---------------------------------------------------------------------
123 /* This prints out the bytes downloaded and the overall average line
125 void AcqTextStatus::Stop()
127 pkgAcquireStatus::Stop();
132 cout
<< '\r' << BlankLine
<< '\r' << flush
;
134 if (FetchedBytes
!= 0 && _error
->PendingError() == false)
135 ioprintf(cout
,_("Fetched %sB in %s (%sB/s)\n"),
136 SizeToStr(FetchedBytes
).c_str(),
137 TimeToStr(ElapsedTime
).c_str(),
138 SizeToStr(CurrentCPS
).c_str());
141 // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
142 // ---------------------------------------------------------------------
143 /* This draws the current progress. Each line has an overall percent
144 meter and a per active item status meter along with an overall
145 bandwidth and ETA indicator. */
146 bool AcqTextStatus::Pulse(pkgAcquire
*Owner
)
151 pkgAcquireStatus::Pulse(Owner
);
153 enum {Long
= 0,Medium
,Short
} Mode
= Long
;
155 char Buffer
[sizeof(BlankLine
)];
156 char *End
= Buffer
+ sizeof(Buffer
);
158 if (ScreenWidth
>= sizeof(Buffer
))
159 ScreenWidth
= sizeof(Buffer
)-1;
161 // Put in the percent done
162 sprintf(S
,"%ld%%",long(double((CurrentBytes
+ CurrentItems
)*100.0)/double(TotalBytes
+TotalItems
)));
165 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
166 I
= Owner
->WorkerStep(I
))
170 // There is no item running
171 if (I
->CurrentItem
== 0)
173 if (I
->Status
.empty() == false)
175 snprintf(S
,End
-S
," [%s]",I
->Status
.c_str());
184 // Add in the short description
185 if (I
->CurrentItem
->Owner
->ID
!= 0)
186 snprintf(S
,End
-S
," [%lu %s",I
->CurrentItem
->Owner
->ID
,
187 I
->CurrentItem
->ShortDesc
.c_str());
189 snprintf(S
,End
-S
," [%s",I
->CurrentItem
->ShortDesc
.c_str());
192 // Show the short mode string
193 if (I
->CurrentItem
->Owner
->Mode
!= 0)
195 snprintf(S
,End
-S
," %s",I
->CurrentItem
->Owner
->Mode
);
199 // Add the current progress
201 snprintf(S
,End
-S
," %lu",I
->CurrentSize
);
204 if (Mode
== Medium
|| I
->TotalSize
== 0)
205 snprintf(S
,End
-S
," %sB",SizeToStr(I
->CurrentSize
).c_str());
209 // Add the total size and percent
210 if (I
->TotalSize
> 0 && I
->CurrentItem
->Owner
->Complete
== false)
213 snprintf(S
,End
-S
," %lu%%",
214 long(double(I
->CurrentSize
*100.0)/double(I
->TotalSize
)));
216 snprintf(S
,End
-S
,"/%sB %lu%%",SizeToStr(I
->TotalSize
).c_str(),
217 long(double(I
->CurrentSize
*100.0)/double(I
->TotalSize
)));
220 snprintf(S
,End
-S
,"]");
225 snprintf(S
,End
-S
,_(" [Working]"));
227 /* Put in the ETA and cps meter, block off signals to prevent strangeness
229 sigset_t Sigs
,OldSigs
;
231 sigaddset(&Sigs
,SIGWINCH
);
232 sigprocmask(SIG_BLOCK
,&Sigs
,&OldSigs
);
237 unsigned long ETA
= (unsigned long)((TotalBytes
- CurrentBytes
)/CurrentCPS
);
238 sprintf(Tmp
," %sB/s %s",SizeToStr(CurrentCPS
).c_str(),TimeToStr(ETA
).c_str());
239 unsigned int Len
= strlen(Buffer
);
240 unsigned int LenT
= strlen(Tmp
);
241 if (Len
+ LenT
< ScreenWidth
)
243 memset(Buffer
+ Len
,' ',ScreenWidth
- Len
);
244 strcpy(Buffer
+ ScreenWidth
- LenT
,Tmp
);
247 Buffer
[ScreenWidth
] = 0;
248 BlankLine
[ScreenWidth
] = 0;
249 sigprocmask(SIG_SETMASK
,&OldSigs
,0);
251 // Draw the current status
252 if (strlen(Buffer
) == strlen(BlankLine
))
253 cout
<< '\r' << Buffer
<< flush
;
255 cout
<< '\r' << BlankLine
<< '\r' << Buffer
<< flush
;
256 memset(BlankLine
,' ',strlen(Buffer
));
257 BlankLine
[strlen(Buffer
)] = 0;
264 // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
265 // ---------------------------------------------------------------------
266 /* Prompt for a media swap */
267 bool AcqTextStatus::MediaChange(string Media
,string Drive
)
270 cout
<< '\r' << BlankLine
<< '\r';
271 ioprintf(cout
,_("Media change: please insert the disc labeled\n"
273 "in the drive '%s' and press enter\n"),
274 Media
.c_str(),Drive
.c_str());
278 while (C
!= '\n' && C
!= '\r')
280 int len
= read(STDIN_FILENO
,&C
,1);
281 if(C
== 'c' || len
<= 0)