diff --git a/bin/doxec b/bin/doxec index 8eb8023d61024f0c6398ae859e236517e6440e27..433da3ca7f13350da2170e79f4d7a99b9c41b399 100755 --- a/bin/doxec +++ b/bin/doxec @@ -3,6 +3,7 @@ import argparse import doxec import sys +import os if __name__ == "__main__": parser = argparse.ArgumentParser( @@ -21,6 +22,12 @@ if __name__ == "__main__": parser.add_argument("--short", action="store_true", help="Suppresses the standard output of operations.") + parser.add_argument("--plain", action="store_true", + help="Do no use ASCII control characters, equal to DOXEC_COLOR=0.") + + parser.add_argument("--color", action="store_true", + help="Force the usage ASCII control characters. This overrides DOXEC_COLOR=0.") + parser.add_argument("documents", metavar="DOCUMENT", nargs="+", default=[], help="A document from which the code examples should be parsed and " "executed") @@ -33,19 +40,30 @@ if __name__ == "__main__": parser = doxec.parser[args.syntax] + plain = args.plain or \ + ("DOXEC_COLOR" in os.environ and os.environ["DOXEC_COLOR"] == '0') + + plain = plain and (not args.color) + + print("Doxec -- Copyright (c) 2017 Frank Sauerburger") # Loop over documents for doc_path in args.documents: doc = doxec.Document(doc_path, syntax=parser) - monitor = doxec.Monitor(doc_path, short=args.short) + monitor = doxec.Monitor(doc_path, short=args.short, color=not plain) doc.run(monitor=monitor) # Print summary print("-"*80) color = "\033[31m" if monitor.fail_count > 0 else "\033[32m" - print(color + "Failed: %5d\033[0m" % monitor.fail_count) + if not plain: print(color, end="") + print("Failed: %5d" % monitor.fail_count, end="") + if not plain: print("\033[0m", end="") + print() + + print("Total: %5d" % monitor.total_count) # Return code equals number of failed operations diff --git a/doxec/__init__.py b/doxec/__init__.py index b4c3dad1fcb226d4f477ddf00ee2d023a4a5a29a..a7b51168ec515483dac569ca0092da184d0bedd5 100644 --- a/doxec/__init__.py +++ b/doxec/__init__.py @@ -440,7 +440,7 @@ class Monitor(metaclass=abc.ABCMeta): of a fail and total counter. """ - def __init__(self, path, short=False): + def __init__(self, path, short=False, color=True): """ Initializes the monitor. A new monitor should be used for each file. The short argument, defines whether the standard output of operations @@ -449,6 +449,7 @@ class Monitor(metaclass=abc.ABCMeta): self.full_path = os.path.abspath(path) self.short = short + self.color = color self.first_line = False @@ -461,7 +462,10 @@ class Monitor(metaclass=abc.ABCMeta): This method set the internal values and caches the lines and the operation. """ - print("\033[2K\033[0G\033[33m%s:%-5d %s ... \033[39;49m" % (self.full_path, line, operation), end="") + if self.color: print("\033[2K\033[0G\033[33m", end="") + print("%s:%-5d %s ... " % (self.full_path, line, operation), end="") + if self.color: print("\033[39;49m", end="") + self.pending_line_break = True self.line = line self.operation = operation @@ -475,7 +479,10 @@ class Monitor(metaclass=abc.ABCMeta): """ self.total_count += 1 if error is None: - print("\033[2K\033[0G\033[32m%s:%-5d %s ... done\033[39;49m" % (self.full_path, self.line, self.operation)) + if self.color: print("\033[2K\033[0G\033[32m", end="") + print("%s:%-5d %s ... done" % (self.full_path, self.line, self.operation), end="") + if self.color: print("\033[39;49m", end="") + print() else: self.fail_count += 1 if self.pending_line_break: @@ -487,8 +494,13 @@ class Monitor(metaclass=abc.ABCMeta): else: args = [str(error)] for error_line in args: - print("\033[31m%s" % str(error_line)) - print("\033[31m%s:%-5d %s ... failed\033[39;49m" % (self.full_path, self.line, self.operation)) + if self.color: print("\033[31m") + print(str(error_line)) + + if self.color: print("\033[31m", end="") + print("%s:%-5d %s ... failed" % (self.full_path, self.line, self.operation), end="") + if self.color: print("\033[39;49m", end="") + print() def log(self, lines): """