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