From 0c6c75bf58166ebd2cd157b85c783ea91ac6a9d9 Mon Sep 17 00:00:00 2001
From: Edwin Eefting <edwin@datux.nl>
Date: Tue, 22 Feb 2022 18:41:54 +0100
Subject: [PATCH] cleaner progress clearing

---
 zfs_autobackup/LogConsole.py    | 14 +++++++++++---
 zfs_autobackup/ZfsAutobackup.py | 21 +++++++++++----------
 zfs_autobackup/ZfsAutoverify.py |  8 ++++----
 3 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/zfs_autobackup/LogConsole.py b/zfs_autobackup/LogConsole.py
index eedb66b..e76959c 100644
--- a/zfs_autobackup/LogConsole.py
+++ b/zfs_autobackup/LogConsole.py
@@ -10,6 +10,7 @@ class LogConsole:
         self.last_log = ""
         self.show_debug = show_debug
         self.show_verbose = show_verbose
+        self._progress_uncleared=False
 
         if color:
             # try to use color, failback if colorama not available
@@ -25,6 +26,7 @@ class LogConsole:
             self.colorama=False
 
     def error(self, txt):
+        self.clear_progress()
         if self.colorama:
             print(colorama.Fore.RED + colorama.Style.BRIGHT + "! " + txt + colorama.Style.RESET_ALL, file=sys.stderr)
         else:
@@ -32,6 +34,7 @@ class LogConsole:
         sys.stderr.flush()
 
     def warning(self, txt):
+        self.clear_progress()
         if self.colorama:
             print(colorama.Fore.YELLOW + colorama.Style.BRIGHT + "  NOTE: " + txt + colorama.Style.RESET_ALL)
         else:
@@ -40,6 +43,7 @@ class LogConsole:
 
     def verbose(self, txt):
         if self.show_verbose:
+            self.clear_progress()
             if self.colorama:
                 print(colorama.Style.NORMAL + "  " + txt + colorama.Style.RESET_ALL)
             else:
@@ -48,6 +52,7 @@ class LogConsole:
 
     def debug(self, txt):
         if self.show_debug:
+            self.clear_progress()
             if self.colorama:
                 print(colorama.Fore.GREEN + "# " + txt + colorama.Style.RESET_ALL)
             else:
@@ -57,10 +62,13 @@ class LogConsole:
     def progress(self, txt):
         """print progress output to stderr (stays on same line)"""
         self.clear_progress()
+        self._progress_uncleared=True
         print(">>> {}\r".format(txt), end='', file=sys.stderr)
         sys.stderr.flush()
 
     def clear_progress(self):
-        import colorama
-        print(colorama.ansi.clear_line(), end='', file=sys.stderr)
-        sys.stderr.flush()
+        if self._progress_uncleared:
+            import colorama
+            print(colorama.ansi.clear_line(), end='', file=sys.stderr)
+            # sys.stderr.flush()
+            self._progress_uncleared=False
diff --git a/zfs_autobackup/ZfsAutobackup.py b/zfs_autobackup/ZfsAutobackup.py
index 0b899b2..8ef9c93 100644
--- a/zfs_autobackup/ZfsAutobackup.py
+++ b/zfs_autobackup/ZfsAutobackup.py
@@ -157,8 +157,8 @@ class ZfsAutobackup(ZfsAuto):
             except Exception as e:
                 dataset.error("Error during thinning of missing datasets ({})".format(str(e)))
 
-        if self.args.progress:
-            self.clear_progress()
+        # if self.args.progress:
+        #     self.clear_progress()
 
     # NOTE: this method also uses self.args. args that need extra processing are passed as function parameters:
     def destroy_missing_targets(self, target_dataset, used_target_datasets):
@@ -217,13 +217,13 @@ class ZfsAutobackup(ZfsAuto):
                                 dataset.destroy(fail_exception=True)
 
             except Exception as e:
-                if self.args.progress:
-                    self.clear_progress()
+                # if self.args.progress:
+                #     self.clear_progress()
 
                 dataset.error("Error during --destroy-missing: {}".format(str(e)))
 
-        if self.args.progress:
-            self.clear_progress()
+        # if self.args.progress:
+        #     self.clear_progress()
 
     def get_send_pipes(self, logger):
         """determine the zfs send pipe"""
@@ -333,8 +333,8 @@ class ZfsAutobackup(ZfsAuto):
                                               decrypt=self.args.decrypt, encrypt=self.args.encrypt,
                                               zfs_compressed=self.args.zfs_compressed)
             except Exception as e:
-                if self.args.progress:
-                    self.clear_progress()
+                # if self.args.progress:
+                #     self.clear_progress()
 
                 fail_count = fail_count + 1
                 source_dataset.error("FAILED: " + str(e))
@@ -342,8 +342,8 @@ class ZfsAutobackup(ZfsAuto):
                     self.verbose("Debug mode, aborting on first error")
                     raise
 
-        if self.args.progress:
-            self.clear_progress()
+        # if self.args.progress:
+        #     self.clear_progress()
 
         target_path_dataset = target_node.get_dataset(self.args.target_path)
         if not self.args.no_thinning:
@@ -477,6 +477,7 @@ class ZfsAutobackup(ZfsAuto):
                 self.verbose("")
                 self.warning("TEST MODE - DID NOT MAKE ANY CHANGES!")
 
+            self.clear_progress()
             return fail_count
 
         except Exception as e:
diff --git a/zfs_autobackup/ZfsAutoverify.py b/zfs_autobackup/ZfsAutoverify.py
index 37caf05..2c2f9b4 100644
--- a/zfs_autobackup/ZfsAutoverify.py
+++ b/zfs_autobackup/ZfsAutoverify.py
@@ -203,8 +203,8 @@ class ZfsAutoverify(ZfsAuto):
 
 
             except Exception as e:
-                if self.args.progress:
-                    self.clear_progress()
+                # if self.args.progress:
+                #     self.clear_progress()
 
                 fail_count = fail_count + 1
                 target_dataset.error("FAILED: " + str(e))
@@ -212,8 +212,8 @@ class ZfsAutoverify(ZfsAuto):
                     self.verbose("Debug mode, aborting on first error")
                     raise
 
-        if self.args.progress:
-            self.clear_progress()
+        # if self.args.progress:
+        #     self.clear_progress()
 
         return fail_count