]> git.saurik.com Git - apt.git/blame_incremental - apt-private/acqprogress.cc
reimplement the last uses of sprintf
[apt.git] / apt-private / acqprogress.cc
... / ...
CommitLineData
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
13#include <apt-pkg/acquire.h>
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
20#include <apt-private/acqprogress.h>
21
22#include <string.h>
23#include <stdio.h>
24#include <signal.h>
25#include <iostream>
26#include <sstream>
27#include <unistd.h>
28
29#include <apti18n.h>
30 /*}}}*/
31
32using namespace std;
33
34// AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
35// ---------------------------------------------------------------------
36/* */
37AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int const Quiet) :
38 pkgAcquireStatus(), ScreenWidth(ScreenWidth), LastLineLength(0), ID(0), Quiet(Quiet)
39{
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;
43}
44 /*}}}*/
45// AcqTextStatus::Start - Downloading has started /*{{{*/
46// ---------------------------------------------------------------------
47/* */
48void AcqTextStatus::Start()
49{
50 pkgAcquireStatus::Start();
51 LastLineLength = 0;
52 ID = 1;
53}
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
63 clearLastLine();
64
65 cout << _("Hit ") << Itm.Description;
66 cout << endl;
67 Update = true;
68}
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;
78
79 Itm.Owner->ID = ID++;
80
81 if (Quiet > 1)
82 return;
83
84 clearLastLine();
85
86 cout << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description;
87 if (Itm.Owner->FileSize != 0)
88 cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
89 cout << endl;
90}
91 /*}}}*/
92// AcqTextStatus::Done - Completed a download /*{{{*/
93// ---------------------------------------------------------------------
94/* We don't display anything... */
95void AcqTextStatus::Done(pkgAcquire::ItemDesc &/*Itm*/)
96{
97 Update = true;
98}
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;
111
112 clearLastLine();
113
114 if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
115 {
116 cout << _("Ign ") << Itm.Description << endl;
117 if (Itm.Owner->ErrorText.empty() == false &&
118 _config->FindB("Acquire::Progress::Ignore::ShowErrorText", false) == true)
119 cout << " " << Itm.Owner->ErrorText << endl;
120 }
121 else
122 {
123 cout << _("Err ") << Itm.Description << endl;
124 cout << " " << Itm.Owner->ErrorText << endl;
125 }
126
127 Update = true;
128}
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
140 clearLastLine();
141
142 if (_config->FindB("quiet::NoStatistic", false) == true)
143 return;
144
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);
160
161 if (Quiet > 0)
162 return true;
163
164 enum {Long = 0,Medium,Short} Mode = Medium;
165
166 std::string Line;
167 {
168 std::stringstream S;
169 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
170 I = Owner->WorkerStep(I))
171 {
172 // There is no item running
173 if (I->CurrentItem == 0)
174 {
175 if (I->Status.empty() == false)
176 S << " [" << I->Status << "]";
177
178 continue;
179 }
180
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;
186
187 // Show the short mode string
188 if (I->CurrentItem->Owner->ActiveSubprocess.empty() == false)
189 S << " " << I->CurrentItem->Owner->ActiveSubprocess;
190
191 // Add the current progress
192 if (Mode == Long)
193 S << " " << I->CurrentSize;
194 else
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(),
207 (I->CurrentSize*100.0)/I->TotalSize);
208 }
209 S << "]";
210 }
211
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 }
226
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);
233
234 if (CurrentCPS != 0)
235 {
236 unsigned long long ETA = (TotalBytes - CurrentBytes)/CurrentCPS;
237 std::string Tmp = " " + SizeToStr(CurrentCPS) + "B/s " + TimeToStr(ETA);
238 size_t alignment = Line.length() + Tmp.length();
239 if (alignment < ScreenWidth)
240 {
241 alignment = ScreenWidth - alignment;
242 for (size_t i = 0; i < alignment; ++i)
243 Line.append(" ");
244 Line.append(Tmp);
245 }
246 }
247 if (Line.length() > ScreenWidth)
248 Line.erase(ScreenWidth);
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");
254 if (LastLineLength > Line.length())
255 clearLastLine();
256 else
257 cout << '\r';
258 cout << Line << flush;
259 if (_config->FindB("Apt::Color", false) == true)
260 cout << _config->Find("APT::Color::Neutral") << flush;
261
262 LastLineLength = Line.length();
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
283 clearLastLine();
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 /*}}}*/
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 /*}}}*/