Copy code from apt-get to display download progress
authorMichael Spang <mspang@csclub.uwaterloo.ca>
Thu, 19 Nov 2009 05:33:18 +0000 (00:33 -0500)
committerMichael Spang <mspang@csclub.uwaterloo.ca>
Tue, 22 Dec 2009 03:09:49 +0000 (22:09 -0500)
Signed-off-by: Michael Spang <mspang@csclub.uwaterloo.ca>
Makefile
acqprogress.cc [new file with mode: 0644]
acqprogress.h [new file with mode: 0644]
magic.cc

index 6f0111f..f7875de 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 all: magic awesome awesome.png
 magic: magic.cc
-       g++ -o magic -g3 -Wall -Werror -lapt-pkg -lapt-inst magic.cc
-awesome: awesome.c
+       g++ -o magic -g3 -Wall -Werror -lapt-pkg -lapt-inst magic.cc acqprogress.cc
+awesome: awesome.c acqprogress.cc
        g++ -o awesome -g3 -Wall -Werror -lapt-pkg -lapt-inst awesome.c
 awesome.c: awesome.rl
        ragel awesome.rl
diff --git a/acqprogress.cc b/acqprogress.cc
new file mode 100644 (file)
index 0000000..be5e91a
--- /dev/null
@@ -0,0 +1,290 @@
+// -*- mode: cpp; mode: fold -*-
+// Description                                                         /*{{{*/
+// $Id: acqprogress.cc,v 1.24 2003/04/27 01:56:48 doogie Exp $
+/* ######################################################################
+
+   Acquire Progress - Command line progress meter 
+   
+   ##################################################################### */
+                                                                       /*}}}*/
+// Include files                                                       /*{{{*/
+#include "acqprogress.h"
+#include <apt-pkg/acquire-item.h>
+#include <apt-pkg/acquire-worker.h>
+#include <apt-pkg/strutl.h>
+#include <apt-pkg/error.h>
+
+    
+#include <stdio.h>
+#include <signal.h>
+#include <iostream>
+
+#define _
+                                                                       /*}}}*/
+
+using namespace std;
+
+// AcqTextStatus::AcqTextStatus - Constructor                          /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+AcqTextStatus::AcqTextStatus(unsigned int &ScreenWidth,unsigned int Quiet) :
+    ScreenWidth(ScreenWidth), Quiet(Quiet)
+{
+}
+                                                                       /*}}}*/
+// AcqTextStatus::Start - Downloading has started                      /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void AcqTextStatus::Start() 
+{
+   pkgAcquireStatus::Start(); 
+   BlankLine[0] = 0;
+   ID = 1;
+};
+                                                                       /*}}}*/
+// AcqTextStatus::IMSHit - Called when an item got a HIT response      /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
+{
+   if (Quiet > 1)
+      return;
+
+   if (Quiet <= 0)
+      cout << '\r' << BlankLine << '\r';   
+   
+   cout << _("Hit ") << Itm.Description;
+   if (Itm.Owner->FileSize != 0)
+      cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
+   cout << endl;
+   Update = true;
+};
+                                                                       /*}}}*/
+// AcqTextStatus::Fetch - An item has started to download              /*{{{*/
+// ---------------------------------------------------------------------
+/* This prints out the short description and the expected size */
+void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
+{
+   Update = true;
+   if (Itm.Owner->Complete == true)
+      return;
+   
+   Itm.Owner->ID = ID++;
+   
+   if (Quiet > 1)
+      return;
+
+   if (Quiet <= 0)
+      cout << '\r' << BlankLine << '\r';
+   
+   cout << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description;
+   if (Itm.Owner->FileSize != 0)
+      cout << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
+   cout << endl;
+};
+                                                                       /*}}}*/
+// AcqTextStatus::Done - Completed a download                          /*{{{*/
+// ---------------------------------------------------------------------
+/* We don't display anything... */
+void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm)
+{
+   Update = true;
+};
+                                                                       /*}}}*/
+// AcqTextStatus::Fail - Called when an item fails to download         /*{{{*/
+// ---------------------------------------------------------------------
+/* We print out the error text  */
+void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
+{
+   if (Quiet > 1)
+      return;
+
+   // Ignore certain kinds of transient failures (bad code)
+   if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
+      return;
+      
+   if (Quiet <= 0)
+      cout << '\r' << BlankLine << '\r';
+   
+   if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
+   {
+      cout << _("Ign ") << Itm.Description << endl;
+   }
+   else
+   {
+      cout << _("Err ") << Itm.Description << endl;
+      cout << "  " << Itm.Owner->ErrorText << endl;
+   }
+   
+   Update = true;
+};
+                                                                       /*}}}*/
+// AcqTextStatus::Stop - Finished downloading                          /*{{{*/
+// ---------------------------------------------------------------------
+/* This prints out the bytes downloaded and the overall average line
+   speed */
+void AcqTextStatus::Stop()
+{
+   pkgAcquireStatus::Stop();
+   if (Quiet > 1)
+      return;
+
+   if (Quiet <= 0)
+      cout << '\r' << BlankLine << '\r' << flush;
+
+   if (FetchedBytes != 0 && _error->PendingError() == false)
+      ioprintf(cout,_("Fetched %sB in %s (%sB/s)\n"),
+              SizeToStr(FetchedBytes).c_str(),
+              TimeToStr(ElapsedTime).c_str(),
+              SizeToStr(CurrentCPS).c_str());
+}
+                                                                       /*}}}*/
+// AcqTextStatus::Pulse - Regular event pulse                          /*{{{*/
+// ---------------------------------------------------------------------
+/* This draws the current progress. Each line has an overall percent
+   meter and a per active item status meter along with an overall 
+   bandwidth and ETA indicator. */
+bool AcqTextStatus::Pulse(pkgAcquire *Owner)
+{
+   pkgAcquireStatus::Pulse(Owner);
+   
+   if (Quiet > 0)
+      return true;
+   
+   enum {Long = 0,Medium,Short} Mode = Long;
+   
+   char Buffer[sizeof(BlankLine)];
+   char *End = Buffer + sizeof(Buffer);
+   char *S = Buffer;
+   if (ScreenWidth >= sizeof(Buffer))
+      ScreenWidth = sizeof(Buffer)-1;
+
+   // Put in the percent done
+   sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems)));
+
+   bool Shown = false;
+   for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
+       I = Owner->WorkerStep(I))
+   {
+      S += strlen(S);
+      
+      // There is no item running 
+      if (I->CurrentItem == 0)
+      {
+        if (I->Status.empty() == false)
+        {
+           snprintf(S,End-S," [%s]",I->Status.c_str());
+           Shown = true;
+        }
+        
+        continue;
+      }
+
+      Shown = true;
+      
+      // Add in the short description
+      if (I->CurrentItem->Owner->ID != 0)
+        snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
+                 I->CurrentItem->ShortDesc.c_str());
+      else
+        snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
+      S += strlen(S);
+
+      // Show the short mode string
+      if (I->CurrentItem->Owner->Mode != 0)
+      {
+        snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
+        S += strlen(S);
+      }
+            
+      // Add the current progress
+      if (Mode == Long)
+        snprintf(S,End-S," %lu",I->CurrentSize);
+      else
+      {
+        if (Mode == Medium || I->TotalSize == 0)
+           snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
+      }
+      S += strlen(S);
+      
+      // Add the total size and percent
+      if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
+      {
+        if (Mode == Short)
+           snprintf(S,End-S," %lu%%",
+                    long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
+        else
+           snprintf(S,End-S,"/%sB %lu%%",SizeToStr(I->TotalSize).c_str(),
+                    long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
+      }      
+      S += strlen(S);
+      snprintf(S,End-S,"]");
+   }
+
+   // Show something..
+   if (Shown == false)
+      snprintf(S,End-S,_(" [Working]"));
+      
+   /* Put in the ETA and cps meter, block off signals to prevent strangeness
+      during resizing */
+   sigset_t Sigs,OldSigs;
+   sigemptyset(&Sigs);
+   sigaddset(&Sigs,SIGWINCH);
+   sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
+   
+   if (CurrentCPS != 0)
+   {      
+      char Tmp[300];
+      unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
+      sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
+      unsigned int Len = strlen(Buffer);
+      unsigned int LenT = strlen(Tmp);
+      if (Len + LenT < ScreenWidth)
+      {         
+        memset(Buffer + Len,' ',ScreenWidth - Len);
+        strcpy(Buffer + ScreenWidth - LenT,Tmp);
+      }      
+   }
+   Buffer[ScreenWidth] = 0;
+   BlankLine[ScreenWidth] = 0;
+   sigprocmask(SIG_SETMASK,&OldSigs,0);
+
+   // Draw the current status
+   if (strlen(Buffer) == strlen(BlankLine))
+      cout << '\r' << Buffer << flush;
+   else
+      cout << '\r' << BlankLine << '\r' << Buffer << flush;
+   memset(BlankLine,' ',strlen(Buffer));
+   BlankLine[strlen(Buffer)] = 0;
+   
+   Update = false;
+
+   return true;
+}
+                                                                       /*}}}*/
+// AcqTextStatus::MediaChange - Media need to be swapped               /*{{{*/
+// ---------------------------------------------------------------------
+/* Prompt for a media swap */
+bool AcqTextStatus::MediaChange(string Media,string Drive)
+{
+   if (Quiet <= 0)
+      cout << '\r' << BlankLine << '\r';
+   ioprintf(cout,_("Media change: please insert the disc labeled\n"
+                  " '%s'\n"
+                  "in the drive '%s' and press enter\n"),
+           Media.c_str(),Drive.c_str());
+
+   char C = 0;
+   bool bStatus = true;
+   while (C != '\n' && C != '\r')
+   {
+      int len = read(STDIN_FILENO,&C,1);
+      if(C == 'c' || len <= 0)
+        bStatus = false;
+   }
+
+   if(bStatus)
+      Update = true;
+   return bStatus;
+}
+                                                                       /*}}}*/
diff --git a/acqprogress.h b/acqprogress.h
new file mode 100644 (file)
index 0000000..48f90ae
--- /dev/null
@@ -0,0 +1,37 @@
+// -*- mode: cpp; mode: fold -*-
+// Description                                                         /*{{{*/
+// $Id: acqprogress.h,v 1.5 2003/02/02 22:24:11 jgg Exp $
+/* ######################################################################
+
+   Acquire Progress - Command line progress meter 
+   
+   ##################################################################### */
+                                                                       /*}}}*/
+#ifndef ACQPROGRESS_H
+#define ACQPROGRESS_H
+
+#include <apt-pkg/acquire.h>
+
+class AcqTextStatus : public pkgAcquireStatus
+{
+   unsigned int &ScreenWidth;
+   char BlankLine[1024];
+   unsigned long ID;
+   unsigned long Quiet;
+   
+   public:
+   
+   virtual bool MediaChange(string Media,string Drive);
+   virtual void IMSHit(pkgAcquire::ItemDesc &Itm);
+   virtual void Fetch(pkgAcquire::ItemDesc &Itm);
+   virtual void Done(pkgAcquire::ItemDesc &Itm);
+   virtual void Fail(pkgAcquire::ItemDesc &Itm);
+   virtual void Start();
+   virtual void Stop();
+   
+   bool Pulse(pkgAcquire *Owner);
+
+   AcqTextStatus(unsigned int &ScreenWidth,unsigned int Quiet);
+};
+
+#endif
index 0b1a6e8..556c810 100644 (file)
--- a/magic.cc
+++ b/magic.cc
@@ -13,6 +13,8 @@
 #include <apt-pkg/sptr.h>
 #include <apt-pkg/acquire-item.h>
 
+#include "acqprogress.h"
+
 using namespace std;
 
 
@@ -54,7 +56,9 @@ bool InstallPackages(pkgCacheFile &Cache,bool ShwKept = false,bool Ask = true,
          return _error->Error(("Unable to lock the download directory"));
    }
 
-   pkgAcquire Fetcher;
+   unsigned int width = 80;
+   AcqTextStatus status (width, 0);
+   pkgAcquire Fetcher (&status);
 
    pkgSourceList List;
    if (List.ReadMainList() == false)