]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
Package Record parser
[apt.git] / apt-pkg / contrib / progress.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: progress.cc,v 1.4 1998/07/26 04:51:45 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 ((int)LastPercent == (int)Percent)
80 return false;
81 LastPercent = Percent;
82
83 // Check time delta
84 struct timeval Now;
85 gettimeofday(&Now,0);
86 double Diff = Now.tv_sec - LastTime.tv_sec + (Now.tv_usec - LastTime.tv_usec)/1000000.0;
87 if (Diff < Interval)
88 return false;
89 LastTime = Now;
90 return true;
91 }
92 /*}}}*/
93 // OpTextProgress::Done - Clean up the display /*{{{*/
94 // ---------------------------------------------------------------------
95 /* */
96 void OpTextProgress::Done()
97 {
98 if (NoUpdate == false && OldOp.empty() == false)
99 {
100 char S[300];
101 if (_error->PendingError() == true)
102 snprintf(S,sizeof(S),"\r%s... Error!",OldOp.c_str());
103 else
104 snprintf(S,sizeof(S),"\r%s... Done",OldOp.c_str());
105 Write(S);
106 cout << endl;
107 OldOp = string();
108 }
109 }
110 /*}}}*/
111 // OpTextProgress::Update - Simple text spinner /*{{{*/
112 // ---------------------------------------------------------------------
113 /* */
114 void OpTextProgress::Update()
115 {
116 if (CheckChange() == false)
117 return;
118
119 // No percent spinner
120 if (NoUpdate == true)
121 {
122 if (MajorChange == false)
123 return;
124 cout << Op << endl;
125 return;
126 }
127
128 // Erase the old text and 'log' the event
129 char S[300];
130 if (MajorChange == true && OldOp.empty() == false)
131 {
132 snprintf(S,sizeof(S),"\r%s",OldOp.c_str());
133 Write(S);
134 cout << endl;
135 }
136
137 // Print the spinner
138 snprintf(S,sizeof(S),"\r%s... %u%%",Op.c_str(),(unsigned int)Percent);
139 Write(S);
140
141 OldOp = Op;
142 }
143 /*}}}*/
144 // OpTextProgress::Write - Write the progress string /*{{{*/
145 // ---------------------------------------------------------------------
146 /* This space fills the end to overwrite the previous text */
147 void OpTextProgress::Write(const char *S)
148 {
149 cout << S;
150 for (unsigned int I = strlen(S); I < LastLen; I++)
151 cout << ' ';
152 cout << '\r' << flush;
153 LastLen = strlen(S);
154 }
155 /*}}}*/