]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
aa5acf0105710bfe4859034f253034c3e9235bd6
[apt.git] / apt-pkg / contrib / progress.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: progress.cc,v 1.3 1998/07/26 04:49:35 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 if (_errors->PendingError() == true)
101 snprintf(S,sizeof(S),"\r%s... Error!",OldOp.c_str());
102 else
103 snprintf(S,sizeof(S),"\r%s... Done",OldOp.c_str());
104 Write(S);
105 cout << endl;
106 OldOp = string();
107 }
108 }
109 /*}}}*/
110 // OpTextProgress::Update - Simple text spinner /*{{{*/
111 // ---------------------------------------------------------------------
112 /* */
113 void OpTextProgress::Update()
114 {
115 if (CheckChange() == false)
116 return;
117
118 // No percent spinner
119 if (NoUpdate == true)
120 {
121 if (MajorChange == false)
122 return;
123 cout << Op << endl;
124 return;
125 }
126
127 // Erase the old text and 'log' the event
128 char S[300];
129 if (MajorChange == true && OldOp.empty() == false)
130 {
131 snprintf(S,sizeof(S),"\r%s",OldOp.c_str());
132 Write(S);
133 cout << endl;
134 }
135
136 // Print the spinner
137 snprintf(S,sizeof(S),"\r%s... %u%%",Op.c_str(),(unsigned int)Percent);
138 Write(S);
139
140 OldOp = Op;
141 }
142 /*}}}*/
143 // OpTextProgress::Write - Write the progress string /*{{{*/
144 // ---------------------------------------------------------------------
145 /* This space fills the end to overwrite the previous text */
146 void OpTextProgress::Write(const char *S)
147 {
148 cout << S;
149 for (unsigned int I = strlen(S); I < LastLen; I++)
150 cout << ' ';
151 cout << '\r' << flush;
152 LastLen = strlen(S);
153 }
154 /*}}}*/