]>
Commit | Line | Data |
---|---|---|
0919e3f9 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
d568ed2d | 3 | // $Id: acqprogress.cc,v 1.3 1998/11/23 07:32:24 jgg Exp $ |
0919e3f9 AL |
4 | /* ###################################################################### |
5 | ||
6 | Acquire Progress - Command line progress meter | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include files /*{{{*/ | |
11 | #include "acqprogress.h" | |
12 | #include <apt-pkg/acquire-item.h> | |
13 | #include <apt-pkg/acquire-worker.h> | |
14 | #include <stdio.h> | |
15 | #include <strutl.h> | |
16 | /*}}}*/ | |
17 | ||
18 | // AcqTextStatus::AcqTextStatus - Constructor /*{{{*/ | |
19 | // --------------------------------------------------------------------- | |
20 | /* */ | |
d7827aca | 21 | AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int Quiet) : |
0919e3f9 AL |
22 | ScreenWidth(ScreenWidth), Quiet(Quiet) |
23 | { | |
24 | } | |
25 | /*}}}*/ | |
26 | // AcqTextStatus::Start - Downloading has started /*{{{*/ | |
27 | // --------------------------------------------------------------------- | |
28 | /* */ | |
29 | void AcqTextStatus::Start() | |
30 | { | |
31 | pkgAcquireStatus::Start(); | |
32 | BlankLine[0] = 0; | |
33 | ID = 1; | |
34 | }; | |
35 | /*}}}*/ | |
36 | // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/ | |
37 | // --------------------------------------------------------------------- | |
38 | /* */ | |
39 | void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm) | |
40 | { | |
41 | if (Quiet > 1) | |
42 | return; | |
43 | ||
44 | if (Quiet <= 0) | |
45 | cout << '\r' << BlankLine << '\r'; | |
46 | ||
47 | cout << "Hit " << Itm.Description; | |
48 | if (Itm.Owner->FileSize != 0) | |
49 | cout << " [" << SizeToStr(Itm.Owner->FileSize) << ']'; | |
50 | cout << endl; | |
51 | Update = true; | |
52 | }; | |
53 | /*}}}*/ | |
54 | // AcqTextStatus::Fetch - An item has started to download /*{{{*/ | |
55 | // --------------------------------------------------------------------- | |
56 | /* This prints out the short description and the expected size */ | |
57 | void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm) | |
58 | { | |
59 | Update = true; | |
60 | if (Itm.Owner->Complete == true) | |
61 | return; | |
62 | ||
63 | Itm.Owner->ID = ID++; | |
64 | ||
65 | if (Quiet > 1) | |
66 | return; | |
67 | ||
68 | if (Quiet <= 0) | |
69 | cout << '\r' << BlankLine << '\r'; | |
70 | ||
71 | cout << "Get:" << hex << Itm.Owner->ID << dec << ' ' << Itm.Description; | |
72 | if (Itm.Owner->FileSize != 0) | |
73 | cout << " [" << SizeToStr(Itm.Owner->FileSize) << ']'; | |
74 | cout << endl; | |
75 | }; | |
76 | /*}}}*/ | |
77 | // AcqTextStatus::Done - Completed a download /*{{{*/ | |
78 | // --------------------------------------------------------------------- | |
79 | /* We don't display anything... */ | |
80 | void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm) | |
81 | { | |
82 | Update = true; | |
83 | }; | |
84 | /*}}}*/ | |
85 | // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/ | |
86 | // --------------------------------------------------------------------- | |
87 | /* We print out the error text */ | |
88 | void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm) | |
89 | { | |
90 | if (Quiet > 1) | |
91 | return; | |
92 | ||
93 | if (Quiet <= 0) | |
94 | cout << '\r' << BlankLine << '\r'; | |
95 | ||
96 | cout << "Err " << Itm.Description << endl; | |
97 | cout << " " << Itm.Owner->ErrorText << endl; | |
98 | Update = true; | |
99 | }; | |
100 | /*}}}*/ | |
101 | // AcqTextStatus::Stop - Finished downloading /*{{{*/ | |
102 | // --------------------------------------------------------------------- | |
103 | /* This prints out the bytes downloaded and the overall average line | |
104 | speed */ | |
105 | void AcqTextStatus::Stop() | |
106 | { | |
107 | pkgAcquireStatus::Stop(); | |
108 | if (Quiet > 1) | |
109 | return; | |
110 | ||
111 | if (Quiet <= 0) | |
112 | cout << '\r' << BlankLine << '\r'; | |
113 | ||
114 | if (FetchedBytes != 0) | |
115 | cout << "Fetched " << SizeToStr(FetchedBytes) << " in " << | |
116 | TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) << | |
117 | "/s)" << endl; | |
118 | } | |
119 | /*}}}*/ | |
120 | // AcqTextStatus::Pulse - Regular event pulse /*{{{*/ | |
121 | // --------------------------------------------------------------------- | |
122 | /* This draws the current progress. Each line has an overall percent | |
123 | meter and a per active item status meter along with an overall | |
124 | bandwidth and ETA indicator. */ | |
125 | void AcqTextStatus::Pulse(pkgAcquire *Owner) | |
126 | { | |
127 | if (Quiet > 0) | |
128 | return; | |
129 | ||
130 | pkgAcquireStatus::Pulse(Owner); | |
131 | ||
132 | enum {Long = 0,Medium,Short} Mode = Long; | |
133 | ||
134 | char Buffer[300]; | |
135 | char *End = Buffer + sizeof(Buffer); | |
136 | char *S = Buffer; | |
137 | ||
138 | // Put in the percent done | |
d568ed2d AL |
139 | sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems))); |
140 | ||
141 | bool Shown = false; | |
0919e3f9 AL |
142 | for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0; |
143 | I = Owner->WorkerStep(I)) | |
144 | { | |
145 | S += strlen(S); | |
146 | ||
147 | // There is no item running | |
148 | if (I->CurrentItem == 0) | |
149 | { | |
150 | if (I->Status.empty() == false) | |
151 | snprintf(S,End-S," [%s]",I->Status.c_str()); | |
152 | continue; | |
153 | } | |
154 | ||
d568ed2d AL |
155 | Shown = true; |
156 | ||
0919e3f9 AL |
157 | // Add in the short description |
158 | if (I->CurrentItem->Owner->ID != 0) | |
159 | snprintf(S,End-S," [%x %s",I->CurrentItem->Owner->ID, | |
160 | I->CurrentItem->ShortDesc.c_str()); | |
161 | else | |
162 | snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str()); | |
163 | S += strlen(S); | |
164 | ||
165 | // Show the short mode string | |
166 | if (I->CurrentItem->Owner->Mode != 0) | |
167 | { | |
168 | snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode); | |
169 | S += strlen(S); | |
170 | } | |
171 | ||
172 | // Add the current progress | |
173 | if (Mode == Long) | |
174 | snprintf(S,End-S," %u",I->CurrentSize); | |
175 | else | |
176 | { | |
177 | if (Mode == Medium || I->TotalSize == 0) | |
178 | snprintf(S,End-S," %s",SizeToStr(I->CurrentSize).c_str()); | |
179 | } | |
180 | S += strlen(S); | |
181 | ||
182 | // Add the total size and percent | |
183 | if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false) | |
184 | { | |
185 | if (Mode == Short) | |
186 | snprintf(S,End-S," %u%%", | |
187 | long(double(I->CurrentSize*100.0)/double(I->TotalSize))); | |
188 | else | |
189 | snprintf(S,End-S,"/%s %u%%",SizeToStr(I->TotalSize).c_str(), | |
190 | long(double(I->CurrentSize*100.0)/double(I->TotalSize))); | |
191 | } | |
192 | S += strlen(S); | |
193 | snprintf(S,End-S,"]"); | |
194 | } | |
195 | ||
d568ed2d AL |
196 | // Show something.. |
197 | if (Shown == false) | |
198 | snprintf(S,End-S," [Working]"); | |
199 | ||
0919e3f9 AL |
200 | // Put in the ETA and cps meter |
201 | if (CurrentCPS != 0) | |
202 | { | |
203 | char Tmp[300]; | |
204 | unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS); | |
205 | sprintf(Tmp," %s/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str()); | |
206 | unsigned int Len = strlen(Buffer); | |
207 | unsigned int LenT = strlen(Tmp); | |
208 | if (Len + LenT < ScreenWidth) | |
209 | { | |
210 | memset(Buffer + Len,' ',ScreenWidth - Len); | |
211 | strcpy(Buffer + ScreenWidth - LenT,Tmp); | |
212 | } | |
213 | } | |
214 | Buffer[ScreenWidth] = 0; | |
215 | ||
216 | // Draw the current status | |
217 | if (strlen(Buffer) == strlen(BlankLine)) | |
218 | cout << '\r' << Buffer << flush; | |
219 | else | |
220 | cout << '\r' << BlankLine << '\r' << Buffer << flush; | |
221 | memset(BlankLine,' ',strlen(Buffer)); | |
222 | BlankLine[strlen(Buffer)] = 0; | |
223 | ||
224 | Update = false; | |
225 | } | |
226 | /*}}}*/ |