]>
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 /*{{{*/
11 #include <apt-pkg/progress.h>
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/configuration.h>
23 // OpProgress::OpProgress - Constructor /*{{{*/
24 // ---------------------------------------------------------------------
26 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
27 LastPercent(0), Percent(0)
29 memset(&LastTime
,0,sizeof(LastTime
));
32 // OpProgress::Progress - Sub progress with no state change /*{{{*/
33 // ---------------------------------------------------------------------
34 /* Current is the Base Overall progress in units of Total. Cur is the sub
35 progress in units of SubTotal. Size is a scaling factor that says what
36 percent of Total SubTotal is. */
37 void OpProgress::Progress(unsigned long Cur
)
39 if (Total
== 0 || Size
== 0 || SubTotal
== 0)
42 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
46 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
47 // ---------------------------------------------------------------------
49 void OpProgress::OverallProgress(unsigned long Current
, unsigned long Total
,
50 unsigned long Size
,const string
&Op
)
52 this->Current
= Current
;
60 Percent
= Current
*100.0/Total
;
64 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
65 // ---------------------------------------------------------------------
67 void OpProgress::SubProgress(unsigned long SubTotal
,const string
&Op
)
69 this->SubTotal
= SubTotal
;
74 Percent
= Current
*100.0/Total
;
78 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
79 // ---------------------------------------------------------------------
81 void OpProgress::SubProgress(unsigned long SubTotal
)
83 this->SubTotal
= SubTotal
;
87 Percent
= Current
*100.0/Total
;
91 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
92 // ---------------------------------------------------------------------
93 /* Progress calls are made so frequently that if every one resulted in
94 an update the display would be swamped and the system much slower.
95 This provides an upper bound on the update rate. */
96 bool OpProgress::CheckChange(float Interval
)
98 // New major progress indication
107 if (SubOp
!= LastSubOp
)
113 if ((int)LastPercent
== (int)Percent
)
116 LastPercent
= Percent
;
123 gettimeofday(&Now
,0);
124 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
131 // OpTextProgress::OpTextProgress - Constructor /*{{{*/
132 // ---------------------------------------------------------------------
134 OpTextProgress::OpTextProgress(Configuration
&Config
) :
135 NoUpdate(false), NoDisplay(false), LastLen(0)
137 if (Config
.FindI("quiet",0) >= 1)
139 if (Config
.FindI("quiet",0) >= 2)
143 // OpTextProgress::Done - Clean up the display /*{{{*/
144 // ---------------------------------------------------------------------
146 void OpTextProgress::Done()
148 if (NoUpdate
== false && OldOp
.empty() == false)
151 if (_error
->PendingError() == true)
152 snprintf(S
,sizeof(S
),_("%c%s... Error!"),'\r',OldOp
.c_str());
154 snprintf(S
,sizeof(S
),_("%c%s... Done"),'\r',OldOp
.c_str());
160 if (NoUpdate
== true && NoDisplay
== false && OldOp
.empty() == false)
167 // OpTextProgress::Update - Simple text spinner /*{{{*/
168 // ---------------------------------------------------------------------
170 void OpTextProgress::Update()
172 if (CheckChange((NoUpdate
== true?0:0.7)) == false)
175 // No percent spinner
176 if (NoUpdate
== true)
178 if (MajorChange
== false)
180 if (NoDisplay
== false)
182 if (OldOp
.empty() == false)
185 cout
<< Op
<< "..." << flush
;
191 // Erase the old text and 'log' the event
193 if (MajorChange
== true && OldOp
.empty() == false)
195 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
201 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
207 // OpTextProgress::Write - Write the progress string /*{{{*/
208 // ---------------------------------------------------------------------
209 /* This space fills the end to overwrite the previous text */
210 void OpTextProgress::Write(const char *S
)
213 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
215 cout
<< '\r' << flush
;