]> git.saurik.com Git - apt.git/blob - apt-private/acqprogress.cc
Merge remote-tracking branch 'upstream/debian/sid' into feature/apt-download-file
[apt.git] / apt-private / acqprogress.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acqprogress.cc,v 1.24 2003/04/27 01:56:48 doogie Exp $
4 /* ######################################################################
5
6 Acquire Progress - Command line progress meter
7
8 ##################################################################### */
9 /*}}}*/
10 // Include files /*{{{*/
11 #include<config.h>
12
13 #include <apt-pkg/acquire-item.h>
14 #include <apt-pkg/acquire-worker.h>
15 #include <apt-pkg/configuration.h>
16 #include <apt-pkg/strutl.h>
17 #include <apt-pkg/error.h>
18
19 #include <stdio.h>
20 #include <signal.h>
21 #include <iostream>
22 #include <unistd.h>
23
24 #include "acqprogress.h"
25 #include <apti18n.h>
26 /*}}}*/
27
28 using namespace std;
29
30 // AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
31 // ---------------------------------------------------------------------
32 /* */
33 AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int const Quiet) :
34 pkgAcquireStatus(), ScreenWidth(ScreenWidth), ID(0), Quiet(Quiet)
35 {
36 BlankLine[0] = 0;
37 // testcases use it to disable pulses without disabling other user messages
38 if (Quiet == 0 && _config->FindB("quiet::NoUpdate", false) == true)
39 this->Quiet = 1;
40 }
41 /*}}}*/
42 // AcqTextStatus::Start - Downloading has started /*{{{*/
43 // ---------------------------------------------------------------------
44 /* */
45 void AcqTextStatus::Start()
46 {
47 pkgAcquireStatus::Start();
48 BlankLine[0] = 0;
49 ID = 1;
50 };
51 /*}}}*/
52 // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
53 // ---------------------------------------------------------------------
54 /* */
55 void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
56 {
57 if (Quiet > 1)
58 return;
59
60 if (Quiet <= 0)
61 cout << '\r' << BlankLine << '\r';
62
63 cout << _("Hit ") << Itm.Description;
64 if (Itm.Owner->FileSize != 0)
65 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
66 cout << endl;
67 Update = true;
68 };
69 /*}}}*/
70 // AcqTextStatus::Fetch - An item has started to download /*{{{*/
71 // ---------------------------------------------------------------------
72 /* This prints out the short description and the expected size */
73 void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
74 {
75 Update = true;
76 if (Itm.Owner->Complete == true)
77 return;
78
79 Itm.Owner->ID = ID++;
80
81 if (Quiet > 1)
82 return;
83
84 if (Quiet <= 0)
85 cout << '\r' << BlankLine << '\r';
86
87 cout << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description;
88 if (Itm.Owner->FileSize != 0)
89 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
90 cout << endl;
91 };
92 /*}}}*/
93 // AcqTextStatus::Done - Completed a download /*{{{*/
94 // ---------------------------------------------------------------------
95 /* We don't display anything... */
96 void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm)
97 {
98 Update = true;
99 };
100 /*}}}*/
101 // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
102 // ---------------------------------------------------------------------
103 /* We print out the error text */
104 void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
105 {
106 if (Quiet > 1)
107 return;
108
109 // Ignore certain kinds of transient failures (bad code)
110 if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
111 return;
112
113 if (Quiet <= 0)
114 cout << '\r' << BlankLine << '\r';
115
116 if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
117 {
118 cout << _("Ign ") << Itm.Description << endl;
119 }
120 else
121 {
122 cout << _("Err ") << Itm.Description << endl;
123 cout << " " << Itm.Owner->ErrorText << endl;
124 }
125
126 Update = true;
127 };
128 /*}}}*/
129 // AcqTextStatus::Stop - Finished downloading /*{{{*/
130 // ---------------------------------------------------------------------
131 /* This prints out the bytes downloaded and the overall average line
132 speed */
133 void AcqTextStatus::Stop()
134 {
135 pkgAcquireStatus::Stop();
136 if (Quiet > 1)
137 return;
138
139 if (Quiet <= 0)
140 cout << '\r' << BlankLine << '\r' << flush;
141
142 if (FetchedBytes != 0 && _error->PendingError() == false)
143 ioprintf(cout,_("Fetched %sB in %s (%sB/s)\n"),
144 SizeToStr(FetchedBytes).c_str(),
145 TimeToStr(ElapsedTime).c_str(),
146 SizeToStr(CurrentCPS).c_str());
147 }
148 /*}}}*/
149 // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This draws the current progress. Each line has an overall percent
152 meter and a per active item status meter along with an overall
153 bandwidth and ETA indicator. */
154 bool AcqTextStatus::Pulse(pkgAcquire *Owner)
155 {
156 pkgAcquireStatus::Pulse(Owner);
157
158 if (Quiet > 0)
159 return true;
160
161 enum {Long = 0,Medium,Short} Mode = Medium;
162
163 char Buffer[sizeof(BlankLine)];
164 char *End = Buffer + sizeof(Buffer);
165 char *S = Buffer;
166 if (ScreenWidth >= sizeof(Buffer))
167 ScreenWidth = sizeof(Buffer)-1;
168
169 // Put in the percent done
170 sprintf(S,"%.0f%%",((CurrentBytes + CurrentItems)*100.0)/(TotalBytes+TotalItems));
171
172 bool Shown = false;
173 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
174 I = Owner->WorkerStep(I))
175 {
176 S += strlen(S);
177
178 // There is no item running
179 if (I->CurrentItem == 0)
180 {
181 if (I->Status.empty() == false)
182 {
183 snprintf(S,End-S," [%s]",I->Status.c_str());
184 Shown = true;
185 }
186
187 continue;
188 }
189
190 Shown = true;
191
192 // Add in the short description
193 if (I->CurrentItem->Owner->ID != 0)
194 snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
195 I->CurrentItem->ShortDesc.c_str());
196 else
197 snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
198 S += strlen(S);
199
200 // Show the short mode string
201 if (I->CurrentItem->Owner->Mode != 0)
202 {
203 snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
204 S += strlen(S);
205 }
206
207 // Add the current progress
208 if (Mode == Long)
209 snprintf(S,End-S," %llu",I->CurrentSize);
210 else
211 {
212 if (Mode == Medium || I->TotalSize == 0)
213 snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
214 }
215 S += strlen(S);
216
217 // Add the total size and percent
218 if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
219 {
220 if (Mode == Short)
221 snprintf(S,End-S," %.0f%%",
222 (I->CurrentSize*100.0)/I->TotalSize);
223 else
224 snprintf(S,End-S,"/%sB %.0f%%",SizeToStr(I->TotalSize).c_str(),
225 (I->CurrentSize*100.0)/I->TotalSize);
226 }
227 S += strlen(S);
228 snprintf(S,End-S,"]");
229 }
230
231 // Show something..
232 if (Shown == false)
233 snprintf(S,End-S,_(" [Working]"));
234
235 /* Put in the ETA and cps meter, block off signals to prevent strangeness
236 during resizing */
237 sigset_t Sigs,OldSigs;
238 sigemptyset(&Sigs);
239 sigaddset(&Sigs,SIGWINCH);
240 sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
241
242 if (CurrentCPS != 0)
243 {
244 char Tmp[300];
245 unsigned long long ETA = (TotalBytes - CurrentBytes)/CurrentCPS;
246 sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
247 unsigned int Len = strlen(Buffer);
248 unsigned int LenT = strlen(Tmp);
249 if (Len + LenT < ScreenWidth)
250 {
251 memset(Buffer + Len,' ',ScreenWidth - Len);
252 strcpy(Buffer + ScreenWidth - LenT,Tmp);
253 }
254 }
255 Buffer[ScreenWidth] = 0;
256 BlankLine[ScreenWidth] = 0;
257 sigprocmask(SIG_SETMASK,&OldSigs,0);
258
259 // Draw the current status
260 if (_config->FindB("Apt::Color", false) == true)
261 cout << _config->Find("APT::Color::Yellow");
262 if (strlen(Buffer) == strlen(BlankLine))
263 cout << '\r' << Buffer << flush;
264 else
265 cout << '\r' << BlankLine << '\r' << Buffer << flush;
266 if (_config->FindB("Apt::Color", false) == true)
267 cout << _config->Find("APT::Color::Neutral");
268
269 memset(BlankLine,' ',strlen(Buffer));
270 BlankLine[strlen(Buffer)] = 0;
271
272 Update = false;
273
274 return true;
275 }
276 /*}}}*/
277 // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
278 // ---------------------------------------------------------------------
279 /* Prompt for a media swap */
280 bool AcqTextStatus::MediaChange(string Media,string Drive)
281 {
282 // If we do not output on a terminal and one of the options to avoid user
283 // interaction is given, we assume that no user is present who could react
284 // on your media change request
285 if (isatty(STDOUT_FILENO) != 1 && Quiet >= 2 &&
286 (_config->FindB("APT::Get::Assume-Yes",false) == true ||
287 _config->FindB("APT::Get::Force-Yes",false) == true ||
288 _config->FindB("APT::Get::Trivial-Only",false) == true))
289
290 return false;
291
292 if (Quiet <= 0)
293 cout << '\r' << BlankLine << '\r';
294 ioprintf(cout,_("Media change: please insert the disc labeled\n"
295 " '%s'\n"
296 "in the drive '%s' and press enter\n"),
297 Media.c_str(),Drive.c_str());
298
299 char C = 0;
300 bool bStatus = true;
301 while (C != '\n' && C != '\r')
302 {
303 int len = read(STDIN_FILENO,&C,1);
304 if(C == 'c' || len <= 0)
305 bStatus = false;
306 }
307
308 if(bStatus)
309 Update = true;
310 return bStatus;
311 }
312 /*}}}*/