This commit is contained in:
Edwin Eefting
2021-04-13 17:07:01 +02:00
parent bef3be4955
commit 85d493469d
3 changed files with 25 additions and 25 deletions

View File

@ -3,27 +3,29 @@ from __future__ import print_function
import sys
colorama = False
if sys.stdout.isatty():
try:
import colorama
except ImportError:
colorama = False
pass
class LogConsole:
"""Log-class that outputs to console, adding colors if needed"""
def __init__(self, show_debug=False, show_verbose=False):
def __init__(self, show_debug, show_verbose, color):
self.last_log = ""
self.show_debug = show_debug
self.show_verbose = show_verbose
@staticmethod
def error(txt):
if colorama:
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):
if self.colorama:
print(colorama.Fore.RED + colorama.Style.BRIGHT + "! " + txt + colorama.Style.RESET_ALL, file=sys.stderr)
else:
print("! " + txt, file=sys.stderr)
@ -31,7 +33,7 @@ class LogConsole:
def verbose(self, txt):
if self.show_verbose:
if colorama:
if self.colorama:
print(colorama.Style.NORMAL + " " + txt + colorama.Style.RESET_ALL)
else:
print(" " + txt)
@ -39,7 +41,7 @@ class LogConsole:
def debug(self, txt):
if self.show_debug:
if colorama:
if self.colorama:
print(colorama.Fore.GREEN + "# " + txt + colorama.Style.RESET_ALL)
else:
print("# " + txt)

View File

@ -131,7 +131,7 @@ class ZfsAutobackup:
if args.destroy_incompatible:
args.rollback = True
self.log = LogConsole(show_debug=self.args.debug, show_verbose=self.args.verbose)
self.log = LogConsole(show_debug=self.args.debug, show_verbose=self.args.verbose, color=sys.stdout.isatty())
if args.resume:
self.verbose("NOTE: The --resume option isn't needed anymore (its autodetected now)")