]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: progress.cc,v 1.8 1998/10/02 04:39:53 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>
16 #include <apt-pkg/configuration.h>
20 // OpProgress::OpProgress - Constructor /*{{{*/
21 // ---------------------------------------------------------------------
23 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
24 LastPercent(0), Percent(0)
26 memset(&LastTime
,0,sizeof(LastTime
));
29 // OpProgress::Progress - Sub progress with no state change /*{{{*/
30 // ---------------------------------------------------------------------
31 /* Current is the Base Overall progress in units of Total. Cur is the sub
32 progress in units of SubTotal. Size is a scaling factor that says what
33 percent of Total SubTotal is. */
34 void OpProgress::Progress(unsigned long Cur
)
36 Percent
= (Current
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
;
40 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
41 // ---------------------------------------------------------------------
43 void OpProgress::OverallProgress(unsigned long Current
, unsigned long Total
,
44 unsigned long Size
,string Op
)
46 this->Current
= Current
;
51 Percent
= Current
*100.0/Total
;
55 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
56 // ---------------------------------------------------------------------
58 void OpProgress::SubProgress(unsigned long SubTotal
,string Op
)
60 this->SubTotal
= SubTotal
;
62 Percent
= Current
*100.0/Total
;
66 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
67 // ---------------------------------------------------------------------
69 void OpProgress::SubProgress(unsigned long SubTotal
)
71 this->SubTotal
= SubTotal
;
72 Percent
= Current
*100.0/Total
;
76 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
77 // ---------------------------------------------------------------------
78 /* Progress calls are made so frequently that if every one resulted in
79 an update the display would be swamped and the system much slower.
80 This provides an upper bound on the update rate. */
81 bool OpProgress::CheckChange(float Interval
)
83 // New major progress indication
92 if (SubOp
!= LastSubOp
)
98 if ((int)LastPercent
== (int)Percent
)
103 gettimeofday(&Now
,0);
104 double Diff
= Now
.tv_sec
- LastTime
.tv_sec
+ (Now
.tv_usec
- LastTime
.tv_usec
)/1000000.0;
108 LastPercent
= Percent
;
112 // OpTextProgress::OpTextProgress - Constructor /*{{{*/
113 // ---------------------------------------------------------------------
115 OpTextProgress::OpTextProgress(Configuration
&Config
) :
116 NoUpdate(false), NoDisplay(false), LastLen(0)
118 if (Config
.FindI("quiet",0) >= 1)
120 if (Config
.FindI("quiet",0) >= 2)
124 // OpTextProgress::Done - Clean up the display /*{{{*/
125 // ---------------------------------------------------------------------
127 void OpTextProgress::Done()
129 if (NoUpdate
== false && OldOp
.empty() == false)
132 if (_error
->PendingError() == true)
133 snprintf(S
,sizeof(S
),"\r%s... Error!",OldOp
.c_str());
135 snprintf(S
,sizeof(S
),"\r%s... Done",OldOp
.c_str());
141 if (NoUpdate
== true && NoDisplay
== false && OldOp
.empty() == false)
148 // OpTextProgress::Update - Simple text spinner /*{{{*/
149 // ---------------------------------------------------------------------
151 void OpTextProgress::Update()
153 if (CheckChange() == false)
156 // No percent spinner
157 if (NoUpdate
== true)
159 if (MajorChange
== false)
161 if (NoDisplay
== false)
163 if (OldOp
.empty() == false)
166 cout
<< Op
<< "..." << flush
;
172 // Erase the old text and 'log' the event
174 if (MajorChange
== true && OldOp
.empty() == false)
176 snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str());
182 snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
);
188 // OpTextProgress::Write - Write the progress string /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This space fills the end to overwrite the previous text */
191 void OpTextProgress::Write(const char *S
)
194 for (unsigned int I
= strlen(S
); I
< LastLen
; I
++)
196 cout
<< '\r' << flush
;