progress.cc 5.8 KB

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