]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/progress.cc
   1 // -*- mode: cpp; mode: fold -*- 
   3 // $Id: progress.cc,v 1.12 2003/01/11 07:17:04 jgg Exp $ 
   4 /* ###################################################################### 
   6    OpProgress - Operation Progress 
   8    ##################################################################### */ 
  10 // Include Files                                                        /*{{{*/ 
  13 #include <apt-pkg/progress.h> 
  14 #include <apt-pkg/error.h> 
  15 #include <apt-pkg/configuration.h> 
  26 // OpProgress::OpProgress - Constructor                                 /*{{{*/ 
  27 // --------------------------------------------------------------------- 
  29 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),  
  30                            LastPercent(0), Percent(0) 
  32    memset(&LastTime
,0,sizeof(LastTime
)); 
  35 // OpProgress::Progress - Sub progress with no state change             /*{{{*/ 
  36 // --------------------------------------------------------------------- 
  37 /* Current is the Base Overall progress in units of Total. Cur is the sub 
  38    progress in units of SubTotal. Size is a scaling factor that says what 
  39    percent of Total SubTotal is. */ 
  40 void OpProgress::Progress(unsigned long long Cur
) 
  42    if (Total 
== 0 || Size 
== 0 || SubTotal 
== 0) 
  45       Percent 
= (Current 
+ Cur
/((float)SubTotal
)*Size
)*100.0/Total
; 
  49 // OpProgress::OverallProgress - Set the overall progress               /*{{{*/ 
  50 // --------------------------------------------------------------------- 
  52 void OpProgress::OverallProgress(unsigned long long Current
, unsigned long long Total
, 
  53                                  unsigned long long Size
,const string 
&Op
) 
  55    this->Current 
= Current
; 
  63       Percent 
= Current
*100.0/Total
; 
  67 // OpProgress::SubProgress - Set the sub progress state                 /*{{{*/ 
  68 // --------------------------------------------------------------------- 
  70 void OpProgress::SubProgress(unsigned long long SubTotal
,const string 
&Op
, 
  73    this->SubTotal 
= SubTotal
; 
  74    if (Op
.empty() == false) 
  76    if (Total 
== 0 || Percent 
== 0) 
  78    else if (Percent 
!= -1) 
  79       this->Percent 
= this->Current 
+= (Size
*Percent
)/SubTotal
; 
  81       this->Percent 
= Current
*100.0/Total
; 
  85 // OpProgress::CheckChange - See if the display should be updated       /*{{{*/ 
  86 // --------------------------------------------------------------------- 
  87 /* Progress calls are made so frequently that if every one resulted in  
  88    an update the display would be swamped and the system much slower. 
  89    This provides an upper bound on the update rate. */ 
  90 bool OpProgress::CheckChange(float Interval
) 
  92    // New major progress indication 
 101    if (SubOp 
!= LastSubOp
) 
 107    if ((int)LastPercent 
== (int)Percent
) 
 110    LastPercent 
= Percent
; 
 117    gettimeofday(&Now
,0); 
 118    double Diff 
= Now
.tv_sec 
- LastTime
.tv_sec 
+ (Now
.tv_usec 
- LastTime
.tv_usec
)/1000000.0; 
 125 // OpTextProgress::OpTextProgress - Constructor                         /*{{{*/ 
 126 // --------------------------------------------------------------------- 
 128 OpTextProgress::OpTextProgress(Configuration 
&Config
) :  
 129                                NoUpdate(false), NoDisplay(false), LastLen(0)  
 131    if (Config
.FindI("quiet",0) >= 1 || Config
.FindB("quiet::NoUpdate", false) == true) 
 133    if (Config
.FindI("quiet",0) >= 2) 
 137 // OpTextProgress::Done - Clean up the display                          /*{{{*/ 
 138 // --------------------------------------------------------------------- 
 140 void OpTextProgress::Done() 
 142    if (NoUpdate 
== false && OldOp
.empty() == false) 
 145       if (_error
->PendingError() == true) 
 146          snprintf(S
,sizeof(S
),_("%c%s... Error!"),'\r',OldOp
.c_str()); 
 148          snprintf(S
,sizeof(S
),_("%c%s... Done"),'\r',OldOp
.c_str()); 
 154    if (NoUpdate 
== true && NoDisplay 
== false && OldOp
.empty() == false) 
 161 // OpTextProgress::Update - Simple text spinner                         /*{{{*/ 
 162 // --------------------------------------------------------------------- 
 164 void OpTextProgress::Update() 
 166    if (CheckChange((NoUpdate 
== true?0:0.7)) == false) 
 169    // No percent spinner 
 170    if (NoUpdate 
== true) 
 172       if (MajorChange 
== false) 
 174       if (NoDisplay 
== false) 
 176          if (OldOp
.empty() == false) 
 179          cout 
<< Op 
<< "..." << flush
; 
 185    // Erase the old text and 'log' the event 
 187    if (MajorChange 
== true && OldOp
.empty() == false) 
 189       snprintf(S
,sizeof(S
),"\r%s",OldOp
.c_str()); 
 195    snprintf(S
,sizeof(S
),"\r%s... %u%%",Op
.c_str(),(unsigned int)Percent
); 
 201 // OpTextProgress::Write - Write the progress string                    /*{{{*/ 
 202 // --------------------------------------------------------------------- 
 203 /* This space fills the end to overwrite the previous text */ 
 204 void OpTextProgress::Write(const char *S
) 
 207    for (unsigned int I 
= strlen(S
); I 
< LastLen
; I
++) 
 209    cout 
<< '\r' << flush
;