]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
9a29c4b662285ec6cff4f204589abc3b3d635352
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: progress.cc,v 1.7 1998/09/07 05:28:38 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>
19 // OpProgress::OpProgress - Constructor /*{{{*/
20 // ---------------------------------------------------------------------
22 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
23 LastPercent(0), Percent(0)
25 memset(&LastTime
,0,sizeof(LastTime
));
28 // OpProgress::Progress - Sub progress with no state change /*{{{*/
29 // ---------------------------------------------------------------------
30 /* Current is the Base Overall progress in units of Total. Cur is the sub
31 progress in units of SubTotal. Size is a scaling factor that says what
32 percent of Total SubTotal is. */
33 void OpProgress::Progress(unsigned long Cur
)
35 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
39 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
40 // ---------------------------------------------------------------------
42 void OpProgress::OverallProgress(unsigned long Current
, unsigned long Total
,
43 unsigned long Size
,string Op
)
45 this->Current
= Current
;
50 Percent
= Current
*100.0/Total
;
54 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
55 // ---------------------------------------------------------------------
57 void OpProgress::SubProgress(unsigned long SubTotal
,string Op
)
59 this->SubTotal
= SubTotal
;
61 Percent
= Current
*100.0/Total
;
65 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
66 // ---------------------------------------------------------------------
68 void OpProgress::SubProgress(unsigned long SubTotal
)
70 this->SubTotal
= SubTotal
;
71 Percent
= Current
*100.0/Total
;
75 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
76 // ---------------------------------------------------------------------
77 /* Progress calls are made so frequently that if every one resulted in
78 an update the display would be swamped and the system much slower.
79 This provides an upper bound on the update rate. */
80 bool OpProgress::CheckChange(float Interval
)
82 // New major progress indication
91 if (SubOp
!= LastSubOp
)
97 if ((int)LastPercent
== (int)Percent
)
102 gettimeofday(&Now
,0);
103 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
107 LastPercent
= Percent
;
111 // OpTextProgress::Done - Clean up the display /*{{{*/
112 // ---------------------------------------------------------------------
114 void OpTextProgress::Done()
116 if (NoUpdate
== false && OldOp
.empty() == false)
119 if (_error
->PendingError() == true)
120 snprintf(S
,sizeof(S
),"\r%s... Error!",OldOp
.c_str());
122 snprintf(S
,sizeof(S
),"\r%s... Done",OldOp
.c_str());
129 // OpTextProgress::Update - Simple text spinner /*{{{*/
130 // ---------------------------------------------------------------------
132 void OpTextProgress::Update()
134 if (CheckChange() == false)
137 // No percent spinner
138 if (NoUpdate
== true)
140 if (MajorChange
== false)
146 // Erase the old text and 'log' the event
148 if (MajorChange
== true && OldOp
.empty() == false)
150 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
156 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
162 // OpTextProgress::Write - Write the progress string /*{{{*/
163 // ---------------------------------------------------------------------
164 /* This space fills the end to overwrite the previous text */
165 void OpTextProgress::Write(const char *S
)
168 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
170 cout
<< '\r' << flush
;