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