]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
dfa978485861322d131528b38a3cc481c6a5cb16
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: progress.cc,v 1.9 2000/06/05 04:22:25 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>
20 // OpProgress::OpProgress - Constructor /*{{{*/
21 // ---------------------------------------------------------------------
23 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
24 LastPercent(0), Percent(0)
26 memset(&LastTime
,0,sizeof(LastTime
));
29 // OpProgress::Progress - Sub progress with no state change /*{{{*/
30 // ---------------------------------------------------------------------
31 /* Current is the Base Overall progress in units of Total. Cur is the sub
32 progress in units of SubTotal. Size is a scaling factor that says what
33 percent of Total SubTotal is. */
34 void OpProgress::Progress(unsigned long Cur
)
36 if (Total
== 0 || Size
== 0 || SubTotal
== 0)
39 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
43 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
44 // ---------------------------------------------------------------------
46 void OpProgress::OverallProgress(unsigned long Current
, unsigned long Total
,
47 unsigned long Size
,string Op
)
49 this->Current
= Current
;
57 Percent
= Current
*100.0/Total
;
61 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
62 // ---------------------------------------------------------------------
64 void OpProgress::SubProgress(unsigned long SubTotal
,string Op
)
66 this->SubTotal
= SubTotal
;
71 Percent
= Current
*100.0/Total
;
75 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
76 // ---------------------------------------------------------------------
78 void OpProgress::SubProgress(unsigned long SubTotal
)
80 this->SubTotal
= SubTotal
;
84 Percent
= Current
*100.0/Total
;
88 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
89 // ---------------------------------------------------------------------
90 /* Progress calls are made so frequently that if every one resulted in
91 an update the display would be swamped and the system much slower.
92 This provides an upper bound on the update rate. */
93 bool OpProgress::CheckChange(float Interval
)
95 // New major progress indication
104 if (SubOp
!= LastSubOp
)
110 if ((int)LastPercent
== (int)Percent
)
115 gettimeofday(&Now
,0);
116 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
120 LastPercent
= Percent
;
124 // OpTextProgress::OpTextProgress - Constructor /*{{{*/
125 // ---------------------------------------------------------------------
127 OpTextProgress::OpTextProgress(Configuration
&Config
) :
128 NoUpdate(false), NoDisplay(false), LastLen(0)
130 if (Config
.FindI("quiet",0) >= 1)
132 if (Config
.FindI("quiet",0) >= 2)
136 // OpTextProgress::Done - Clean up the display /*{{{*/
137 // ---------------------------------------------------------------------
139 void OpTextProgress::Done()
141 if (NoUpdate
== false && OldOp
.empty() == false)
144 if (_error
->PendingError() == true)
145 snprintf(S
,sizeof(S
),"\r%s... Error!",OldOp
.c_str());
147 snprintf(S
,sizeof(S
),"\r%s... Done",OldOp
.c_str());
153 if (NoUpdate
== true && NoDisplay
== false && OldOp
.empty() == false)
160 // OpTextProgress::Update - Simple text spinner /*{{{*/
161 // ---------------------------------------------------------------------
163 void OpTextProgress::Update()
165 if (CheckChange() == false)
168 // No percent spinner
169 if (NoUpdate
== true)
171 if (MajorChange
== false)
173 if (NoDisplay
== false)
175 if (OldOp
.empty() == false)
178 cout
<< Op
<< "..." << flush
;
184 // Erase the old text and 'log' the event
186 if (MajorChange
== true && OldOp
.empty() == false)
188 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
194 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
200 // OpTextProgress::Write - Write the progress string /*{{{*/
201 // ---------------------------------------------------------------------
202 /* This space fills the end to overwrite the previous text */
203 void OpTextProgress::Write(const char *S
)
206 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
208 cout
<< '\r' << flush
;