progress.cc 4.4 KB

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