# Path: zfs_autobackup/LogConsole.py
# class LogConsole:
#     """Log-class that outputs to console, adding colors if needed"""
# 
#     def __init__(self, show_debug, show_verbose, color):
#         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
#             self.colorama=False
#             try:
#                 import colorama
#                 global colorama
#                 self.colorama = True
#             except ImportError:
#                 pass
# 
#         else:
#             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:
#             print("! " + txt, file=sys.stderr)
#         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:
#             print("  NOTE: " + txt)
#         sys.stdout.flush()
# 
#     def verbose(self, txt):
#         if self.show_verbose:
#             self.clear_progress()
#             if self.colorama:
#                 print(colorama.Style.NORMAL + "  " + txt + colorama.Style.RESET_ALL)
#             else:
#                 print("  " + txt)
#             sys.stdout.flush()
# 
#     def debug(self, txt):
#         if self.show_debug:
#             self.clear_progress()
#             if self.colorama:
#                 print(colorama.Fore.GREEN + "# " + txt + colorama.Style.RESET_ALL)
#             else:
#                 print("# " + txt)
#             sys.stdout.flush()
# 
#     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):
#         if self._progress_uncleared:
#             import colorama
#             print(colorama.ansi.clear_line(), end='', file=sys.stderr)
#             # sys.stderr.flush()
#             self._progress_uncleared=False


from zfs_autobackup.LogConsole import LogConsole
from basetest import *
                l.verbose("verbose")
                l.debug("debug")

            with redirect_stdout(buf):
                l=LogConsole(show_verbose=True, show_debug=True, color=True)
                l.verbose("verbose")
                l.debug("debug")

            with redirect_stderr(buf):
                l=LogConsole(show_verbose=False, show_debug=False, color=True)
                l.error("error")

            print(list(buf.getvalue()))
            self.assertEqual(list(buf.getvalue()), ['\x1b', '[', '2', '2', 'm', ' ', ' ', 'v', 'e', 'r', 'b', 'o', 's', 'e', '\x1b', '[', '0', 'm', '\n', '\x1b', '[', '3', '2', 'm', '#', ' ', 'd', 'e', 'b', 'u', 'g', '\x1b', '[', '0', 'm', '\n', '\x1b', '[', '3', '1', 'm', '\x1b', '[', '1', 'm', '!', ' ', 'e', 'r', 'r', 'o', 'r', '\x1b', '[', '0', 'm', '\n'])

    def test_nocolor(self):
        """test without color output"""

        with OutputIO() as buf:
            with redirect_stdout(buf):
                l=LogConsole(show_verbose=False, show_debug=False, color=False)
                l.verbose("verbose")
                l.debug("debug")

            with redirect_stdout(buf):
                l=LogConsole(show_verbose=True, show_debug=True, color=False)
                l.verbose("verbose")
                l.debug("debug")

            with redirect_stderr(buf):

# Path: zfs_autobackup/LogConsole.py
# class LogConsole:
#     """Log-class that outputs to console, adding colors if needed"""
# 
#     def __init__(self, show_debug, show_verbose, color):
#         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
#             self.colorama=False
#             try:
#                 import colorama
#                 global colorama
#                 self.colorama = True
#             except ImportError:
#                 pass
# 
#         else:
#             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:
#             print("! " + txt, file=sys.stderr)
#         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:
#             print("  NOTE: " + txt)
#         sys.stdout.flush()
# 
#     def verbose(self, txt):
#         if self.show_verbose:
#             self.clear_progress()
#             if self.colorama:
#                 print(colorama.Style.NORMAL + "  " + txt + colorama.Style.RESET_ALL)
#             else:
#                 print("  " + txt)
#             sys.stdout.flush()
# 
#     def debug(self, txt):
#         if self.show_debug:
#             self.clear_progress()
#             if self.colorama:
#                 print(colorama.Fore.GREEN + "# " + txt + colorama.Style.RESET_ALL)
#             else:
#                 print("# " + txt)
#             sys.stdout.flush()
# 
#     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):
#         if self._progress_uncleared:
#             import colorama
#             print(colorama.ansi.clear_line(), end='', file=sys.stderr)
#             # sys.stderr.flush()
#             self._progress_uncleared=False


# Path: tests/test_log.py
from zfs_autobackup.LogConsole import LogConsole
from basetest import *

                l.verbose("verbose")
                l.debug("debug")

            with redirect_stdout(buf):
                l=LogConsole(show_verbose=True, show_debug=True, color=True)
                l.verbose("verbose")
                l.debug("debug")

            with redirect_stderr(buf):
                l=LogConsole(show_verbose=False, show_debug=False, color=True)
                l.error("error")

            print(list(buf.getvalue()))
            self.assertEqual(list(buf.getvalue()), ['\x1b', '[', '2', '2', 'm', ' ', ' ', 'v', 'e', 'r', 'b', 'o', 's', 'e', '\x1b', '[', '0', 'm', '\n', '\x1b', '[', '3', '2', 'm', '#', ' ', 'd', 'e', 'b', 'u', 'g', '\x1b', '[', '0', 'm', '\n', '\x1b', '[', '3', '1', 'm', '\x1b', '[', '1', 'm', '!', ' ', 'e', 'r', 'r', 'o', 'r', '\x1b', '[', '0', 'm', '\n'])

    def test_nocolor(self):
        """test without color output"""

        with OutputIO() as buf:
            with redirect_stdout(buf):
                l=LogConsole(show_verbose=False, show_debug=False, color=False)
                l.verbose("verbose")
                l.debug("debug")

            with redirect_stdout(buf):
                l=LogConsole(show_verbose=True, show_debug=True, color=False)
                l.verbose("verbose")
                l.debug("debug")

            with redirect_stderr(buf):

                l=LogConsole(show_verbose=False, show_debug=False, color=False)