acqprogress.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acqprogress.cc,v 1.15 1999/06/13 05:06:40 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 (Quiet <= 0)
  85. cout << '\r' << BlankLine << '\r';
  86. if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
  87. {
  88. cout << "Ign " << Itm.Description << endl;
  89. }
  90. else
  91. {
  92. cout << "Err " << Itm.Description << endl;
  93. cout << " " << Itm.Owner->ErrorText << endl;
  94. }
  95. Update = true;
  96. };
  97. /*}}}*/
  98. // AcqTextStatus::Stop - Finished downloading /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* This prints out the bytes downloaded and the overall average line
  101. speed */
  102. void AcqTextStatus::Stop()
  103. {
  104. pkgAcquireStatus::Stop();
  105. if (Quiet > 1)
  106. return;
  107. if (Quiet <= 0)
  108. cout << '\r' << BlankLine << '\r';
  109. if (FetchedBytes != 0)
  110. cout << "Fetched " << SizeToStr(FetchedBytes) << "B in " <<
  111. TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) <<
  112. "B/s)" << endl;
  113. }
  114. /*}}}*/
  115. // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
  116. // ---------------------------------------------------------------------
  117. /* This draws the current progress. Each line has an overall percent
  118. meter and a per active item status meter along with an overall
  119. bandwidth and ETA indicator. */
  120. bool AcqTextStatus::Pulse(pkgAcquire *Owner)
  121. {
  122. if (Quiet > 0)
  123. return true;
  124. pkgAcquireStatus::Pulse(Owner);
  125. enum {Long = 0,Medium,Short} Mode = Long;
  126. char Buffer[300];
  127. char *End = Buffer + sizeof(Buffer);
  128. char *S = Buffer;
  129. // Put in the percent done
  130. sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems)));
  131. bool Shown = false;
  132. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  133. I = Owner->WorkerStep(I))
  134. {
  135. S += strlen(S);
  136. // There is no item running
  137. if (I->CurrentItem == 0)
  138. {
  139. if (I->Status.empty() == false)
  140. {
  141. snprintf(S,End-S," [%s]",I->Status.c_str());
  142. Shown = true;
  143. }
  144. continue;
  145. }
  146. Shown = true;
  147. // Add in the short description
  148. if (I->CurrentItem->Owner->ID != 0)
  149. snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
  150. I->CurrentItem->ShortDesc.c_str());
  151. else
  152. snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
  153. S += strlen(S);
  154. // Show the short mode string
  155. if (I->CurrentItem->Owner->Mode != 0)
  156. {
  157. snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
  158. S += strlen(S);
  159. }
  160. // Add the current progress
  161. if (Mode == Long)
  162. snprintf(S,End-S," %lu",I->CurrentSize);
  163. else
  164. {
  165. if (Mode == Medium || I->TotalSize == 0)
  166. snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
  167. }
  168. S += strlen(S);
  169. // Add the total size and percent
  170. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  171. {
  172. if (Mode == Short)
  173. snprintf(S,End-S," %lu%%",
  174. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  175. else
  176. snprintf(S,End-S,"/%sB %lu%%",SizeToStr(I->TotalSize).c_str(),
  177. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  178. }
  179. S += strlen(S);
  180. snprintf(S,End-S,"]");
  181. }
  182. // Show something..
  183. if (Shown == false)
  184. snprintf(S,End-S," [Working]");
  185. /* Put in the ETA and cps meter, block off signals to prevent strangeness
  186. during resizing */
  187. sigset_t Sigs,OldSigs;
  188. sigemptyset(&Sigs);
  189. sigaddset(&Sigs,SIGWINCH);
  190. sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
  191. if (CurrentCPS != 0)
  192. {
  193. char Tmp[300];
  194. unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
  195. sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
  196. unsigned int Len = strlen(Buffer);
  197. unsigned int LenT = strlen(Tmp);
  198. if (Len + LenT < ScreenWidth)
  199. {
  200. memset(Buffer + Len,' ',ScreenWidth - Len);
  201. strcpy(Buffer + ScreenWidth - LenT,Tmp);
  202. }
  203. }
  204. Buffer[ScreenWidth] = 0;
  205. BlankLine[ScreenWidth] = 0;
  206. sigprocmask(SIG_UNBLOCK,&OldSigs,0);
  207. // Draw the current status
  208. if (strlen(Buffer) == strlen(BlankLine))
  209. cout << '\r' << Buffer << flush;
  210. else
  211. cout << '\r' << BlankLine << '\r' << Buffer << flush;
  212. memset(BlankLine,' ',strlen(Buffer));
  213. BlankLine[strlen(Buffer)] = 0;
  214. Update = false;
  215. return true;
  216. }
  217. /*}}}*/
  218. // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
  219. // ---------------------------------------------------------------------
  220. /* Prompt for a media swap */
  221. bool AcqTextStatus::MediaChange(string Media,string Drive)
  222. {
  223. if (Quiet <= 0)
  224. cout << '\r' << BlankLine << '\r';
  225. cout << "Media Change: Please insert the disc labeled '" << Media << "' in "\
  226. "the drive '" << Drive << "' and press enter" << endl;
  227. char C = 0;
  228. while (C != '\n' && C != '\r')
  229. read(STDIN_FILENO,&C,1);
  230. Update = true;
  231. return true;
  232. }
  233. /*}}}*/