progress.cc 4.9 KB

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