]>
git.saurik.com Git - apt.git/blob - test/scratch.cc
1 #include <apt-pkg/acquire-item.h>
2 #include <apt-pkg/acquire-worker.h>
3 #include <apt-pkg/init.h>
4 #include <apt-pkg/error.h>
10 class AcqTextStatus
: public pkgAcquireStatus
12 unsigned int ScreenWidth
;
18 virtual void IMSHit(pkgAcquire::ItemDesc
&Itm
);
19 virtual void Fetch(pkgAcquire::ItemDesc
&Itm
);
20 virtual void Done(pkgAcquire::ItemDesc
&Itm
);
21 virtual void Fail(pkgAcquire::ItemDesc
&Itm
);
22 virtual void Start() {pkgAcquireStatus::Start(); BlankLine
[0] = 0; ID
= 1;};
25 void Pulse(pkgAcquire
*Owner
);
28 // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
29 // ---------------------------------------------------------------------
31 void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc
&Itm
)
33 cout
<< '\r' << BlankLine
<< '\r';
34 cout
<< "Hit " << Itm
.Description
;
35 if (Itm
.Owner
->FileSize
!= 0)
36 cout
<< " [" << SizeToStr(Itm
.Owner
->FileSize
) << ']';
41 // AcqTextStatus::Fetch - An item has started to download /*{{{*/
42 // ---------------------------------------------------------------------
43 /* This prints out the short description and the expected size */
44 void AcqTextStatus::Fetch(pkgAcquire::ItemDesc
&Itm
)
47 if (Itm
.Owner
->Complete
== true)
52 cout
<< '\r' << BlankLine
<< '\r';
53 cout
<< hex
<< Itm
.Owner
->ID
<< dec
<< " Get " << Itm
.Description
;
54 if (Itm
.Owner
->FileSize
!= 0)
55 cout
<< " [" << SizeToStr(Itm
.Owner
->FileSize
) << ']';
59 // AcqTextStatus::Done - Completed a download /*{{{*/
60 // ---------------------------------------------------------------------
61 /* We don't display anything... */
62 void AcqTextStatus::Done(pkgAcquire::ItemDesc
&Itm
)
67 // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
68 // ---------------------------------------------------------------------
69 /* We print out the error text */
70 void AcqTextStatus::Fail(pkgAcquire::ItemDesc
&Itm
)
72 cout
<< '\r' << BlankLine
<< '\r';
73 cout
<< "Err " << Itm
.Description
<< endl
;
74 cout
<< " " << Itm
.Owner
->ErrorText
<< endl
;
78 // AcqTextStatus::Stop - Finished downloading /*{{{*/
79 // ---------------------------------------------------------------------
80 /* This prints out the bytes downloaded and the overall average line
82 void AcqTextStatus::Stop()
84 pkgAcquireStatus::Stop();
85 cout
<< '\r' << BlankLine
<< '\r';
87 if (FetchedBytes
== 0)
90 cout
<< "Fetched " << SizeToStr(FetchedBytes
) << " in " <<
91 TimeToStr(ElapsedTime
) << " (" << SizeToStr(CurrentCPS
) <<
95 // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
96 // ---------------------------------------------------------------------
97 /* This draws the current progress. Each line has an overall percent
98 meter and a per active item status meter along with an overall
99 bandwidth and ETA indicator. */
100 void AcqTextStatus::Pulse(pkgAcquire
*Owner
)
102 pkgAcquireStatus::Pulse(Owner
);
104 enum {Long
= 0,Medium
,Short
} Mode
= Long
;
108 char *End
= Buffer
+ sizeof(Buffer
);
111 // Put in the percent done
112 sprintf(S
,"%ld%%",long(double(CurrentBytes
*100.0)/double(TotalBytes
)));
114 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
115 I
= Owner
->WorkerStep(I
))
119 // There is no item running
120 if (I
->CurrentItem
== 0)
122 if (I
->Status
.empty() == false)
123 snprintf(S
,End
-S
," [%s]",I
->Status
.c_str());
127 // Add in the short description
128 if (I
->CurrentItem
->Owner
->ID
!= 0)
129 snprintf(S
,End
-S
," [%x %s",I
->CurrentItem
->Owner
->ID
,
130 I
->CurrentItem
->ShortDesc
.c_str());
132 snprintf(S
,End
-S
," [%s",I
->CurrentItem
->ShortDesc
.c_str());
135 // Show the short mode string
136 if (I
->CurrentItem
->Owner
->Mode
!= 0)
138 snprintf(S
,End
-S
," %s",I
->CurrentItem
->Owner
->Mode
);
142 // Add the current progress
144 snprintf(S
,End
-S
," %u",I
->CurrentSize
);
147 if (Mode
== Medium
|| I
->TotalSize
== 0)
148 snprintf(S
,End
-S
," %s",SizeToStr(I
->CurrentSize
).c_str());
152 // Add the total size and percent
153 if (I
->TotalSize
> 0 && I
->CurrentItem
->Owner
->Complete
== false)
156 snprintf(S
,End
-S
,"/%u %u%%",I
->TotalSize
,
157 long(double(I
->CurrentSize
*100.0)/double(I
->TotalSize
)));
161 snprintf(S
,End
-S
,"/%s %u%%",SizeToStr(I
->TotalSize
).c_str(),
162 long(double(I
->CurrentSize
*100.0)/double(I
->TotalSize
)));
164 snprintf(S
,End
-S
," %u%%",
165 long(double(I
->CurrentSize
*100.0)/double(I
->TotalSize
)));
169 snprintf(S
,End
-S
,"]");
172 // Put in the ETA and cps meter
176 unsigned long ETA
= (unsigned long)((TotalBytes
- CurrentBytes
)/CurrentCPS
);
177 sprintf(Tmp
," %s/s %s",SizeToStr(CurrentCPS
).c_str(),TimeToStr(ETA
).c_str());
178 unsigned int Len
= strlen(Buffer
);
179 unsigned int LenT
= strlen(Tmp
);
180 if (Len
+ LenT
< ScreenWidth
)
182 memset(Buffer
+ Len
,' ',ScreenWidth
- Len
);
183 strcpy(Buffer
+ ScreenWidth
- LenT
,Tmp
);
186 Buffer
[ScreenWidth
] = 0;
188 // Draw the current status
189 if (strlen(Buffer
) == strlen(BlankLine
))
190 cout
<< '\r' << Buffer
<< flush
;
192 cout
<< '\r' << BlankLine
<< '\r' << Buffer
<< flush
;
193 memset(BlankLine
,' ',strlen(Buffer
));
194 BlankLine
[strlen(Buffer
)] = 0;
200 int main(int argc
,char *argv
[])
202 signal(SIGPIPE
,SIG_IGN
);
205 cout << Foo.Access << '\'' << endl;
206 cout << Foo.Host << '\'' << endl;
207 cout << Foo.Path << '\'' << endl;
208 cout << Foo.User << '\'' << endl;
209 cout << Foo.Password << '\'' << endl;
210 cout << Foo.Port << endl;
214 pkgInitialize(*_config
);
218 pkgAcquire
Fetcher(&Stat
);
221 pkgSourceList::const_iterator I
;
222 for (I
= List
.begin(); I
!= List
.end(); I
++)
224 new pkgAcqIndex(&Fetcher
,I
);
225 if (_error
->PendingError() == true)
231 _error
->DumpErrors();