progress.cc 5.8 KB

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