]>
Commit | Line | Data |
---|---|---|
0919e3f9 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
14cb8abc | 3 | // $Id: acqprogress.cc,v 1.24 2003/04/27 01:56:48 doogie Exp $ |
0919e3f9 AL |
4 | /* ###################################################################### |
5 | ||
6 | Acquire Progress - Command line progress meter | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include files /*{{{*/ | |
11 | #include "acqprogress.h" | |
12 | #include <apt-pkg/acquire-item.h> | |
13 | #include <apt-pkg/acquire-worker.h> | |
cdcc6d34 | 14 | #include <apt-pkg/strutl.h> |
8508b1df | 15 | #include <apt-pkg/error.h> |
b0db36b1 | 16 | |
b2e465d6 AL |
17 | #include <apti18n.h> |
18 | ||
0919e3f9 | 19 | #include <stdio.h> |
b0db36b1 | 20 | #include <signal.h> |
076d01b0 | 21 | #include <iostream> |
0919e3f9 AL |
22 | /*}}}*/ |
23 | ||
076d01b0 AL |
24 | using namespace std; |
25 | ||
0919e3f9 AL |
26 | // AcqTextStatus::AcqTextStatus - Constructor /*{{{*/ |
27 | // --------------------------------------------------------------------- | |
28 | /* */ | |
d7827aca | 29 | AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int Quiet) : |
0919e3f9 AL |
30 | ScreenWidth(ScreenWidth), Quiet(Quiet) |
31 | { | |
32 | } | |
33 | /*}}}*/ | |
34 | // AcqTextStatus::Start - Downloading has started /*{{{*/ | |
35 | // --------------------------------------------------------------------- | |
36 | /* */ | |
37 | void AcqTextStatus::Start() | |
38 | { | |
39 | pkgAcquireStatus::Start(); | |
40 | BlankLine[0] = 0; | |
41 | ID = 1; | |
42 | }; | |
43 | /*}}}*/ | |
44 | // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/ | |
45 | // --------------------------------------------------------------------- | |
46 | /* */ | |
47 | void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm) | |
48 | { | |
49 | if (Quiet > 1) | |
50 | return; | |
51 | ||
52 | if (Quiet <= 0) | |
53 | cout << '\r' << BlankLine << '\r'; | |
54 | ||
b2e465d6 | 55 | cout << _("Hit ") << Itm.Description; |
0919e3f9 | 56 | if (Itm.Owner->FileSize != 0) |
314037ba | 57 | cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]"; |
0919e3f9 AL |
58 | cout << endl; |
59 | Update = true; | |
60 | }; | |
61 | /*}}}*/ | |
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) | |
66 | { | |
67 | Update = true; | |
68 | if (Itm.Owner->Complete == true) | |
69 | return; | |
70 | ||
71 | Itm.Owner->ID = ID++; | |
72 | ||
73 | if (Quiet > 1) | |
74 | return; | |
75 | ||
76 | if (Quiet <= 0) | |
77 | cout << '\r' << BlankLine << '\r'; | |
78 | ||
b2e465d6 | 79 | cout << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description; |
0919e3f9 | 80 | if (Itm.Owner->FileSize != 0) |
314037ba | 81 | cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]"; |
0919e3f9 AL |
82 | cout << endl; |
83 | }; | |
84 | /*}}}*/ | |
85 | // AcqTextStatus::Done - Completed a download /*{{{*/ | |
86 | // --------------------------------------------------------------------- | |
87 | /* We don't display anything... */ | |
88 | void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm) | |
89 | { | |
90 | Update = true; | |
91 | }; | |
92 | /*}}}*/ | |
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) | |
97 | { | |
98 | if (Quiet > 1) | |
99 | return; | |
100 | ||
b800c80c | 101 | // Ignore certain kinds of transient failures (bad code) |
281daf46 AL |
102 | if (Itm.Owner->Status == pkgAcquire::Item::StatIdle) |
103 | return; | |
104 | ||
0919e3f9 AL |
105 | if (Quiet <= 0) |
106 | cout << '\r' << BlankLine << '\r'; | |
107 | ||
2b154e53 | 108 | if (Itm.Owner->Status == pkgAcquire::Item::StatDone) |
681d76d0 | 109 | { |
b2e465d6 | 110 | cout << _("Ign ") << Itm.Description << endl; |
681d76d0 AL |
111 | } |
112 | else | |
113 | { | |
b2e465d6 | 114 | cout << _("Err ") << Itm.Description << endl; |
681d76d0 AL |
115 | cout << " " << Itm.Owner->ErrorText << endl; |
116 | } | |
117 | ||
0919e3f9 AL |
118 | Update = true; |
119 | }; | |
120 | /*}}}*/ | |
121 | // AcqTextStatus::Stop - Finished downloading /*{{{*/ | |
122 | // --------------------------------------------------------------------- | |
123 | /* This prints out the bytes downloaded and the overall average line | |
124 | speed */ | |
125 | void AcqTextStatus::Stop() | |
126 | { | |
127 | pkgAcquireStatus::Stop(); | |
128 | if (Quiet > 1) | |
129 | return; | |
130 | ||
131 | if (Quiet <= 0) | |
9e2a06ff | 132 | cout << '\r' << BlankLine << '\r' << flush; |
b2e465d6 | 133 | |
8508b1df | 134 | if (FetchedBytes != 0 && _error->PendingError() == false) |
b2e465d6 AL |
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()); | |
0919e3f9 AL |
139 | } |
140 | /*}}}*/ | |
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. */ | |
024d1123 | 146 | bool AcqTextStatus::Pulse(pkgAcquire *Owner) |
0919e3f9 | 147 | { |
09fab244 MV |
148 | pkgAcquireStatus::Pulse(Owner); |
149 | ||
0919e3f9 | 150 | if (Quiet > 0) |
024d1123 | 151 | return true; |
0919e3f9 | 152 | |
2893f7b5 | 153 | enum {Long = 0,Medium,Short} Mode = Medium; |
0919e3f9 | 154 | |
3b77265b | 155 | char Buffer[sizeof(BlankLine)]; |
0919e3f9 AL |
156 | char *End = Buffer + sizeof(Buffer); |
157 | char *S = Buffer; | |
8b067c22 AL |
158 | if (ScreenWidth >= sizeof(Buffer)) |
159 | ScreenWidth = sizeof(Buffer)-1; | |
160 | ||
0919e3f9 | 161 | // Put in the percent done |
d568ed2d AL |
162 | sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems))); |
163 | ||
164 | bool Shown = false; | |
0919e3f9 AL |
165 | for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0; |
166 | I = Owner->WorkerStep(I)) | |
167 | { | |
168 | S += strlen(S); | |
169 | ||
170 | // There is no item running | |
171 | if (I->CurrentItem == 0) | |
172 | { | |
173 | if (I->Status.empty() == false) | |
afce5764 | 174 | { |
0919e3f9 | 175 | snprintf(S,End-S," [%s]",I->Status.c_str()); |
afce5764 AL |
176 | Shown = true; |
177 | } | |
178 | ||
0919e3f9 AL |
179 | continue; |
180 | } | |
181 | ||
d568ed2d AL |
182 | Shown = true; |
183 | ||
0919e3f9 AL |
184 | // Add in the short description |
185 | if (I->CurrentItem->Owner->ID != 0) | |
eec898ad | 186 | snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID, |
0919e3f9 AL |
187 | I->CurrentItem->ShortDesc.c_str()); |
188 | else | |
189 | snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str()); | |
190 | S += strlen(S); | |
191 | ||
192 | // Show the short mode string | |
193 | if (I->CurrentItem->Owner->Mode != 0) | |
194 | { | |
195 | snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode); | |
196 | S += strlen(S); | |
197 | } | |
198 | ||
199 | // Add the current progress | |
200 | if (Mode == Long) | |
f40e3a64 | 201 | snprintf(S,End-S," %lu",I->CurrentSize); |
0919e3f9 AL |
202 | else |
203 | { | |
204 | if (Mode == Medium || I->TotalSize == 0) | |
314037ba | 205 | snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str()); |
0919e3f9 AL |
206 | } |
207 | S += strlen(S); | |
208 | ||
209 | // Add the total size and percent | |
210 | if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false) | |
211 | { | |
212 | if (Mode == Short) | |
f40e3a64 | 213 | snprintf(S,End-S," %lu%%", |
0919e3f9 AL |
214 | long(double(I->CurrentSize*100.0)/double(I->TotalSize))); |
215 | else | |
314037ba | 216 | snprintf(S,End-S,"/%sB %lu%%",SizeToStr(I->TotalSize).c_str(), |
0919e3f9 AL |
217 | long(double(I->CurrentSize*100.0)/double(I->TotalSize))); |
218 | } | |
219 | S += strlen(S); | |
220 | snprintf(S,End-S,"]"); | |
221 | } | |
222 | ||
d568ed2d AL |
223 | // Show something.. |
224 | if (Shown == false) | |
b2e465d6 | 225 | snprintf(S,End-S,_(" [Working]")); |
d568ed2d | 226 | |
b0db36b1 AL |
227 | /* Put in the ETA and cps meter, block off signals to prevent strangeness |
228 | during resizing */ | |
229 | sigset_t Sigs,OldSigs; | |
230 | sigemptyset(&Sigs); | |
231 | sigaddset(&Sigs,SIGWINCH); | |
232 | sigprocmask(SIG_BLOCK,&Sigs,&OldSigs); | |
233 | ||
0919e3f9 AL |
234 | if (CurrentCPS != 0) |
235 | { | |
236 | char Tmp[300]; | |
237 | unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS); | |
314037ba | 238 | sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str()); |
0919e3f9 AL |
239 | unsigned int Len = strlen(Buffer); |
240 | unsigned int LenT = strlen(Tmp); | |
241 | if (Len + LenT < ScreenWidth) | |
242 | { | |
243 | memset(Buffer + Len,' ',ScreenWidth - Len); | |
244 | strcpy(Buffer + ScreenWidth - LenT,Tmp); | |
245 | } | |
246 | } | |
247 | Buffer[ScreenWidth] = 0; | |
b0db36b1 | 248 | BlankLine[ScreenWidth] = 0; |
b2e465d6 | 249 | sigprocmask(SIG_SETMASK,&OldSigs,0); |
b0db36b1 | 250 | |
0919e3f9 AL |
251 | // Draw the current status |
252 | if (strlen(Buffer) == strlen(BlankLine)) | |
253 | cout << '\r' << Buffer << flush; | |
254 | else | |
255 | cout << '\r' << BlankLine << '\r' << Buffer << flush; | |
256 | memset(BlankLine,' ',strlen(Buffer)); | |
257 | BlankLine[strlen(Buffer)] = 0; | |
258 | ||
259 | Update = false; | |
024d1123 AL |
260 | |
261 | return true; | |
0919e3f9 AL |
262 | } |
263 | /*}}}*/ | |
542ec555 AL |
264 | // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/ |
265 | // --------------------------------------------------------------------- | |
266 | /* Prompt for a media swap */ | |
267 | bool AcqTextStatus::MediaChange(string Media,string Drive) | |
268 | { | |
269 | if (Quiet <= 0) | |
b2e465d6 | 270 | cout << '\r' << BlankLine << '\r'; |
db0db9fe | 271 | ioprintf(cout,_("Media change: please insert the disc labeled\n" |
14cb8abc AL |
272 | " '%s'\n" |
273 | "in the drive '%s' and press enter\n"), | |
b2e465d6 | 274 | Media.c_str(),Drive.c_str()); |
542ec555 | 275 | |
ac49a1e5 | 276 | char C = 0; |
2a749770 | 277 | bool bStatus = true; |
ac49a1e5 | 278 | while (C != '\n' && C != '\r') |
2a749770 MV |
279 | { |
280 | int len = read(STDIN_FILENO,&C,1); | |
281 | if(C == 'c' || len <= 0) | |
282 | bStatus = false; | |
283 | } | |
284 | ||
285 | if(bStatus) | |
286 | Update = true; | |
287 | return bStatus; | |
542ec555 AL |
288 | } |
289 | /*}}}*/ |