]>
Commit | Line | Data |
---|---|---|
404ec98e AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
a246f2dc | 3 | // $Id: progress.cc,v 1.7 1998/09/07 05:28:38 jgg Exp $ |
404ec98e AL |
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> | |
6f4f200a | 15 | #include <apt-pkg/error.h> |
404ec98e AL |
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 | // --------------------------------------------------------------------- | |
a246f2dc AL |
30 | /* Current is the Base Overall progress in units of Total. Cur is the sub |
31 | progress in units of SubTotal. Size is a scaling factor that says what | |
32 | percent of Total SubTotal is. */ | |
404ec98e AL |
33 | void OpProgress::Progress(unsigned long Cur) |
34 | { | |
35 | Percent = (Current + Cur/((float)SubTotal)*Size)*100.0/Total; | |
36 | Update(); | |
37 | } | |
38 | /*}}}*/ | |
39 | // OpProgress::OverallProgress - Set the overall progress /*{{{*/ | |
40 | // --------------------------------------------------------------------- | |
41 | /* */ | |
42 | void OpProgress::OverallProgress(unsigned long Current, unsigned long Total, | |
43 | unsigned long Size,string Op) | |
44 | { | |
45 | this->Current = Current; | |
46 | this->Total = Total; | |
47 | this->Size = Size; | |
48 | this->Op = Op; | |
49 | SubOp = string(); | |
50 | Percent = Current*100.0/Total; | |
51 | Update(); | |
52 | } | |
53 | /*}}}*/ | |
54 | // OpProgress::SubProgress - Set the sub progress state /*{{{*/ | |
55 | // --------------------------------------------------------------------- | |
56 | /* */ | |
57 | void OpProgress::SubProgress(unsigned long SubTotal,string Op) | |
58 | { | |
59 | this->SubTotal = SubTotal; | |
60 | SubOp = Op; | |
61 | Percent = Current*100.0/Total; | |
62 | Update(); | |
63 | } | |
64 | /*}}}*/ | |
8ce4327b AL |
65 | // OpProgress::SubProgress - Set the sub progress state /*{{{*/ |
66 | // --------------------------------------------------------------------- | |
67 | /* */ | |
68 | void OpProgress::SubProgress(unsigned long SubTotal) | |
69 | { | |
70 | this->SubTotal = SubTotal; | |
71 | Percent = Current*100.0/Total; | |
72 | Update(); | |
73 | } | |
74 | /*}}}*/ | |
404ec98e AL |
75 | // OpProgress::CheckChange - See if the display should be updated /*{{{*/ |
76 | // --------------------------------------------------------------------- | |
77 | /* Progress calls are made so frequently that if every one resulted in | |
78 | an update the display would be swamped and the system much slower. | |
79 | This provides an upper bound on the update rate. */ | |
80 | bool OpProgress::CheckChange(float Interval) | |
81 | { | |
82 | // New major progress indication | |
83 | if (Op != LastOp) | |
84 | { | |
85 | MajorChange = true; | |
86 | LastOp = Op; | |
87 | return true; | |
88 | } | |
89 | MajorChange = false; | |
90 | ||
1a7c9eba AL |
91 | if (SubOp != LastSubOp) |
92 | { | |
93 | LastSubOp = SubOp; | |
94 | return true; | |
95 | } | |
96 | ||
404ec98e AL |
97 | if ((int)LastPercent == (int)Percent) |
98 | return false; | |
404ec98e AL |
99 | |
100 | // Check time delta | |
101 | struct timeval Now; | |
102 | gettimeofday(&Now,0); | |
103 | double Diff = Now.tv_sec - LastTime.tv_sec + (Now.tv_usec - LastTime.tv_usec)/1000000.0; | |
104 | if (Diff < Interval) | |
105 | return false; | |
106 | LastTime = Now; | |
1a7c9eba | 107 | LastPercent = Percent; |
404ec98e AL |
108 | return true; |
109 | } | |
110 | /*}}}*/ | |
111 | // OpTextProgress::Done - Clean up the display /*{{{*/ | |
112 | // --------------------------------------------------------------------- | |
113 | /* */ | |
114 | void OpTextProgress::Done() | |
115 | { | |
116 | if (NoUpdate == false && OldOp.empty() == false) | |
117 | { | |
118 | char S[300]; | |
6f4f200a | 119 | if (_error->PendingError() == true) |
b35d2f5f AL |
120 | snprintf(S,sizeof(S),"\r%s... Error!",OldOp.c_str()); |
121 | else | |
122 | snprintf(S,sizeof(S),"\r%s... Done",OldOp.c_str()); | |
404ec98e AL |
123 | Write(S); |
124 | cout << endl; | |
125 | OldOp = string(); | |
126 | } | |
127 | } | |
128 | /*}}}*/ | |
129 | // OpTextProgress::Update - Simple text spinner /*{{{*/ | |
130 | // --------------------------------------------------------------------- | |
131 | /* */ | |
132 | void OpTextProgress::Update() | |
133 | { | |
b35d2f5f | 134 | if (CheckChange() == false) |
404ec98e AL |
135 | return; |
136 | ||
137 | // No percent spinner | |
138 | if (NoUpdate == true) | |
139 | { | |
140 | if (MajorChange == false) | |
141 | return; | |
142 | cout << Op << endl; | |
143 | return; | |
144 | } | |
145 | ||
146 | // Erase the old text and 'log' the event | |
147 | char S[300]; | |
148 | if (MajorChange == true && OldOp.empty() == false) | |
149 | { | |
150 | snprintf(S,sizeof(S),"\r%s",OldOp.c_str()); | |
151 | Write(S); | |
152 | cout << endl; | |
153 | } | |
154 | ||
155 | // Print the spinner | |
156 | snprintf(S,sizeof(S),"\r%s... %u%%",Op.c_str(),(unsigned int)Percent); | |
157 | Write(S); | |
158 | ||
159 | OldOp = Op; | |
160 | } | |
161 | /*}}}*/ | |
162 | // OpTextProgress::Write - Write the progress string /*{{{*/ | |
163 | // --------------------------------------------------------------------- | |
164 | /* This space fills the end to overwrite the previous text */ | |
165 | void OpTextProgress::Write(const char *S) | |
166 | { | |
167 | cout << S; | |
168 | for (unsigned int I = strlen(S); I < LastLen; I++) | |
169 | cout << ' '; | |
170 | cout << '\r' << flush; | |
171 | LastLen = strlen(S); | |
172 | } | |
173 | /*}}}*/ |