]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
8d49eff2e1093e982c60e830e647d60f8a89f6aa
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: progress.cc,v 1.2 1998/07/22 01:45: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>
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 snprintf(S
,sizeof(S
),"\r%s... Done",OldOp
.c_str());
107 // OpTextProgress::Update - Simple text spinner /*{{{*/
108 // ---------------------------------------------------------------------
110 void OpTextProgress::Update()
112 if (CheckChange(0) == false)
115 // No percent spinner
116 if (NoUpdate
== true)
118 if (MajorChange
== false)
124 // Erase the old text and 'log' the event
126 if (MajorChange
== true && OldOp
.empty() == false)
128 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
134 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
140 // OpTextProgress::Write - Write the progress string /*{{{*/
141 // ---------------------------------------------------------------------
142 /* This space fills the end to overwrite the previous text */
143 void OpTextProgress::Write(const char *S
)
146 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
148 cout
<< '\r' << flush
;