Skip to content
Snippets Groups Projects
Commit 1650a1e2 authored by Frank Sauerburger's avatar Frank Sauerburger
Browse files

Add support for plain output

Make ASCII escape characters optional. Colored output can be deactivated with
`--plain` or the environment variable `DOXEC_COLOR=0'. The environment variable
can be overridden with `--color`.
parent 8085866a
No related branches found
No related tags found
1 merge request!4Resolve "Colored output should be optional"
Pipeline #
......@@ -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
......
......@@ -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):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment