]> git.saurik.com Git - apt.git/blame - apt-private/acqprogress.cc
apt-private/acqprogress.cc: do not show file size on IMSHit, it wasn't fetched, so...
[apt.git] / apt-private / acqprogress.cc
CommitLineData
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
31using namespace std;
32
33// AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
34// ---------------------------------------------------------------------
35/* */
18cce398
DK
36AcqTextStatus::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 48void 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/* */
58void 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 */
74void 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 97void 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 */
105void 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;
120 }
121 else
122 {
123 cout << _("Err ") << Itm.Description << endl;
124 cout << " " << Itm.Owner->ErrorText << endl;
125 }
d3e8fbb3 126
b9179170 127 Update = true;
d3e8fbb3 128}
b9179170
MV
129 /*}}}*/
130// AcqTextStatus::Stop - Finished downloading /*{{{*/
131// ---------------------------------------------------------------------
132/* This prints out the bytes downloaded and the overall average line
133 speed */
134void AcqTextStatus::Stop()
135{
136 pkgAcquireStatus::Stop();
137 if (Quiet > 1)
138 return;
139
140 if (Quiet <= 0)
141 cout << '\r' << BlankLine << '\r' << flush;
142
143 if (FetchedBytes != 0 && _error->PendingError() == false)
144 ioprintf(cout,_("Fetched %sB in %s (%sB/s)\n"),
145 SizeToStr(FetchedBytes).c_str(),
146 TimeToStr(ElapsedTime).c_str(),
147 SizeToStr(CurrentCPS).c_str());
148}
149 /*}}}*/
150// AcqTextStatus::Pulse - Regular event pulse /*{{{*/
151// ---------------------------------------------------------------------
152/* This draws the current progress. Each line has an overall percent
153 meter and a per active item status meter along with an overall
154 bandwidth and ETA indicator. */
155bool AcqTextStatus::Pulse(pkgAcquire *Owner)
156{
157 pkgAcquireStatus::Pulse(Owner);
d3e8fbb3 158
b9179170
MV
159 if (Quiet > 0)
160 return true;
d3e8fbb3 161
b9179170 162 enum {Long = 0,Medium,Short} Mode = Medium;
d3e8fbb3 163
b9179170
MV
164 char Buffer[sizeof(BlankLine)];
165 char *End = Buffer + sizeof(Buffer);
166 char *S = Buffer;
167 if (ScreenWidth >= sizeof(Buffer))
168 ScreenWidth = sizeof(Buffer)-1;
169
170 // Put in the percent done
96c6cab1 171 sprintf(S,"%.0f%%", Percent);
b9179170
MV
172
173 bool Shown = false;
174 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
175 I = Owner->WorkerStep(I))
176 {
177 S += strlen(S);
d3e8fbb3
DK
178
179 // There is no item running
b9179170
MV
180 if (I->CurrentItem == 0)
181 {
182 if (I->Status.empty() == false)
183 {
184 snprintf(S,End-S," [%s]",I->Status.c_str());
185 Shown = true;
186 }
d3e8fbb3 187
b9179170
MV
188 continue;
189 }
190
191 Shown = true;
d3e8fbb3 192
b9179170
MV
193 // Add in the short description
194 if (I->CurrentItem->Owner->ID != 0)
195 snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
196 I->CurrentItem->ShortDesc.c_str());
197 else
198 snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
199 S += strlen(S);
200
201 // Show the short mode string
202 if (I->CurrentItem->Owner->Mode != 0)
203 {
204 snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
205 S += strlen(S);
206 }
d3e8fbb3 207
b9179170
MV
208 // Add the current progress
209 if (Mode == Long)
210 snprintf(S,End-S," %llu",I->CurrentSize);
211 else
212 {
213 if (Mode == Medium || I->TotalSize == 0)
214 snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
215 }
216 S += strlen(S);
d3e8fbb3 217
b9179170
MV
218 // Add the total size and percent
219 if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
220 {
221 if (Mode == Short)
222 snprintf(S,End-S," %.0f%%",
223 (I->CurrentSize*100.0)/I->TotalSize);
224 else
225 snprintf(S,End-S,"/%sB %.0f%%",SizeToStr(I->TotalSize).c_str(),
226 (I->CurrentSize*100.0)/I->TotalSize);
d3e8fbb3 227 }
b9179170
MV
228 S += strlen(S);
229 snprintf(S,End-S,"]");
230 }
231
232 // Show something..
233 if (Shown == false)
234 snprintf(S,End-S,_(" [Working]"));
d3e8fbb3 235
b9179170
MV
236 /* Put in the ETA and cps meter, block off signals to prevent strangeness
237 during resizing */
238 sigset_t Sigs,OldSigs;
239 sigemptyset(&Sigs);
240 sigaddset(&Sigs,SIGWINCH);
241 sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
d3e8fbb3 242
b9179170 243 if (CurrentCPS != 0)
d3e8fbb3 244 {
b9179170
MV
245 char Tmp[300];
246 unsigned long long ETA = (TotalBytes - CurrentBytes)/CurrentCPS;
247 sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
248 unsigned int Len = strlen(Buffer);
249 unsigned int LenT = strlen(Tmp);
250 if (Len + LenT < ScreenWidth)
d3e8fbb3 251 {
b9179170
MV
252 memset(Buffer + Len,' ',ScreenWidth - Len);
253 strcpy(Buffer + ScreenWidth - LenT,Tmp);
d3e8fbb3 254 }
b9179170
MV
255 }
256 Buffer[ScreenWidth] = 0;
257 BlankLine[ScreenWidth] = 0;
258 sigprocmask(SIG_SETMASK,&OldSigs,0);
259
260 // Draw the current status
261 if (_config->FindB("Apt::Color", false) == true)
262 cout << _config->Find("APT::Color::Yellow");
263 if (strlen(Buffer) == strlen(BlankLine))
264 cout << '\r' << Buffer << flush;
265 else
266 cout << '\r' << BlankLine << '\r' << Buffer << flush;
267 if (_config->FindB("Apt::Color", false) == true)
6f34deea 268 cout << _config->Find("APT::Color::Neutral") << flush;
b9179170
MV
269
270 memset(BlankLine,' ',strlen(Buffer));
271 BlankLine[strlen(Buffer)] = 0;
d3e8fbb3 272
b9179170
MV
273 Update = false;
274
275 return true;
276}
277 /*}}}*/
278// AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
279// ---------------------------------------------------------------------
280/* Prompt for a media swap */
281bool AcqTextStatus::MediaChange(string Media,string Drive)
282{
283 // If we do not output on a terminal and one of the options to avoid user
284 // interaction is given, we assume that no user is present who could react
285 // on your media change request
286 if (isatty(STDOUT_FILENO) != 1 && Quiet >= 2 &&
287 (_config->FindB("APT::Get::Assume-Yes",false) == true ||
288 _config->FindB("APT::Get::Force-Yes",false) == true ||
289 _config->FindB("APT::Get::Trivial-Only",false) == true))
290
291 return false;
292
293 if (Quiet <= 0)
294 cout << '\r' << BlankLine << '\r';
295 ioprintf(cout,_("Media change: please insert the disc labeled\n"
296 " '%s'\n"
297 "in the drive '%s' and press enter\n"),
298 Media.c_str(),Drive.c_str());
299
300 char C = 0;
301 bool bStatus = true;
302 while (C != '\n' && C != '\r')
303 {
304 int len = read(STDIN_FILENO,&C,1);
305 if(C == 'c' || len <= 0)
306 bStatus = false;
307 }
308
309 if(bStatus)
310 Update = true;
311 return bStatus;
312}
313 /*}}}*/