]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
84ee4c124cf3c1c0c9c733f5a58e1aeca4e5f499
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>
24 // OpProgress::OpProgress - Constructor /*{{{*/
25 // ---------------------------------------------------------------------
27 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
28 LastPercent(0), Percent(0)
30 memset(&LastTime
,0,sizeof(LastTime
));
33 // OpProgress::Progress - Sub progress with no state change /*{{{*/
34 // ---------------------------------------------------------------------
35 /* Current is the Base Overall progress in units of Total. Cur is the sub
36 progress in units of SubTotal. Size is a scaling factor that says what
37 percent of Total SubTotal is. */
38 void OpProgress::Progress(unsigned long Cur
)
40 if (Total
== 0 || Size
== 0 || SubTotal
== 0)
43 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
47 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
48 // ---------------------------------------------------------------------
50 void OpProgress::OverallProgress(unsigned long Current
, unsigned long Total
,
51 unsigned long Size
,const string
&Op
)
53 this->Current
= Current
;
61 Percent
= Current
*100.0/Total
;
65 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
66 // ---------------------------------------------------------------------
68 void OpProgress::SubProgress(unsigned long SubTotal
,const string
&Op
,
71 this->SubTotal
= SubTotal
;
72 if (Op
.empty() == false)
74 if (Total
== 0 || Percent
== 0)
76 else if (Percent
!= -1)
77 this->Percent
= this->Current
+= (Size
*Percent
)/SubTotal
;
79 this->Percent
= Current
*100.0/Total
;
83 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
84 // ---------------------------------------------------------------------
85 /* Progress calls are made so frequently that if every one resulted in
86 an update the display would be swamped and the system much slower.
87 This provides an upper bound on the update rate. */
88 bool OpProgress::CheckChange(float Interval
)
90 // New major progress indication
99 if (SubOp
!= LastSubOp
)
105 if ((int)LastPercent
== (int)Percent
)
108 LastPercent
= Percent
;
115 gettimeofday(&Now
,0);
116 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
123 // OpTextProgress::OpTextProgress - Constructor /*{{{*/
124 // ---------------------------------------------------------------------
126 OpTextProgress::OpTextProgress(Configuration
&Config
) :
127 NoUpdate(false), NoDisplay(false), LastLen(0)
129 if (Config
.FindI("quiet",0) >= 1 || Config
.FindB("quiet::NoUpdate", false) == true)
131 if (Config
.FindI("quiet",0) >= 2)
135 // OpTextProgress::Done - Clean up the display /*{{{*/
136 // ---------------------------------------------------------------------
138 void OpTextProgress::Done()
140 if (NoUpdate
== false && OldOp
.empty() == false)
143 if (_error
->PendingError() == true)
144 snprintf(S
,sizeof(S
),_("%c%s... Error!"),'\r',OldOp
.c_str());
146 snprintf(S
,sizeof(S
),_("%c%s... Done"),'\r',OldOp
.c_str());
152 if (NoUpdate
== true && NoDisplay
== false && OldOp
.empty() == false)
159 // OpTextProgress::Update - Simple text spinner /*{{{*/
160 // ---------------------------------------------------------------------
162 void OpTextProgress::Update()
164 if (CheckChange((NoUpdate
== true?0:0.7)) == false)
167 // No percent spinner
168 if (NoUpdate
== true)
170 if (MajorChange
== false)
172 if (NoDisplay
== false)
174 if (OldOp
.empty() == false)
177 cout
<< Op
<< "..." << flush
;
183 // Erase the old text and 'log' the event
185 if (MajorChange
== true && OldOp
.empty() == false)
187 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
193 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
199 // OpTextProgress::Write - Write the progress string /*{{{*/
200 // ---------------------------------------------------------------------
201 /* This space fills the end to overwrite the previous text */
202 void OpTextProgress::Write(const char *S
)
205 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
207 cout
<< '\r' << flush
;