acqprogress.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acqprogress.cc,v 1.2 1998/11/12 05:30:07 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*100.0)/double(TotalBytes)));
  123. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  124. I = Owner->WorkerStep(I))
  125. {
  126. S += strlen(S);
  127. // There is no item running
  128. if (I->CurrentItem == 0)
  129. {
  130. if (I->Status.empty() == false)
  131. snprintf(S,End-S," [%s]",I->Status.c_str());
  132. continue;
  133. }
  134. // Add in the short description
  135. if (I->CurrentItem->Owner->ID != 0)
  136. snprintf(S,End-S," [%x %s",I->CurrentItem->Owner->ID,
  137. I->CurrentItem->ShortDesc.c_str());
  138. else
  139. snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
  140. S += strlen(S);
  141. // Show the short mode string
  142. if (I->CurrentItem->Owner->Mode != 0)
  143. {
  144. snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
  145. S += strlen(S);
  146. }
  147. // Add the current progress
  148. if (Mode == Long)
  149. snprintf(S,End-S," %u",I->CurrentSize);
  150. else
  151. {
  152. if (Mode == Medium || I->TotalSize == 0)
  153. snprintf(S,End-S," %s",SizeToStr(I->CurrentSize).c_str());
  154. }
  155. S += strlen(S);
  156. // Add the total size and percent
  157. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  158. {
  159. if (Mode == Short)
  160. snprintf(S,End-S," %u%%",
  161. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  162. else
  163. snprintf(S,End-S,"/%s %u%%",SizeToStr(I->TotalSize).c_str(),
  164. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  165. }
  166. S += strlen(S);
  167. snprintf(S,End-S,"]");
  168. }
  169. // Put in the ETA and cps meter
  170. if (CurrentCPS != 0)
  171. {
  172. char Tmp[300];
  173. unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
  174. sprintf(Tmp," %s/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
  175. unsigned int Len = strlen(Buffer);
  176. unsigned int LenT = strlen(Tmp);
  177. if (Len + LenT < ScreenWidth)
  178. {
  179. memset(Buffer + Len,' ',ScreenWidth - Len);
  180. strcpy(Buffer + ScreenWidth - LenT,Tmp);
  181. }
  182. }
  183. Buffer[ScreenWidth] = 0;
  184. // Draw the current status
  185. if (strlen(Buffer) == strlen(BlankLine))
  186. cout << '\r' << Buffer << flush;
  187. else
  188. cout << '\r' << BlankLine << '\r' << Buffer << flush;
  189. memset(BlankLine,' ',strlen(Buffer));
  190. BlankLine[strlen(Buffer)] = 0;
  191. Update = false;
  192. }
  193. /*}}}*/