From 90758f769f11bbb32aa9ae76d99dce59004f5a50 Mon Sep 17 00:00:00 2001 From: Michael Hohn Date: Wed, 17 Nov 2021 15:56:43 -0800 Subject: [PATCH] factor common code into `display_underlined` --- bin/sarif-results-summary | 18 ++---------------- sarif_cli/__init__.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/bin/sarif-results-summary b/bin/sarif-results-summary index d854294..350d3f0 100755 --- a/bin/sarif-results-summary +++ b/bin/sarif-results-summary @@ -37,14 +37,7 @@ for runi in S.indices(sarif_struct, 'runs'): if args.list_source: lines = S.load_lines(args.list_source, artifact['uri'], l1, l2) for line, line_num in zip(lines, range(l1, l2+1)): - # Display the line - S.msg("%s" % (line)) - S.msg("\n") - # Print the underline - underline = S.underline_for_result(l1, c1, l2, c2, line, line_num) - S.msg(underline) - # Next result - S.msg("\n") + S.display_underlined(l1, c1, l2, c2, line, line_num) if args.related_locations: # Full path: S.get(sarif_struct, 'runs', runi, 'results', resi, 'relatedLocations') relatedLocations = results.get('relatedLocations', None) @@ -60,12 +53,5 @@ for runi in S.indices(sarif_struct, 'runs'): if args.list_source: lines = S.load_lines(args.list_source, artifact['uri'], l1, l2) for line, line_num in zip(lines, range(l1, l2+1)): - # Display the line - S.msg("%s" % (line)) - S.msg("\n") - # Print the underline - underline = S.underline_for_result(l1, c1, l2, c2, line, line_num) - S.msg(underline) - # Next result - S.msg("\n") + S.display_underlined(l1, c1, l2, c2, line, line_num) S.msg("\n") diff --git a/sarif_cli/__init__.py b/sarif_cli/__init__.py index 5352e3b..36123b2 100644 --- a/sarif_cli/__init__.py +++ b/sarif_cli/__init__.py @@ -6,6 +6,22 @@ MIN_PYTHON = (3, 7) if sys.version_info < MIN_PYTHON: sys.exit("Python %s.%s or later is required.\n" % MIN_PYTHON) +def display_underlined(l1, c1, l2, c2, line, line_num): + """ Display the given line followed by a second line with underscores at the locations. + + l1, c1, l2, c2: the line/column range + line: the line of text + line_num: the line number for the text, used with the line/column range + """ + # Display the line + msg("%s" % (line)) + msg("\n") + # Print the underline + underline = underline_for_result(l1, c1, l2, c2, line, line_num) + msg(underline) + # Next result + msg("\n") + def underline_for_result(first_line, first_column, last_line, last_column, line, line_num): """Provide the underline for a result line.