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