acqprogress.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acqprogress.cc,v 1.3 1998/11/23 07:32:24 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 <stdio.h>
  13. #include <strutl.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) << ']';
  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) << ']';
  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. cout << "Err " << Itm.Description << endl;
  86. cout << " " << Itm.Owner->ErrorText << endl;
  87. Update = true;
  88. };
  89. /*}}}*/
  90. // AcqTextStatus::Stop - Finished downloading /*{{{*/
  91. // ---------------------------------------------------------------------
  92. /* This prints out the bytes downloaded and the overall average line
  93. speed */
  94. void AcqTextStatus::Stop()
  95. {
  96. pkgAcquireStatus::Stop();
  97. if (Quiet > 1)
  98. return;
  99. if (Quiet <= 0)
  100. cout << '\r' << BlankLine << '\r';
  101. if (FetchedBytes != 0)
  102. cout << "Fetched " << SizeToStr(FetchedBytes) << " in " <<
  103. TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) <<
  104. "/s)" << endl;
  105. }
  106. /*}}}*/
  107. // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* This draws the current progress. Each line has an overall percent
  110. meter and a per active item status meter along with an overall
  111. bandwidth and ETA indicator. */
  112. void AcqTextStatus::Pulse(pkgAcquire *Owner)
  113. {
  114. if (Quiet > 0)
  115. return;
  116. pkgAcquireStatus::Pulse(Owner);
  117. enum {Long = 0,Medium,Short} Mode = Long;
  118. char Buffer[300];
  119. char *End = Buffer + sizeof(Buffer);
  120. char *S = Buffer;
  121. // Put in the percent done
  122. sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems)));
  123. bool Shown = false;
  124. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  125. I = Owner->WorkerStep(I))
  126. {
  127. S += strlen(S);
  128. // There is no item running
  129. if (I->CurrentItem == 0)
  130. {
  131. if (I->Status.empty() == false)
  132. snprintf(S,End-S," [%s]",I->Status.c_str());
  133. continue;
  134. }
  135. Shown = true;
  136. // Add in the short description
  137. if (I->CurrentItem->Owner->ID != 0)
  138. snprintf(S,End-S," [%x %s",I->CurrentItem->Owner->ID,
  139. I->CurrentItem->ShortDesc.c_str());
  140. else
  141. snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
  142. S += strlen(S);
  143. // Show the short mode string
  144. if (I->CurrentItem->Owner->Mode != 0)
  145. {
  146. snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
  147. S += strlen(S);
  148. }
  149. // Add the current progress
  150. if (Mode == Long)
  151. snprintf(S,End-S," %u",I->CurrentSize);
  152. else
  153. {
  154. if (Mode == Medium || I->TotalSize == 0)
  155. snprintf(S,End-S," %s",SizeToStr(I->CurrentSize).c_str());
  156. }
  157. S += strlen(S);
  158. // Add the total size and percent
  159. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  160. {
  161. if (Mode == Short)
  162. snprintf(S,End-S," %u%%",
  163. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  164. else
  165. snprintf(S,End-S,"/%s %u%%",SizeToStr(I->TotalSize).c_str(),
  166. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  167. }
  168. S += strlen(S);
  169. snprintf(S,End-S,"]");
  170. }
  171. // Show something..
  172. if (Shown == false)
  173. snprintf(S,End-S," [Working]");
  174. // Put in the ETA and cps meter
  175. if (CurrentCPS != 0)
  176. {
  177. char Tmp[300];
  178. unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
  179. sprintf(Tmp," %s/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
  180. unsigned int Len = strlen(Buffer);
  181. unsigned int LenT = strlen(Tmp);
  182. if (Len + LenT < ScreenWidth)
  183. {
  184. memset(Buffer + Len,' ',ScreenWidth - Len);
  185. strcpy(Buffer + ScreenWidth - LenT,Tmp);
  186. }
  187. }
  188. Buffer[ScreenWidth] = 0;
  189. // Draw the current status
  190. if (strlen(Buffer) == strlen(BlankLine))
  191. cout << '\r' << Buffer << flush;
  192. else
  193. cout << '\r' << BlankLine << '\r' << Buffer << flush;
  194. memset(BlankLine,' ',strlen(Buffer));
  195. BlankLine[strlen(Buffer)] = 0;
  196. Update = false;
  197. }
  198. /*}}}*/