]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
4be7b87b999d512659774216b69f8058b96a83ec
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: progress.cc,v 1.5 1998/08/23 03:52:22 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 /* This assumes that Size is the same as the current sub size */
31 void OpProgress::Progress(unsigned long Cur
)
33 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
37 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
38 // ---------------------------------------------------------------------
40 void OpProgress::OverallProgress(unsigned long Current
, unsigned long Total
,
41 unsigned long Size
,string Op
)
43 this->Current
= Current
;
48 Percent
= Current
*100.0/Total
;
52 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
53 // ---------------------------------------------------------------------
55 void OpProgress::SubProgress(unsigned long SubTotal
,string Op
)
57 this->SubTotal
= SubTotal
;
59 Percent
= Current
*100.0/Total
;
63 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
64 // ---------------------------------------------------------------------
65 /* Progress calls are made so frequently that if every one resulted in
66 an update the display would be swamped and the system much slower.
67 This provides an upper bound on the update rate. */
68 bool OpProgress::CheckChange(float Interval
)
70 // New major progress indication
79 if (SubOp
!= LastSubOp
)
85 if ((int)LastPercent
== (int)Percent
)
91 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
95 LastPercent
= Percent
;
99 // OpTextProgress::Done - Clean up the display /*{{{*/
100 // ---------------------------------------------------------------------
102 void OpTextProgress::Done()
104 if (NoUpdate
== false && OldOp
.empty() == false)
107 if (_error
->PendingError() == true)
108 snprintf(S
,sizeof(S
),"\r%s... Error!",OldOp
.c_str());
110 snprintf(S
,sizeof(S
),"\r%s... Done",OldOp
.c_str());
117 // OpTextProgress::Update - Simple text spinner /*{{{*/
118 // ---------------------------------------------------------------------
120 void OpTextProgress::Update()
122 if (CheckChange() == false)
125 // No percent spinner
126 if (NoUpdate
== true)
128 if (MajorChange
== false)
134 // Erase the old text and 'log' the event
136 if (MajorChange
== true && OldOp
.empty() == false)
138 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
144 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
150 // OpTextProgress::Write - Write the progress string /*{{{*/
151 // ---------------------------------------------------------------------
152 /* This space fills the end to overwrite the previous text */
153 void OpTextProgress::Write(const char *S
)
156 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
158 cout
<< '\r' << flush
;