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