acqprogress.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acqprogress.cc,v 1.7 1999/01/27 02:48:53 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) << ']';
  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. {
  133. snprintf(S,End-S," [%s]",I->Status.c_str());
  134. Shown = true;
  135. }
  136. continue;
  137. }
  138. Shown = true;
  139. // Add in the short description
  140. if (I->CurrentItem->Owner->ID != 0)
  141. snprintf(S,End-S," [%x %s",I->CurrentItem->Owner->ID,
  142. I->CurrentItem->ShortDesc.c_str());
  143. else
  144. snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
  145. S += strlen(S);
  146. // Show the short mode string
  147. if (I->CurrentItem->Owner->Mode != 0)
  148. {
  149. snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
  150. S += strlen(S);
  151. }
  152. // Add the current progress
  153. if (Mode == Long)
  154. snprintf(S,End-S," %u",I->CurrentSize);
  155. else
  156. {
  157. if (Mode == Medium || I->TotalSize == 0)
  158. snprintf(S,End-S," %s",SizeToStr(I->CurrentSize).c_str());
  159. }
  160. S += strlen(S);
  161. // Add the total size and percent
  162. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  163. {
  164. if (Mode == Short)
  165. snprintf(S,End-S," %u%%",
  166. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  167. else
  168. snprintf(S,End-S,"/%s %u%%",SizeToStr(I->TotalSize).c_str(),
  169. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  170. }
  171. S += strlen(S);
  172. snprintf(S,End-S,"]");
  173. }
  174. // Show something..
  175. if (Shown == false)
  176. snprintf(S,End-S," [Working]");
  177. // Put in the ETA and cps meter
  178. if (CurrentCPS != 0)
  179. {
  180. char Tmp[300];
  181. unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
  182. sprintf(Tmp," %s/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
  183. unsigned int Len = strlen(Buffer);
  184. unsigned int LenT = strlen(Tmp);
  185. if (Len + LenT < ScreenWidth)
  186. {
  187. memset(Buffer + Len,' ',ScreenWidth - Len);
  188. strcpy(Buffer + ScreenWidth - LenT,Tmp);
  189. }
  190. }
  191. Buffer[ScreenWidth] = 0;
  192. // Draw the current status
  193. if (strlen(Buffer) == strlen(BlankLine))
  194. cout << '\r' << Buffer << flush;
  195. else
  196. cout << '\r' << BlankLine << '\r' << Buffer << flush;
  197. memset(BlankLine,' ',strlen(Buffer));
  198. BlankLine[strlen(Buffer)] = 0;
  199. Update = false;
  200. }
  201. /*}}}*/
  202. // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
  203. // ---------------------------------------------------------------------
  204. /* Prompt for a media swap */
  205. bool AcqTextStatus::MediaChange(string Media,string Drive)
  206. {
  207. if (Quiet <= 0)
  208. cout << '\r' << BlankLine << '\r';
  209. cout << "Media Change: Please insert the disc labeled '" << Media << "' in "\
  210. "the drive '" << Drive << "' and press enter" << endl;
  211. char C = 0;
  212. while (C != '\n' && C != '\r')
  213. read(STDIN_FILENO,&C,1);
  214. Update = true;
  215. return true;
  216. }
  217. /*}}}*/