acqprogress.cc 7.6 KB

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