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