]>
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 /*{{{*/
13 #include <apt-pkg/progress.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/configuration.h>
28 // OpProgress::OpProgress - Constructor /*{{{*/
29 // ---------------------------------------------------------------------
31 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
32 LastPercent(0), Percent(0)
34 memset(&LastTime
,0,sizeof(LastTime
));
37 // OpProgress::Progress - Sub progress with no state change /*{{{*/
38 // ---------------------------------------------------------------------
39 /* Current is the Base Overall progress in units of Total. Cur is the sub
40 progress in units of SubTotal. Size is a scaling factor that says what
41 percent of Total SubTotal is. */
42 void OpProgress::Progress(unsigned long long Cur
)
44 if (Total
== 0 || Size
== 0 || SubTotal
== 0)
47 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
51 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
52 // ---------------------------------------------------------------------
54 void OpProgress::OverallProgress(unsigned long long Current
, unsigned long long Total
,
55 unsigned long long Size
,const string
&Op
)
57 this->Current
= Current
;
65 Percent
= Current
*100.0/Total
;
69 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
70 // ---------------------------------------------------------------------
72 void OpProgress::SubProgress(unsigned long long SubTotal
,const string
&Op
,
75 this->SubTotal
= SubTotal
;
76 if (Op
.empty() == false)
78 if (Total
== 0 || Percent
== 0)
80 else if (Percent
!= -1)
81 this->Percent
= this->Current
+= (Size
*Percent
)/SubTotal
;
83 this->Percent
= Current
*100.0/Total
;
87 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
88 // ---------------------------------------------------------------------
89 /* Progress calls are made so frequently that if every one resulted in
90 an update the display would be swamped and the system much slower.
91 This provides an upper bound on the update rate. */
92 bool OpProgress::CheckChange(float Interval
)
94 // New major progress indication
103 if (SubOp
!= LastSubOp
)
109 if ((int)LastPercent
== (int)Percent
)
112 LastPercent
= Percent
;
119 gettimeofday(&Now
,0);
120 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
127 // OpTextProgress::OpTextProgress - Constructor /*{{{*/
128 // ---------------------------------------------------------------------
130 OpTextProgress::OpTextProgress(Configuration
&Config
) :
131 NoUpdate(false), NoDisplay(false), LastLen(0)
133 if (Config
.FindI("quiet",0) >= 1 || Config
.FindB("quiet::NoUpdate", false) == true)
135 if (Config
.FindI("quiet",0) >= 2)
139 // OpTextProgress::Done - Clean up the display /*{{{*/
140 // ---------------------------------------------------------------------
142 void OpTextProgress::Done()
144 if (NoUpdate
== false && OldOp
.empty() == false)
147 if (_error
->PendingError() == true)
148 snprintf(S
,sizeof(S
),_("%c%s... Error!"),'\r',OldOp
.c_str());
150 snprintf(S
,sizeof(S
),_("%c%s... Done"),'\r',OldOp
.c_str());
156 if (NoUpdate
== true && NoDisplay
== false && OldOp
.empty() == false)
163 // OpTextProgress::Update - Simple text spinner /*{{{*/
164 // ---------------------------------------------------------------------
166 void OpTextProgress::Update()
168 if (CheckChange((NoUpdate
== true?0:0.7)) == false)
171 // No percent spinner
172 if (NoUpdate
== true)
174 if (MajorChange
== false)
176 if (NoDisplay
== false)
178 if (OldOp
.empty() == false)
181 cout
<< Op
<< _("...") << flush
;
187 // Erase the old text and 'log' the event
189 if (MajorChange
== true && OldOp
.empty() == false)
191 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
197 snprintf(S
,sizeof(S
),_("%c%s... %u%%"),'\r',Op
.c_str(),(unsigned int)Percent
);
203 // OpTextProgress::Write - Write the progress string /*{{{*/
204 // ---------------------------------------------------------------------
205 /* This space fills the end to overwrite the previous text */
206 void OpTextProgress::Write(const char *S
)
209 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
211 cout
<< '\r' << flush
;