progress.cc 5.5 KB

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