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