progress.cc 5.9 KB

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