]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
aa5acf0105710bfe4859034f253034c3e9235bd6
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: progress.cc,v 1.3 1998/07/26 04:49:35 jgg Exp $
4 /* ######################################################################
6 OpProgress - Operation Progress
8 ##################################################################### */
10 // Include Files /*{{{*/
12 #pragma implementation "apt-pkg/progress.h"
14 #include <apt-pkg/progress.h>
18 // OpProgress::OpProgress - Constructor /*{{{*/
19 // ---------------------------------------------------------------------
21 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
22 LastPercent(0), Percent(0)
24 memset(&LastTime
,0,sizeof(LastTime
));
27 // OpProgress::Progress - Sub progress with no state change /*{{{*/
28 // ---------------------------------------------------------------------
29 /* This assumes that Size is the same as the current sub size */
30 void OpProgress::Progress(unsigned long Cur
)
32 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
36 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
37 // ---------------------------------------------------------------------
39 void OpProgress::OverallProgress(unsigned long Current
, unsigned long Total
,
40 unsigned long Size
,string Op
)
42 this->Current
= Current
;
47 Percent
= Current
*100.0/Total
;
51 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
52 // ---------------------------------------------------------------------
54 void OpProgress::SubProgress(unsigned long SubTotal
,string Op
)
56 this->SubTotal
= SubTotal
;
58 Percent
= Current
*100.0/Total
;
62 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
63 // ---------------------------------------------------------------------
64 /* Progress calls are made so frequently that if every one resulted in
65 an update the display would be swamped and the system much slower.
66 This provides an upper bound on the update rate. */
67 bool OpProgress::CheckChange(float Interval
)
69 // New major progress indication
78 if ((int)LastPercent
== (int)Percent
)
80 LastPercent
= Percent
;
85 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
92 // OpTextProgress::Done - Clean up the display /*{{{*/
93 // ---------------------------------------------------------------------
95 void OpTextProgress::Done()
97 if (NoUpdate
== false && OldOp
.empty() == false)
100 if (_errors
->PendingError() == true)
101 snprintf(S
,sizeof(S
),"\r%s... Error!",OldOp
.c_str());
103 snprintf(S
,sizeof(S
),"\r%s... Done",OldOp
.c_str());
110 // OpTextProgress::Update - Simple text spinner /*{{{*/
111 // ---------------------------------------------------------------------
113 void OpTextProgress::Update()
115 if (CheckChange() == false)
118 // No percent spinner
119 if (NoUpdate
== true)
121 if (MajorChange
== false)
127 // Erase the old text and 'log' the event
129 if (MajorChange
== true && OldOp
.empty() == false)
131 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
137 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
143 // OpTextProgress::Write - Write the progress string /*{{{*/
144 // ---------------------------------------------------------------------
145 /* This space fills the end to overwrite the previous text */
146 void OpTextProgress::Write(const char *S
)
149 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
151 cout
<< '\r' << flush
;