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