bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Hi! coreutils' Progress bar patch


From: Andrea Di Pasquale
Subject: Hi! coreutils' Progress bar patch
Date: Wed, 19 Dec 2007 03:06:33 +0100

Hi, i'm andrea, I writed a little patch for cp's progress bar (coreutils
5.97/6.9).

Example:

crash ~ $ cp --help | grep -i progress
  -B  --progress               show a progress bar (file > 1 kB)

crash ~ $ cp -B mov.avi test/
  [==================================================]  100%
691966/691966 kb

progres bar patch codes (it's 6.9 patch):


-- copy.c      2007-03-18 22:36:43.000000000 +0100
+++ copy.c      2007-12-19 02:10:53.000000000 +0100
@@ -17,6 +17,8 @@

 /* Extracted from cp.c and librarified by Jim Meyering.  */


+/* Progress bar support add by Andrea Di Pasquale (address@hidden) */
+
 #include <config.h>
 #include <stdio.h>
 #include <assert.h>

@@ -29,6 +31,8 @@
 # include <priv.h>
 #endif

+# include <sys/ioctl.h>
+
 #include "system.h"
 #include "acl.h"
 #include "backupfile.h"
@@ -244,6 +248,98 @@

   return lchmod (name, mode);
 }

+/* This function displays a progress bar
+   if the progress flag is set.
+   It uses a 1:1/1:2 scales. */
+
+void
+progress_bar(size_t dim_tot, size_t dim_cur)

+{
+  int i;
+  size_t per_tot, per_cur, per_tot_scl, per_cur_scl;
+  struct winsize ws;
+  int scale;
+
+  /* Check:
+     if filesize is less than 1 kb, exit */
+  if (! (dim_tot / 1024))

+    return;
+  /* else display size in kb */
+  else
+    {
+      dim_tot /= 1024;
+      dim_cur /= 1024;
+    }
+
+  per_tot = 100;
+  /* total percentage */
+  per_cur = (per_tot * dim_cur) / dim_tot;

+  /* current percentage */
+
+  /* line lenght */
+  if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0)
+    if (ws.ws_col >= 150)
+      {
+        /* 1:1 scale */

+        scale = 1;
+       per_tot_scl = per_tot;
+       per_cur_scl = per_cur;
+      }
+    else
+      {
+        /* 1:2 scale */
+        scale = 2;
+       per_tot_scl = per_tot / 2;
+       per_cur_scl = per_cur / 2;

+      }
+
+  /* Refresh ther bar indicator */
+  printf("\r [");
+
+  /* Display the bar indicator:
+     - =   : indicates a part the bar
+     - >   : indicates the end of the bar

+             (if it's finish, convert '>' to '='*/
+  for (i = 0; i < per_cur_scl; i++)
+    if ((i + 1) == per_cur_scl)
+      switch (scale)
+        {
+         case 1:
+           if (per_cur_scl == 100)

+             putchar('=');
+           else
+             putchar('>');
+           break;
+
+         case 2:
+            if (per_cur_scl == 50)
+              putchar('=');

+            else
+             putchar('>');
+            break;
+        }
+    else
+      putchar('=');
+
+  for (i = per_cur_scl; i < per_tot_scl; i++)
+    putchar(' ');

+
+  printf("] %d\% \t%d/%d kb", per_cur, dim_cur, dim_tot);
+  fflush(stdout);
+
+  switch (scale)
+    {
+      case 1:
+        if (per_cur_scl == 100)
+         putchar('\n');

+         break;
+      case 2:
+        if (per_cur_scl == 50)
+          putchar('\n');
+          break;
+    }
+}
+
 /* Copy a regular file from SRC_NAME to DST_NAME.
    If the source file contains holes, copies holes and blocks of zeros

    in the source file as holes in the destination file.
@@ -270,6 +366,8 @@
   struct stat sb;
   struct stat src_open_sb;
   bool return_val = true;
+  size_t dim_tot = 0,
+         dim_cur = 0;

   source_desc = open (src_name, O_RDONLY | O_BINARY);
   if (source_desc < 0)
@@ -483,6 +581,14 @@
                }
              last_write_made_hole = false;

+             /* If the progress flag is set, it uses a progress bar */

+              if (x->progress)
+               {
+                 dim_tot = src_sb->st_size;
+                 dim_cur += n;
+
+                 progress_bar (dim_tot, dim_cur);
+               }

              /* A short read on a regular file means EOF.  */
              if (n_read != buf_size && S_ISREG (src_open_sb.st_mode))
                break;



-- copy.h      2007-03-18 22:36:
43.000000000 +0100
+++ copy.h      2007-12-19 01:58:06.000000000 +0100
@@ -83,6 +83,9 @@
 {
   enum backup_type backup_type;

+  /* If true, Display a progress bar */
+  bool progress;
+
   /* If true, copy all files except (directories and, if not dereferencing

      them, symbolic links,) as if they were regular files.  */
   bool copy_as_regular;



--- cp.c        2007-03-18 22:36:43.000000000 +0100
+++ cp.c        2007-12-19 01:58:10.000000000 +0100

@@ -118,6 +118,7 @@
 {
   {"archive", no_argument, NULL, 'a'},
   {"backup", optional_argument, NULL, 'b'},
+  {"progress", no_argument, NULL, 'B'},
   {"copy-contents", no_argument, NULL, COPY_CONTENTS_OPTION},

   {"dereference", no_argument, NULL, 'L'},
   {"force", no_argument, NULL, 'f'},
@@ -171,6 +172,7 @@
   -a, --archive                same as -dpPR\n\
       --backup[=CONTROL]       make a backup of each existing
destination file\n\

   -b                           like --backup but does not accept an argument\n\
+  -B  --progress               show a progress bar (file > 1 kB)\n\
       --copy-contents          copy contents of special files when recursive\n\

   -d                           same as --no-dereference --preserve=link\n\
 "), stdout);
@@ -735,6 +737,7 @@
 static void
 cp_option_init (struct cp_options *x)
 {
+  x->progress = false;

   x->copy_as_regular = true;
   x->dereference = DEREF_UNDEFINED;
   x->unlink_dest_before_opening = false;
@@ -868,7 +871,7 @@
      we'll actually use backup_suffix_string.  */
   backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");


-  while ((c = getopt_long (argc, argv, "abdfHilLprst:uvxPRS:T",
+  while ((c = getopt_long (argc, argv, "abBdfHilLprst:uvxPRS:T",
                           long_opts, NULL))
         != -1)

     {
@@ -895,6 +898,10 @@
            version_control_string = optarg;
          break;

+       case 'B':
+         x.progress = true;
+         break;
+
        case COPY_CONTENTS_OPTION:

          copy_contents = true;
          break;


I'm happy. I can to use cp with progress bar!!!! ^_^

I wait a your response, very thank you for coreutils :)

Andrea

Attachment: coreutils-patch.tar.bz2
Description: BZip2 compressed data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]