describe script usage in the manpage
[ntk/apt.git] / cmdline / acqprogress.cc
CommitLineData
0919e3f9
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
14cb8abc 3// $Id: acqprogress.cc,v 1.24 2003/04/27 01:56:48 doogie Exp $
0919e3f9
AL
4/* ######################################################################
5
6 Acquire Progress - Command line progress meter
7
8 ##################################################################### */
9 /*}}}*/
10// Include files /*{{{*/
ea542140
DK
11#include<config.h>
12
0919e3f9
AL
13#include <apt-pkg/acquire-item.h>
14#include <apt-pkg/acquire-worker.h>
996c6447 15#include <apt-pkg/configuration.h>
cdcc6d34 16#include <apt-pkg/strutl.h>
8508b1df 17#include <apt-pkg/error.h>
b0db36b1 18
0919e3f9 19#include <stdio.h>
b0db36b1 20#include <signal.h>
076d01b0 21#include <iostream>
996c6447 22#include <unistd.h>
ea542140
DK
23
24#include "acqprogress.h"
25#include <apti18n.h>
0919e3f9
AL
26 /*}}}*/
27
076d01b0
AL
28using namespace std;
29
0919e3f9
AL
30// AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
31// ---------------------------------------------------------------------
32/* */
d7827aca 33AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int Quiet) :
dcaa1185 34 ScreenWidth(ScreenWidth), ID(0), Quiet(Quiet)
0919e3f9 35{
dcaa1185 36 BlankLine[0] = 0;
0919e3f9
AL
37}
38 /*}}}*/
39// AcqTextStatus::Start - Downloading has started /*{{{*/
40// ---------------------------------------------------------------------
41/* */
42void AcqTextStatus::Start()
43{
44 pkgAcquireStatus::Start();
45 BlankLine[0] = 0;
46 ID = 1;
47};
48 /*}}}*/
49// AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
50// ---------------------------------------------------------------------
51/* */
52void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
53{
54 if (Quiet > 1)
55 return;
56
57 if (Quiet <= 0)
58 cout << '\r' << BlankLine << '\r';
59
b2e465d6 60 cout << _("Hit ") << Itm.Description;
0919e3f9 61 if (Itm.Owner->FileSize != 0)
314037ba 62 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
0919e3f9
AL
63 cout << endl;
64 Update = true;
65};
66 /*}}}*/
67// AcqTextStatus::Fetch - An item has started to download /*{{{*/
68// ---------------------------------------------------------------------
69/* This prints out the short description and the expected size */
70void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
71{
72 Update = true;
73 if (Itm.Owner->Complete == true)
74 return;
75
76 Itm.Owner->ID = ID++;
77
78 if (Quiet > 1)
79 return;
80
81 if (Quiet <= 0)
82 cout << '\r' << BlankLine << '\r';
83
b2e465d6 84 cout << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description;
0919e3f9 85 if (Itm.Owner->FileSize != 0)
314037ba 86 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
0919e3f9
AL
87 cout << endl;
88};
89 /*}}}*/
90// AcqTextStatus::Done - Completed a download /*{{{*/
91// ---------------------------------------------------------------------
92/* We don't display anything... */
93void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm)
94{
95 Update = true;
96};
97 /*}}}*/
98// AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
99// ---------------------------------------------------------------------
100/* We print out the error text */
101void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
102{
103 if (Quiet > 1)
104 return;
105
b800c80c 106 // Ignore certain kinds of transient failures (bad code)
281daf46
AL
107 if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
108 return;
109
0919e3f9
AL
110 if (Quiet <= 0)
111 cout << '\r' << BlankLine << '\r';
112
2b154e53 113 if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
681d76d0 114 {
b2e465d6 115 cout << _("Ign ") << Itm.Description << endl;
681d76d0
AL
116 }
117 else
118 {
b2e465d6 119 cout << _("Err ") << Itm.Description << endl;
681d76d0
AL
120 cout << " " << Itm.Owner->ErrorText << endl;
121 }
122
0919e3f9
AL
123 Update = true;
124};
125 /*}}}*/
126// AcqTextStatus::Stop - Finished downloading /*{{{*/
127// ---------------------------------------------------------------------
128/* This prints out the bytes downloaded and the overall average line
129 speed */
130void AcqTextStatus::Stop()
131{
132 pkgAcquireStatus::Stop();
133 if (Quiet > 1)
134 return;
135
136 if (Quiet <= 0)
9e2a06ff 137 cout << '\r' << BlankLine << '\r' << flush;
b2e465d6 138
8508b1df 139 if (FetchedBytes != 0 && _error->PendingError() == false)
b2e465d6
AL
140 ioprintf(cout,_("Fetched %sB in %s (%sB/s)\n"),
141 SizeToStr(FetchedBytes).c_str(),
142 TimeToStr(ElapsedTime).c_str(),
143 SizeToStr(CurrentCPS).c_str());
0919e3f9
AL
144}
145 /*}}}*/
146// AcqTextStatus::Pulse - Regular event pulse /*{{{*/
147// ---------------------------------------------------------------------
148/* This draws the current progress. Each line has an overall percent
149 meter and a per active item status meter along with an overall
150 bandwidth and ETA indicator. */
024d1123 151bool AcqTextStatus::Pulse(pkgAcquire *Owner)
0919e3f9 152{
09fab244
MV
153 pkgAcquireStatus::Pulse(Owner);
154
0919e3f9 155 if (Quiet > 0)
024d1123 156 return true;
0919e3f9 157
2893f7b5 158 enum {Long = 0,Medium,Short} Mode = Medium;
0919e3f9 159
3b77265b 160 char Buffer[sizeof(BlankLine)];
0919e3f9
AL
161 char *End = Buffer + sizeof(Buffer);
162 char *S = Buffer;
8b067c22
AL
163 if (ScreenWidth >= sizeof(Buffer))
164 ScreenWidth = sizeof(Buffer)-1;
165
0919e3f9 166 // Put in the percent done
650faab0 167 sprintf(S,"%.0f%%",((CurrentBytes + CurrentItems)*100.0)/(TotalBytes+TotalItems));
d568ed2d
AL
168
169 bool Shown = false;
0919e3f9
AL
170 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
171 I = Owner->WorkerStep(I))
172 {
173 S += strlen(S);
174
175 // There is no item running
176 if (I->CurrentItem == 0)
177 {
178 if (I->Status.empty() == false)
afce5764 179 {
0919e3f9 180 snprintf(S,End-S," [%s]",I->Status.c_str());
afce5764
AL
181 Shown = true;
182 }
183
0919e3f9
AL
184 continue;
185 }
186
d568ed2d
AL
187 Shown = true;
188
0919e3f9
AL
189 // Add in the short description
190 if (I->CurrentItem->Owner->ID != 0)
eec898ad 191 snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
0919e3f9
AL
192 I->CurrentItem->ShortDesc.c_str());
193 else
194 snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
195 S += strlen(S);
196
197 // Show the short mode string
198 if (I->CurrentItem->Owner->Mode != 0)
199 {
200 snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
201 S += strlen(S);
202 }
203
204 // Add the current progress
205 if (Mode == Long)
dbbc5494 206 snprintf(S,End-S," %llu",I->CurrentSize);
0919e3f9
AL
207 else
208 {
209 if (Mode == Medium || I->TotalSize == 0)
314037ba 210 snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
0919e3f9
AL
211 }
212 S += strlen(S);
213
214 // Add the total size and percent
215 if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
216 {
217 if (Mode == Short)
650faab0
DK
218 snprintf(S,End-S," %.0f%%",
219 (I->CurrentSize*100.0)/I->TotalSize);
0919e3f9 220 else
650faab0
DK
221 snprintf(S,End-S,"/%sB %.0f%%",SizeToStr(I->TotalSize).c_str(),
222 (I->CurrentSize*100.0)/I->TotalSize);
0919e3f9
AL
223 }
224 S += strlen(S);
225 snprintf(S,End-S,"]");
226 }
227
d568ed2d
AL
228 // Show something..
229 if (Shown == false)
b2e465d6 230 snprintf(S,End-S,_(" [Working]"));
d568ed2d 231
b0db36b1
AL
232 /* Put in the ETA and cps meter, block off signals to prevent strangeness
233 during resizing */
234 sigset_t Sigs,OldSigs;
235 sigemptyset(&Sigs);
236 sigaddset(&Sigs,SIGWINCH);
237 sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
238
0919e3f9
AL
239 if (CurrentCPS != 0)
240 {
241 char Tmp[300];
650faab0 242 unsigned long long ETA = (TotalBytes - CurrentBytes)/CurrentCPS;
314037ba 243 sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
0919e3f9
AL
244 unsigned int Len = strlen(Buffer);
245 unsigned int LenT = strlen(Tmp);
246 if (Len + LenT < ScreenWidth)
247 {
248 memset(Buffer + Len,' ',ScreenWidth - Len);
249 strcpy(Buffer + ScreenWidth - LenT,Tmp);
250 }
251 }
252 Buffer[ScreenWidth] = 0;
b0db36b1 253 BlankLine[ScreenWidth] = 0;
b2e465d6 254 sigprocmask(SIG_SETMASK,&OldSigs,0);
b0db36b1 255
0919e3f9
AL
256 // Draw the current status
257 if (strlen(Buffer) == strlen(BlankLine))
258 cout << '\r' << Buffer << flush;
259 else
260 cout << '\r' << BlankLine << '\r' << Buffer << flush;
261 memset(BlankLine,' ',strlen(Buffer));
262 BlankLine[strlen(Buffer)] = 0;
263
264 Update = false;
024d1123
AL
265
266 return true;
0919e3f9
AL
267}
268 /*}}}*/
542ec555
AL
269// AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
270// ---------------------------------------------------------------------
271/* Prompt for a media swap */
272bool AcqTextStatus::MediaChange(string Media,string Drive)
273{
996c6447
DK
274 // If we do not output on a terminal and one of the options to avoid user
275 // interaction is given, we assume that no user is present who could react
276 // on your media change request
277 if (isatty(STDOUT_FILENO) != 1 && Quiet >= 2 &&
278 (_config->FindB("APT::Get::Assume-Yes",false) == true ||
279 _config->FindB("APT::Get::Force-Yes",false) == true ||
280 _config->FindB("APT::Get::Trivial-Only",false) == true))
281
282 return false;
283
542ec555 284 if (Quiet <= 0)
b2e465d6 285 cout << '\r' << BlankLine << '\r';
db0db9fe 286 ioprintf(cout,_("Media change: please insert the disc labeled\n"
14cb8abc
AL
287 " '%s'\n"
288 "in the drive '%s' and press enter\n"),
b2e465d6 289 Media.c_str(),Drive.c_str());
542ec555 290
ac49a1e5 291 char C = 0;
2a749770 292 bool bStatus = true;
ac49a1e5 293 while (C != '\n' && C != '\r')
2a749770
MV
294 {
295 int len = read(STDIN_FILENO,&C,1);
296 if(C == 'c' || len <= 0)
297 bStatus = false;
298 }
299
300 if(bStatus)
301 Update = true;
302 return bStatus;
542ec555
AL
303}
304 /*}}}*/