3 more bugs
[ntk/apt.git] / cmdline / acqprogress.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acqprogress.cc,v 1.19 1999/11/16 03:16:34 jgg Exp $
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>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/error.h>
16
17 #include <stdio.h>
18 #include <signal.h>
19 /*}}}*/
20
21 // AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
22 // ---------------------------------------------------------------------
23 /* */
24 AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int Quiet) :
25 ScreenWidth(ScreenWidth), Quiet(Quiet)
26 {
27 }
28 /*}}}*/
29 // AcqTextStatus::Start - Downloading has started /*{{{*/
30 // ---------------------------------------------------------------------
31 /* */
32 void 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 /* */
42 void 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)
52 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
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 */
60 void 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
74 cout << "Get:" << Itm.Owner->ID << ' ' << Itm.Description;
75 if (Itm.Owner->FileSize != 0)
76 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
77 cout << endl;
78 };
79 /*}}}*/
80 // AcqTextStatus::Done - Completed a download /*{{{*/
81 // ---------------------------------------------------------------------
82 /* We don't display anything... */
83 void 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 */
91 void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
92 {
93 if (Quiet > 1)
94 return;
95
96 if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
97 return;
98
99 if (Quiet <= 0)
100 cout << '\r' << BlankLine << '\r';
101
102 if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
103 {
104 cout << "Ign " << Itm.Description << endl;
105 }
106 else
107 {
108 cout << "Err " << Itm.Description << endl;
109 cout << " " << Itm.Owner->ErrorText << endl;
110 }
111
112 Update = true;
113 };
114 /*}}}*/
115 // AcqTextStatus::Stop - Finished downloading /*{{{*/
116 // ---------------------------------------------------------------------
117 /* This prints out the bytes downloaded and the overall average line
118 speed */
119 void AcqTextStatus::Stop()
120 {
121 pkgAcquireStatus::Stop();
122 if (Quiet > 1)
123 return;
124
125 if (Quiet <= 0)
126 cout << '\r' << BlankLine << '\r' << flush;
127
128 if (FetchedBytes != 0 && _error->PendingError() == false)
129 cout << "Fetched " << SizeToStr(FetchedBytes) << "B in " <<
130 TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) <<
131 "B/s)" << endl;
132 }
133 /*}}}*/
134 // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
135 // ---------------------------------------------------------------------
136 /* This draws the current progress. Each line has an overall percent
137 meter and a per active item status meter along with an overall
138 bandwidth and ETA indicator. */
139 bool AcqTextStatus::Pulse(pkgAcquire *Owner)
140 {
141 if (Quiet > 0)
142 return true;
143
144 pkgAcquireStatus::Pulse(Owner);
145
146 enum {Long = 0,Medium,Short} Mode = Long;
147
148 char Buffer[1024];
149 char *End = Buffer + sizeof(Buffer);
150 char *S = Buffer;
151 if (ScreenWidth >= sizeof(Buffer))
152 ScreenWidth = sizeof(Buffer)-1;
153
154 // Put in the percent done
155 sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems)));
156
157 bool Shown = false;
158 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
159 I = Owner->WorkerStep(I))
160 {
161 S += strlen(S);
162
163 // There is no item running
164 if (I->CurrentItem == 0)
165 {
166 if (I->Status.empty() == false)
167 {
168 snprintf(S,End-S," [%s]",I->Status.c_str());
169 Shown = true;
170 }
171
172 continue;
173 }
174
175 Shown = true;
176
177 // Add in the short description
178 if (I->CurrentItem->Owner->ID != 0)
179 snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
180 I->CurrentItem->ShortDesc.c_str());
181 else
182 snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
183 S += strlen(S);
184
185 // Show the short mode string
186 if (I->CurrentItem->Owner->Mode != 0)
187 {
188 snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
189 S += strlen(S);
190 }
191
192 // Add the current progress
193 if (Mode == Long)
194 snprintf(S,End-S," %lu",I->CurrentSize);
195 else
196 {
197 if (Mode == Medium || I->TotalSize == 0)
198 snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
199 }
200 S += strlen(S);
201
202 // Add the total size and percent
203 if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
204 {
205 if (Mode == Short)
206 snprintf(S,End-S," %lu%%",
207 long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
208 else
209 snprintf(S,End-S,"/%sB %lu%%",SizeToStr(I->TotalSize).c_str(),
210 long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
211 }
212 S += strlen(S);
213 snprintf(S,End-S,"]");
214 }
215
216 // Show something..
217 if (Shown == false)
218 snprintf(S,End-S," [Working]");
219
220 /* Put in the ETA and cps meter, block off signals to prevent strangeness
221 during resizing */
222 sigset_t Sigs,OldSigs;
223 sigemptyset(&Sigs);
224 sigaddset(&Sigs,SIGWINCH);
225 sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
226
227 if (CurrentCPS != 0)
228 {
229 char Tmp[300];
230 unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
231 sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
232 unsigned int Len = strlen(Buffer);
233 unsigned int LenT = strlen(Tmp);
234 if (Len + LenT < ScreenWidth)
235 {
236 memset(Buffer + Len,' ',ScreenWidth - Len);
237 strcpy(Buffer + ScreenWidth - LenT,Tmp);
238 }
239 }
240 Buffer[ScreenWidth] = 0;
241 BlankLine[ScreenWidth] = 0;
242 sigprocmask(SIG_UNBLOCK,&OldSigs,0);
243
244 // Draw the current status
245 if (strlen(Buffer) == strlen(BlankLine))
246 cout << '\r' << Buffer << flush;
247 else
248 cout << '\r' << BlankLine << '\r' << Buffer << flush;
249 memset(BlankLine,' ',strlen(Buffer));
250 BlankLine[strlen(Buffer)] = 0;
251
252 Update = false;
253
254 return true;
255 }
256 /*}}}*/
257 // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
258 // ---------------------------------------------------------------------
259 /* Prompt for a media swap */
260 bool AcqTextStatus::MediaChange(string Media,string Drive)
261 {
262 if (Quiet <= 0)
263 cout << '\r' << BlankLine << '\r';
264 cout << "Media Change: Please insert the disc labeled '" << Media << "' in "\
265 "the drive '" << Drive << "' and press enter" << endl;
266
267 char C = 0;
268 while (C != '\n' && C != '\r')
269 read(STDIN_FILENO,&C,1);
270
271 Update = true;
272 return true;
273 }
274 /*}}}*/