acqprogress.cc 7.2 KB

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