progress.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: progress.cc,v 1.12 2003/01/11 07:17:04 jgg Exp $
  4. /* ######################################################################
  5. OpProgress - Operation Progress
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <config.h>
  10. #include <apt-pkg/progress.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <sys/time.h>
  14. #include <string>
  15. #include <iostream>
  16. #include <stdio.h>
  17. #include <cstring>
  18. #include <apti18n.h>
  19. /*}}}*/
  20. using namespace std;
  21. // OpProgress::OpProgress - Constructor /*{{{*/
  22. // ---------------------------------------------------------------------
  23. /* */
  24. OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
  25. LastPercent(0), Percent(0)
  26. {
  27. memset(&LastTime,0,sizeof(LastTime));
  28. }
  29. /*}}}*/
  30. // OpProgress::Progress - Sub progress with no state change /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* Current is the Base Overall progress in units of Total. Cur is the sub
  33. progress in units of SubTotal. Size is a scaling factor that says what
  34. percent of Total SubTotal is. */
  35. void OpProgress::Progress(unsigned long long Cur)
  36. {
  37. if (Total == 0 || Size == 0 || SubTotal == 0)
  38. Percent = 0;
  39. else
  40. Percent = (Current + Cur/((float)SubTotal)*Size)*100.0/Total;
  41. Update();
  42. }
  43. /*}}}*/
  44. // OpProgress::OverallProgress - Set the overall progress /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* */
  47. void OpProgress::OverallProgress(unsigned long long Current, unsigned long long Total,
  48. unsigned long long Size,const string &Op)
  49. {
  50. this->Current = Current;
  51. this->Total = Total;
  52. this->Size = Size;
  53. this->Op = Op;
  54. SubOp = string();
  55. if (Total == 0)
  56. Percent = 0;
  57. else
  58. Percent = Current*100.0/Total;
  59. Update();
  60. }
  61. /*}}}*/
  62. // OpProgress::SubProgress - Set the sub progress state /*{{{*/
  63. // ---------------------------------------------------------------------
  64. /* */
  65. void OpProgress::SubProgress(unsigned long long SubTotal,const string &Op,
  66. float const Percent)
  67. {
  68. this->SubTotal = SubTotal;
  69. if (Op.empty() == false)
  70. SubOp = Op;
  71. if (Total == 0 || Percent == 0)
  72. this->Percent = 0;
  73. else if (Percent != -1)
  74. this->Percent = this->Current += (Size*Percent)/SubTotal;
  75. else
  76. this->Percent = Current*100.0/Total;
  77. Update();
  78. }
  79. /*}}}*/
  80. // OpProgress::CheckChange - See if the display should be updated /*{{{*/
  81. // ---------------------------------------------------------------------
  82. /* Progress calls are made so frequently that if every one resulted in
  83. an update the display would be swamped and the system much slower.
  84. This provides an upper bound on the update rate. */
  85. bool OpProgress::CheckChange(float Interval)
  86. {
  87. // New major progress indication
  88. if (Op != LastOp)
  89. {
  90. MajorChange = true;
  91. LastOp = Op;
  92. return true;
  93. }
  94. MajorChange = false;
  95. if (SubOp != LastSubOp)
  96. {
  97. LastSubOp = SubOp;
  98. return true;
  99. }
  100. if ((int)LastPercent == (int)Percent)
  101. return false;
  102. LastPercent = Percent;
  103. if (Interval == 0)
  104. return false;
  105. // Check time delta
  106. struct timeval Now;
  107. gettimeofday(&Now,0);
  108. double Diff = Now.tv_sec - LastTime.tv_sec + (Now.tv_usec - LastTime.tv_usec)/1000000.0;
  109. if (Diff < Interval)
  110. return false;
  111. LastTime = Now;
  112. return true;
  113. }
  114. /*}}}*/
  115. // OpTextProgress::OpTextProgress - Constructor /*{{{*/
  116. // ---------------------------------------------------------------------
  117. /* */
  118. OpTextProgress::OpTextProgress(Configuration &Config) :
  119. NoUpdate(false), NoDisplay(false), LastLen(0)
  120. {
  121. if (Config.FindI("quiet",0) >= 1 || Config.FindB("quiet::NoUpdate", false) == true)
  122. NoUpdate = true;
  123. if (Config.FindI("quiet",0) >= 2 || Config.FindB("quiet::NoProgress", false) == true)
  124. NoDisplay = true;
  125. }
  126. /*}}}*/
  127. // OpTextProgress::Done - Clean up the display /*{{{*/
  128. // ---------------------------------------------------------------------
  129. /* */
  130. void OpTextProgress::Done()
  131. {
  132. if (NoUpdate == false && OldOp.empty() == false)
  133. {
  134. char S[300];
  135. if (_error->PendingError() == true)
  136. snprintf(S,sizeof(S),_("%c%s... Error!"),'\r',OldOp.c_str());
  137. else
  138. snprintf(S,sizeof(S),_("%c%s... Done"),'\r',OldOp.c_str());
  139. Write(S);
  140. cout << endl;
  141. OldOp = string();
  142. }
  143. if (NoUpdate == true && NoDisplay == false && OldOp.empty() == false)
  144. {
  145. OldOp = string();
  146. cout << endl;
  147. }
  148. }
  149. /*}}}*/
  150. // OpTextProgress::Update - Simple text spinner /*{{{*/
  151. // ---------------------------------------------------------------------
  152. /* */
  153. void OpTextProgress::Update()
  154. {
  155. if (CheckChange((NoUpdate == true?0:0.7)) == false)
  156. return;
  157. // No percent spinner
  158. if (NoUpdate == true)
  159. {
  160. if (MajorChange == false)
  161. return;
  162. if (NoDisplay == false)
  163. {
  164. if (OldOp.empty() == false)
  165. cout << endl;
  166. OldOp = "a";
  167. cout << Op << _("...") << flush;
  168. }
  169. return;
  170. }
  171. // Erase the old text and 'log' the event
  172. char S[300];
  173. if (MajorChange == true && OldOp.empty() == false)
  174. {
  175. snprintf(S,sizeof(S),"\r%s",OldOp.c_str());
  176. Write(S);
  177. cout << endl;
  178. }
  179. // Print the spinner
  180. snprintf(S,sizeof(S),_("%c%s... %u%%"),'\r',Op.c_str(),(unsigned int)Percent);
  181. Write(S);
  182. OldOp = Op;
  183. }
  184. /*}}}*/
  185. // OpTextProgress::Write - Write the progress string /*{{{*/
  186. // ---------------------------------------------------------------------
  187. /* This space fills the end to overwrite the previous text */
  188. void OpTextProgress::Write(const char *S)
  189. {
  190. cout << S;
  191. for (unsigned int I = strlen(S); I < LastLen; I++)
  192. cout << ' ';
  193. cout << '\r' << flush;
  194. LastLen = strlen(S);
  195. }
  196. /*}}}*/