factor common code into display_underlined

This commit is contained in:
Michael Hohn
2021-11-17 15:56:43 -08:00
committed by =Michael Hohn
parent f5bb156c8c
commit 90758f769f
2 changed files with 18 additions and 16 deletions

View File

@@ -37,14 +37,7 @@ for runi in S.indices(sarif_struct, 'runs'):
if args.list_source: if args.list_source:
lines = S.load_lines(args.list_source, artifact['uri'], l1, l2) lines = S.load_lines(args.list_source, artifact['uri'], l1, l2)
for line, line_num in zip(lines, range(l1, l2+1)): for line, line_num in zip(lines, range(l1, l2+1)):
# Display the line S.display_underlined(l1, c1, l2, c2, line, line_num)
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")
if args.related_locations: if args.related_locations:
# Full path: S.get(sarif_struct, 'runs', runi, 'results', resi, 'relatedLocations') # Full path: S.get(sarif_struct, 'runs', runi, 'results', resi, 'relatedLocations')
relatedLocations = results.get('relatedLocations', None) relatedLocations = results.get('relatedLocations', None)
@@ -60,12 +53,5 @@ for runi in S.indices(sarif_struct, 'runs'):
if args.list_source: if args.list_source:
lines = S.load_lines(args.list_source, artifact['uri'], l1, l2) lines = S.load_lines(args.list_source, artifact['uri'], l1, l2)
for line, line_num in zip(lines, range(l1, l2+1)): for line, line_num in zip(lines, range(l1, l2+1)):
# Display the line S.display_underlined(l1, c1, l2, c2, line, line_num)
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.msg("\n") S.msg("\n")

View File

@@ -6,6 +6,22 @@ MIN_PYTHON = (3, 7)
if sys.version_info < MIN_PYTHON: if sys.version_info < MIN_PYTHON:
sys.exit("Python %s.%s or later is required.\n" % 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): def underline_for_result(first_line, first_column, last_line, last_column, line, line_num):
"""Provide the underline for a result line. """Provide the underline for a result line.