]>
git.saurik.com Git - apt.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/configuration.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/error.h>
28 // AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
29 // ---------------------------------------------------------------------
31 AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth
,unsigned int Quiet
) :
32 ScreenWidth(ScreenWidth
), Quiet(Quiet
)
36 // AcqTextStatus::Start - Downloading has started /*{{{*/
37 // ---------------------------------------------------------------------
39 void AcqTextStatus::Start()
41 pkgAcquireStatus::Start();
46 // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
47 // ---------------------------------------------------------------------
49 void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc
&Itm
)
55 cout
<< '\r' << BlankLine
<< '\r';
57 cout
<< _("Hit ") << Itm
.Description
;
58 if (Itm
.Owner
->FileSize
!= 0)
59 cout
<< " [" << SizeToStr(Itm
.Owner
->FileSize
) << "B]";
64 // AcqTextStatus::Fetch - An item has started to download /*{{{*/
65 // ---------------------------------------------------------------------
66 /* This prints out the short description and the expected size */
67 void AcqTextStatus::Fetch(pkgAcquire::ItemDesc
&Itm
)
70 if (Itm
.Owner
->Complete
== true)
79 cout
<< '\r' << BlankLine
<< '\r';
81 cout
<< _("Get:") << Itm
.Owner
->ID
<< ' ' << Itm
.Description
;
82 if (Itm
.Owner
->FileSize
!= 0)
83 cout
<< " [" << SizeToStr(Itm
.Owner
->FileSize
) << "B]";
87 // AcqTextStatus::Done - Completed a download /*{{{*/
88 // ---------------------------------------------------------------------
89 /* We don't display anything... */
90 void AcqTextStatus::Done(pkgAcquire::ItemDesc
&Itm
)
95 // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
96 // ---------------------------------------------------------------------
97 /* We print out the error text */
98 void AcqTextStatus::Fail(pkgAcquire::ItemDesc
&Itm
)
103 // Ignore certain kinds of transient failures (bad code)
104 if (Itm
.Owner
->Status
== pkgAcquire::Item::StatIdle
)
108 cout
<< '\r' << BlankLine
<< '\r';
110 if (Itm
.Owner
->Status
== pkgAcquire::Item::StatDone
)
112 cout
<< _("Ign ") << Itm
.Description
<< endl
;
116 cout
<< _("Err ") << Itm
.Description
<< endl
;
117 cout
<< " " << Itm
.Owner
->ErrorText
<< endl
;
123 // AcqTextStatus::Stop - Finished downloading /*{{{*/
124 // ---------------------------------------------------------------------
125 /* This prints out the bytes downloaded and the overall average line
127 void AcqTextStatus::Stop()
129 pkgAcquireStatus::Stop();
134 cout
<< '\r' << BlankLine
<< '\r' << flush
;
136 if (FetchedBytes
!= 0 && _error
->PendingError() == false)
137 ioprintf(cout
,_("Fetched %sB in %s (%sB/s)\n"),
138 SizeToStr(FetchedBytes
).c_str(),
139 TimeToStr(ElapsedTime
).c_str(),
140 SizeToStr(CurrentCPS
).c_str());
143 // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
144 // ---------------------------------------------------------------------
145 /* This draws the current progress. Each line has an overall percent
146 meter and a per active item status meter along with an overall
147 bandwidth and ETA indicator. */
148 bool AcqTextStatus::Pulse(pkgAcquire
*Owner
)
150 pkgAcquireStatus::Pulse(Owner
);
155 enum {Long
= 0,Medium
,Short
} Mode
= Medium
;
157 char Buffer
[sizeof(BlankLine
)];
158 char *End
= Buffer
+ sizeof(Buffer
);
160 if (ScreenWidth
>= sizeof(Buffer
))
161 ScreenWidth
= sizeof(Buffer
)-1;
163 // Put in the percent done
164 sprintf(S
,"%ld%%",long(double((CurrentBytes
+ CurrentItems
)*100.0)/double(TotalBytes
+TotalItems
)));
167 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
168 I
= Owner
->WorkerStep(I
))
172 // There is no item running
173 if (I
->CurrentItem
== 0)
175 if (I
->Status
.empty() == false)
177 snprintf(S
,End
-S
," [%s]",I
->Status
.c_str());
186 // Add in the short description
187 if (I
->CurrentItem
->Owner
->ID
!= 0)
188 snprintf(S
,End
-S
," [%lu %s",I
->CurrentItem
->Owner
->ID
,
189 I
->CurrentItem
->ShortDesc
.c_str());
191 snprintf(S
,End
-S
," [%s",I
->CurrentItem
->ShortDesc
.c_str());
194 // Show the short mode string
195 if (I
->CurrentItem
->Owner
->Mode
!= 0)
197 snprintf(S
,End
-S
," %s",I
->CurrentItem
->Owner
->Mode
);
201 // Add the current progress
203 snprintf(S
,End
-S
," %lu",I
->CurrentSize
);
206 if (Mode
== Medium
|| I
->TotalSize
== 0)
207 snprintf(S
,End
-S
," %sB",SizeToStr(I
->CurrentSize
).c_str());
211 // Add the total size and percent
212 if (I
->TotalSize
> 0 && I
->CurrentItem
->Owner
->Complete
== false)
215 snprintf(S
,End
-S
," %lu%%",
216 long(double(I
->CurrentSize
*100.0)/double(I
->TotalSize
)));
218 snprintf(S
,End
-S
,"/%sB %lu%%",SizeToStr(I
->TotalSize
).c_str(),
219 long(double(I
->CurrentSize
*100.0)/double(I
->TotalSize
)));
222 snprintf(S
,End
-S
,"]");
227 snprintf(S
,End
-S
,_(" [Working]"));
229 /* Put in the ETA and cps meter, block off signals to prevent strangeness
231 sigset_t Sigs
,OldSigs
;
233 sigaddset(&Sigs
,SIGWINCH
);
234 sigprocmask(SIG_BLOCK
,&Sigs
,&OldSigs
);
239 unsigned long ETA
= (unsigned long)((TotalBytes
- CurrentBytes
)/CurrentCPS
);
240 sprintf(Tmp
," %sB/s %s",SizeToStr(CurrentCPS
).c_str(),TimeToStr(ETA
).c_str());
241 unsigned int Len
= strlen(Buffer
);
242 unsigned int LenT
= strlen(Tmp
);
243 if (Len
+ LenT
< ScreenWidth
)
245 memset(Buffer
+ Len
,' ',ScreenWidth
- Len
);
246 strcpy(Buffer
+ ScreenWidth
- LenT
,Tmp
);
249 Buffer
[ScreenWidth
] = 0;
250 BlankLine
[ScreenWidth
] = 0;
251 sigprocmask(SIG_SETMASK
,&OldSigs
,0);
253 // Draw the current status
254 if (strlen(Buffer
) == strlen(BlankLine
))
255 cout
<< '\r' << Buffer
<< flush
;
257 cout
<< '\r' << BlankLine
<< '\r' << Buffer
<< flush
;
258 memset(BlankLine
,' ',strlen(Buffer
));
259 BlankLine
[strlen(Buffer
)] = 0;
266 // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
267 // ---------------------------------------------------------------------
268 /* Prompt for a media swap */
269 bool AcqTextStatus::MediaChange(string Media
,string Drive
)
271 // If we do not output on a terminal and one of the options to avoid user
272 // interaction is given, we assume that no user is present who could react
273 // on your media change request
274 if (isatty(STDOUT_FILENO
) != 1 && Quiet
>= 2 &&
275 (_config
->FindB("APT::Get::Assume-Yes",false) == true ||
276 _config
->FindB("APT::Get::Force-Yes",false) == true ||
277 _config
->FindB("APT::Get::Trivial-Only",false) == true))
282 cout
<< '\r' << BlankLine
<< '\r';
283 ioprintf(cout
,_("Media change: please insert the disc labeled\n"
285 "in the drive '%s' and press enter\n"),
286 Media
.c_str(),Drive
.c_str());
290 while (C
!= '\n' && C
!= '\r')
292 int len
= read(STDIN_FILENO
,&C
,1);
293 if(C
== 'c' || len
<= 0)