]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: progress.cc,v 1.12 2003/01/11 07:17:04 jgg Exp $
4 /* ######################################################################
6 OpProgress - Operation Progress
8 ##################################################################### */
10 // Include Files /*{{{*/
12 #pragma implementation "apt-pkg/progress.h"
14 #include <apt-pkg/progress.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/configuration.h>
26 // OpProgress::OpProgress - Constructor /*{{{*/
27 // ---------------------------------------------------------------------
29 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
30 LastPercent(0), Percent(0)
32 memset(&LastTime
,0,sizeof(LastTime
));
35 // OpProgress::Progress - Sub progress with no state change /*{{{*/
36 // ---------------------------------------------------------------------
37 /* Current is the Base Overall progress in units of Total. Cur is the sub
38 progress in units of SubTotal. Size is a scaling factor that says what
39 percent of Total SubTotal is. */
40 void OpProgress::Progress(unsigned long Cur
)
42 if (Total
== 0 || Size
== 0 || SubTotal
== 0)
45 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
49 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
50 // ---------------------------------------------------------------------
52 void OpProgress::OverallProgress(unsigned long Current
, unsigned long Total
,
53 unsigned long Size
,const string
&Op
)
55 this->Current
= Current
;
63 Percent
= Current
*100.0/Total
;
67 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
68 // ---------------------------------------------------------------------
70 void OpProgress::SubProgress(unsigned long SubTotal
,const string
&Op
)
72 this->SubTotal
= SubTotal
;
77 Percent
= Current
*100.0/Total
;
81 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
82 // ---------------------------------------------------------------------
84 void OpProgress::SubProgress(unsigned long SubTotal
)
86 this->SubTotal
= SubTotal
;
90 Percent
= Current
*100.0/Total
;
94 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
95 // ---------------------------------------------------------------------
96 /* Progress calls are made so frequently that if every one resulted in
97 an update the display would be swamped and the system much slower.
98 This provides an upper bound on the update rate. */
99 bool OpProgress::CheckChange(float Interval
)
101 // New major progress indication
110 if (SubOp
!= LastSubOp
)
116 if ((int)LastPercent
== (int)Percent
)
119 LastPercent
= Percent
;
126 gettimeofday(&Now
,0);
127 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
134 // OpTextProgress::OpTextProgress - Constructor /*{{{*/
135 // ---------------------------------------------------------------------
137 OpTextProgress::OpTextProgress(Configuration
&Config
) :
138 NoUpdate(false), NoDisplay(false), LastLen(0)
140 if (Config
.FindI("quiet",0) >= 1)
142 if (Config
.FindI("quiet",0) >= 2)
146 // OpTextProgress::Done - Clean up the display /*{{{*/
147 // ---------------------------------------------------------------------
149 void OpTextProgress::Done()
151 if (NoUpdate
== false && OldOp
.empty() == false)
154 if (_error
->PendingError() == true)
155 snprintf(S
,sizeof(S
),_("%c%s... Error!"),'\r',OldOp
.c_str());
157 snprintf(S
,sizeof(S
),_("%c%s... Done"),'\r',OldOp
.c_str());
163 if (NoUpdate
== true && NoDisplay
== false && OldOp
.empty() == false)
170 // OpTextProgress::Update - Simple text spinner /*{{{*/
171 // ---------------------------------------------------------------------
173 void OpTextProgress::Update()
175 if (CheckChange((NoUpdate
== true?0:0.7)) == false)
178 // No percent spinner
179 if (NoUpdate
== true)
181 if (MajorChange
== false)
183 if (NoDisplay
== false)
185 if (OldOp
.empty() == false)
188 cout
<< Op
<< "..." << flush
;
194 // Erase the old text and 'log' the event
196 if (MajorChange
== true && OldOp
.empty() == false)
198 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
204 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
210 // OpTextProgress::Write - Write the progress string /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This space fills the end to overwrite the previous text */
213 void OpTextProgress::Write(const char *S
)
216 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
218 cout
<< '\r' << flush
;