Commit ecbf0d3a ecbf0d3a1ab03da431257128d66c5a242a0294a5 by Sergey Poznyakoff

movemail: implement --progress-meter option

1 parent f9a28c03
1 GNU mailutils NEWS -- history of user-visible changes. 2017-04-06 1 GNU mailutils NEWS -- history of user-visible changes. 2017-04-08
2 Copyright (C) 2002-2017 Free Software Foundation, Inc. 2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 See the end of file for copying conditions. 3 See the end of file for copying conditions.
4 4
...@@ -86,6 +86,8 @@ defined. Instead, the following constants are defined in config.h: ...@@ -86,6 +86,8 @@ defined. Instead, the following constants are defined in config.h:
86 MAILUTILS_VERSION_MINOR Minor version number 86 MAILUTILS_VERSION_MINOR Minor version number
87 MAILUTILS_VERSION_PATCH Patchlevel number (or 0, for stable releases). 87 MAILUTILS_VERSION_PATCH Patchlevel number (or 0, for stable releases).
88 88
89 * movemail: new option --progress-meter
90
89 91
90 Version 3.2 - 2017-03-11 92 Version 3.2 - 2017-03-11
91 93
......
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
29 #include <mailutils/tls.h> 29 #include <mailutils/tls.h>
30 #include "mailutils/cli.h" 30 #include "mailutils/cli.h"
31 #include <muaux.h> 31 #include <muaux.h>
32 #ifdef HAVE_TERMIOS_H
33 # include <termios.h>
34 #endif
35 #include <sys/ioctl.h>
32 36
33 static int reverse_order; 37 static int reverse_order;
34 static int preserve_mail; 38 static int preserve_mail;
...@@ -39,6 +43,7 @@ static int ignore_errors; ...@@ -39,6 +43,7 @@ static int ignore_errors;
39 static char *program_id_option; 43 static char *program_id_option;
40 static size_t max_messages_option; 44 static size_t max_messages_option;
41 static int notify; 45 static int notify;
46 static int progress_meter_option;
42 47
43 /* These bits tell what to do when an error occurs: */ 48 /* These bits tell what to do when an error occurs: */
44 #define ONERROR_SKIP 0x01 /* Skip to the next message */ 49 #define ONERROR_SKIP 0x01 /* Skip to the next message */
...@@ -303,6 +308,10 @@ static struct mu_option movemail_options[] = { ...@@ -303,6 +308,10 @@ static struct mu_option movemail_options[] = {
303 N_("enable biff notification"), 308 N_("enable biff notification"),
304 mu_c_bool, &notify }, 309 mu_c_bool, &notify },
305 310
311 { "progress-meter", 'm', NULL, MU_OPTION_DEFAULT,
312 N_("enable progress meter"),
313 mu_c_bool, &progress_meter_option },
314
306 MU_OPTION_END 315 MU_OPTION_END
307 }, *options[] = { movemail_options, NULL }; 316 }, *options[] = { movemail_options, NULL };
308 317
...@@ -840,6 +849,76 @@ set_program_id (const char *source_name, const char *dest_name) ...@@ -840,6 +849,76 @@ set_program_id (const char *source_name, const char *dest_name)
840 mu_stdstream_strerr_setup (MU_STRERR_STDERR); 849 mu_stdstream_strerr_setup (MU_STRERR_STDERR);
841 } 850 }
842 851
852 static int
853 screen_width (void)
854 {
855 struct winsize ws;
856 ws.ws_col = 0;
857 if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
858 {
859 char *p = getenv ("COLUMNS");
860 if (p)
861 ws.ws_col = atol (p);
862 }
863 if (ws.ws_col == 0)
864 return 80;
865 return ws.ws_col;
866 }
867
868 static void
869 progress_format (size_t pos, size_t count)
870 {
871 int n;
872
873 fputc ('\r', stdout);
874 n = printf ("message %zu/%zu", pos, count);
875 n = screen_width () - n;
876 while (n--)
877 fputc (' ', stdout);
878 fflush (stdout);
879 }
880
881 void
882 progress_start (mu_iterator_t itr)
883 {
884 size_t count;
885
886 if (!progress_meter_option)
887 return;
888
889 if (mu_iterator_ctl (itr, mu_itrctl_count, &count))
890 {
891 progress_meter_option = 0;
892 return;
893 }
894 progress_format (0, count);
895 }
896
897 void
898 progress_mark (mu_iterator_t itr)
899 {
900 size_t count, pos;
901
902 if (!progress_meter_option)
903 return;
904
905 if (mu_iterator_ctl (itr, mu_itrctl_count, &count)
906 || mu_iterator_ctl (itr, mu_itrctl_tell, &pos))
907 {
908 progress_meter_option = 0;
909 return;
910 }
911 if (reverse_order)
912 pos = count - pos + 1;
913 progress_format (pos, count);
914 }
915
916 void
917 progress_stop (void)
918 {
919 if (progress_meter_option)
920 fputc ('\n', stdout);
921 }
843 922
844 int 923 int
845 main (int argc, char **argv) 924 main (int argc, char **argv)
...@@ -871,6 +950,9 @@ main (int argc, char **argv) ...@@ -871,6 +950,9 @@ main (int argc, char **argv)
871 950
872 if (ignore_errors) 951 if (ignore_errors)
873 onerror_flags |= ONERROR_SKIP|ONERROR_COUNT; 952 onerror_flags |= ONERROR_SKIP|ONERROR_COUNT;
953
954 if (!isatty (1))
955 progress_meter_option = 0;
874 956
875 if (emacs_mode) 957 if (emacs_mode)
876 { 958 {
...@@ -965,6 +1047,7 @@ main (int argc, char **argv) ...@@ -965,6 +1047,7 @@ main (int argc, char **argv)
965 exit (1); 1047 exit (1);
966 } 1048 }
967 1049
1050 progress_start (itr);
968 for (mu_iterator_first (itr); !mu_iterator_is_done (itr); 1051 for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
969 mu_iterator_next (itr)) 1052 mu_iterator_next (itr))
970 { 1053 {
...@@ -980,6 +1063,7 @@ main (int argc, char **argv) ...@@ -980,6 +1063,7 @@ main (int argc, char **argv)
980 get_err_count++; 1063 get_err_count++;
981 continue; 1064 continue;
982 } 1065 }
1066 progress_mark (itr);
983 if (movemail (dest, msg, uidl->msgno)) 1067 if (movemail (dest, msg, uidl->msgno))
984 break; 1068 break;
985 } 1069 }
...@@ -1000,7 +1084,8 @@ main (int argc, char **argv) ...@@ -1000,7 +1084,8 @@ main (int argc, char **argv)
1000 mu_strerror (rc)); 1084 mu_strerror (rc));
1001 return 1; 1085 return 1;
1002 } 1086 }
1003 1087
1088 progress_start (itr);
1004 for (mu_iterator_first (itr); !mu_iterator_is_done (itr); 1089 for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
1005 mu_iterator_next (itr)) 1090 mu_iterator_next (itr))
1006 { 1091 {
...@@ -1026,8 +1111,10 @@ main (int argc, char **argv) ...@@ -1026,8 +1111,10 @@ main (int argc, char **argv)
1026 1111
1027 if (movemail (dest, msg, msgno)) 1112 if (movemail (dest, msg, msgno))
1028 break; 1113 break;
1114 progress_mark (itr);
1029 } 1115 }
1030 } 1116 }
1117 progress_stop ();
1031 mu_iterator_destroy (&itr); 1118 mu_iterator_destroy (&itr);
1032 1119
1033 if (verbose_option) 1120 if (verbose_option)
......