ldcheck.py

changeset 98
f9d4e59392f7
parent 94
109fb7cf658f
child 100
62759e5c4554
--- a/ldcheck.py	Tue Aug 25 21:58:09 2020 +0300
+++ b/ldcheck.py	Tue Aug 25 22:08:30 2020 +0300
@@ -80,6 +80,31 @@
             ))
         exit(0)
 
+def format_report(report, model, test_suite, *, use_colors = True):
+    from testsuite import problem_text
+    messages = []
+    for problem in report['problems']:
+        text_colour = ''
+        if use_colors:
+            if problem.severity == 'hold':
+                text_colour = colorama.Fore.LIGHTRED_EX
+            elif problem.severity == 'warning':
+                text_colour = colorama.Fore.LIGHTBLUE_EX
+        ldraw_code = model.body[problem.body_index].textual_representation()
+        message = str.format(
+            '{text_colour}{model_name}:{line_number}: {problem_type}: {message}'
+            '{colour_reset}\n\t{ldraw_code}',
+            text_colour = text_colour,
+            model_name = model.name,
+            line_number = problem.line_number,
+            problem_type = problem.severity,
+            message = problem_text(problem, test_suite),
+            colour_reset = use_colors and colorama.Fore.RESET or '',
+            ldraw_code = ldraw_code,
+        )
+        messages.append(message)
+    return '\n'.join(messages)
+
 if __name__ == '__main__':
     from sys import argv
     parser = argparse.ArgumentParser()
@@ -101,11 +126,23 @@
         action = 'store_true',
         help = 'finds a subfile by name and prints out information about it'
     )
+    parser.add_argument('--color',
+        action = 'store_true',
+        help = 'use colors'
+    )
     args = parser.parse_args()
     config = load_config('ldcheck.cfg')
     for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
         with ldconfig_ldr_path.open() as ldconfig_ldr:
             load_colours(ldconfig_ldr)
+    if args.color:
+        try:
+            import colorama
+            colorama.init()
+        except ImportError:
+            from sys import stderr
+            print('Use of --color requires the colorama module, disabling colours', file = stderr)
+            args.color = False
     if args.subfile:
         import filecache
         cache = filecache.SubfileCache(
@@ -138,7 +175,7 @@
                 for entry in model.body:
                     print(entry.textual_representation(), end = '\r\n')
             else:
-                from testsuite import load_tests, check_model, format_report
+                from testsuite import load_tests, check_model
                 test_suite = load_tests()
                 report = check_model(model, test_suite)
-                print(format_report(report, model, test_suite))
+                print(format_report(report, model, test_suite, use_colors = args.color))

mercurial