acqprogress.cc 7.8 KB

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