Oops, removed debug code
[ntk/apt.git] / cmdline / acqprogress.cc
CommitLineData
0919e3f9
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
1bc849af 3// $Id: acqprogress.cc,v 1.9 1999/02/01 08:11:57 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>
0919e3f9 15#include <stdio.h>
0919e3f9
AL
16 /*}}}*/
17
18// AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
19// ---------------------------------------------------------------------
20/* */
d7827aca 21AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int Quiet) :
0919e3f9
AL
22 ScreenWidth(ScreenWidth), Quiet(Quiet)
23{
24}
25 /*}}}*/
26// AcqTextStatus::Start - Downloading has started /*{{{*/
27// ---------------------------------------------------------------------
28/* */
29void 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/* */
39void 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)
1bc849af 49 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "b]";
0919e3f9
AL
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 */
57void 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)
1bc849af 73 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "b]";
0919e3f9
AL
74 cout << endl;
75};
76 /*}}}*/
77// AcqTextStatus::Done - Completed a download /*{{{*/
78// ---------------------------------------------------------------------
79/* We don't display anything... */
80void 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 */
88void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
89{
90 if (Quiet > 1)
91 return;
92
93 if (Quiet <= 0)
94 cout << '\r' << BlankLine << '\r';
95
681d76d0
AL
96 if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
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
0919e3f9
AL
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 */
113void 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)
1bc849af 123 cout << "Fetched " << SizeToStr(FetchedBytes) << "b in " <<
0919e3f9 124 TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) <<
1bc849af 125 "b/s)" << endl;
0919e3f9
AL
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. */
133void 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
d568ed2d
AL
147 sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems)));
148
149 bool Shown = false;
0919e3f9
AL
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)
afce5764 159 {
0919e3f9 160 snprintf(S,End-S," [%s]",I->Status.c_str());
afce5764
AL
161 Shown = true;
162 }
163
0919e3f9
AL
164 continue;
165 }
166
d568ed2d
AL
167 Shown = true;
168
0919e3f9
AL
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)
1bc849af 190 snprintf(S,End-S," %sb",SizeToStr(I->CurrentSize).c_str());
0919e3f9
AL
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
1bc849af 201 snprintf(S,End-S,"/%sb %u%%",SizeToStr(I->TotalSize).c_str(),
0919e3f9
AL
202 long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
203 }
204 S += strlen(S);
205 snprintf(S,End-S,"]");
206 }
207
d568ed2d
AL
208 // Show something..
209 if (Shown == false)
210 snprintf(S,End-S," [Working]");
211
0919e3f9
AL
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);
1bc849af 217 sprintf(Tmp," %sb/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
0919e3f9
AL
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 /*}}}*/
542ec555
AL
239// AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
240// ---------------------------------------------------------------------
241/* Prompt for a media swap */
242bool AcqTextStatus::MediaChange(string Media,string Drive)
243{
244 if (Quiet <= 0)
245 cout << '\r' << BlankLine << '\r';
ac49a1e5
AL
246 cout << "Media Change: Please insert the disc labeled '" << Media << "' in "\
247 "the drive '" << Drive << "' and press enter" << endl;
542ec555 248
ac49a1e5
AL
249 char C = 0;
250 while (C != '\n' && C != '\r')
251 read(STDIN_FILENO,&C,1);
542ec555
AL
252
253 Update = true;
254 return true;
255}
256 /*}}}*/