]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
4be7b87b999d512659774216b69f8058b96a83ec
[apt.git] / apt-pkg / contrib / progress.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: progress.cc,v 1.5 1998/08/23 03:52:22 jgg Exp $
4 /* ######################################################################
5
6 OpProgress - Operation Progress
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #ifdef __GNUG__
12 #pragma implementation "apt-pkg/progress.h"
13 #endif
14 #include <apt-pkg/progress.h>
15 #include <apt-pkg/error.h>
16 #include <stdio.h>
17 /*}}}*/
18
19 // OpProgress::OpProgress - Constructor /*{{{*/
20 // ---------------------------------------------------------------------
21 /* */
22 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
23 LastPercent(0), Percent(0)
24 {
25 memset(&LastTime,0,sizeof(LastTime));
26 }
27 /*}}}*/
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)
32 {
33 Percent = (Current + Cur/((float)SubTotal)*Size)*100.0/Total;
34 Update();
35 }
36 /*}}}*/
37 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
38 // ---------------------------------------------------------------------
39 /* */
40 void OpProgress::OverallProgress(unsigned long Current, unsigned long Total,
41 unsigned long Size,string Op)
42 {
43 this->Current = Current;
44 this->Total = Total;
45 this->Size = Size;
46 this->Op = Op;
47 SubOp = string();
48 Percent = Current*100.0/Total;
49 Update();
50 }
51 /*}}}*/
52 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
53 // ---------------------------------------------------------------------
54 /* */
55 void OpProgress::SubProgress(unsigned long SubTotal,string Op)
56 {
57 this->SubTotal = SubTotal;
58 SubOp = Op;
59 Percent = Current*100.0/Total;
60 Update();
61 }
62 /*}}}*/
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)
69 {
70 // New major progress indication
71 if (Op != LastOp)
72 {
73 MajorChange = true;
74 LastOp = Op;
75 return true;
76 }
77 MajorChange = false;
78
79 if (SubOp != LastSubOp)
80 {
81 LastSubOp = SubOp;
82 return true;
83 }
84
85 if ((int)LastPercent == (int)Percent)
86 return false;
87
88 // Check time delta
89 struct timeval Now;
90 gettimeofday(&Now,0);
91 double Diff = Now.tv_sec - LastTime.tv_sec + (Now.tv_usec - LastTime.tv_usec)/1000000.0;
92 if (Diff < Interval)
93 return false;
94 LastTime = Now;
95 LastPercent = Percent;
96 return true;
97 }
98 /*}}}*/
99 // OpTextProgress::Done - Clean up the display /*{{{*/
100 // ---------------------------------------------------------------------
101 /* */
102 void OpTextProgress::Done()
103 {
104 if (NoUpdate == false && OldOp.empty() == false)
105 {
106 char S[300];
107 if (_error->PendingError() == true)
108 snprintf(S,sizeof(S),"\r%s... Error!",OldOp.c_str());
109 else
110 snprintf(S,sizeof(S),"\r%s... Done",OldOp.c_str());
111 Write(S);
112 cout << endl;
113 OldOp = string();
114 }
115 }
116 /*}}}*/
117 // OpTextProgress::Update - Simple text spinner /*{{{*/
118 // ---------------------------------------------------------------------
119 /* */
120 void OpTextProgress::Update()
121 {
122 if (CheckChange() == false)
123 return;
124
125 // No percent spinner
126 if (NoUpdate == true)
127 {
128 if (MajorChange == false)
129 return;
130 cout << Op << endl;
131 return;
132 }
133
134 // Erase the old text and 'log' the event
135 char S[300];
136 if (MajorChange == true && OldOp.empty() == false)
137 {
138 snprintf(S,sizeof(S),"\r%s",OldOp.c_str());
139 Write(S);
140 cout << endl;
141 }
142
143 // Print the spinner
144 snprintf(S,sizeof(S),"\r%s... %u%%",Op.c_str(),(unsigned int)Percent);
145 Write(S);
146
147 OldOp = Op;
148 }
149 /*}}}*/
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)
154 {
155 cout << S;
156 for (unsigned int I = strlen(S); I < LastLen; I++)
157 cout << ' ';
158 cout << '\r' << flush;
159 LastLen = strlen(S);
160 }
161 /*}}}*/