Skip to content

Commit df58de7

Browse files
authored
format spaces so that increasing amount of digits do not mis-align the lines. (#33)
1 parent 069ac55 commit df58de7

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

modules/github.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
GITHUB_ISSUE_URL = "https://github.com/{}/issues/{}"
3434
LIB_ISSUE_REGEX = re.compile(r"(?P<lib>[a-z]+)?##(?P<number>[0-9]+)", flags=re.IGNORECASE)
3535
GITHUB_CODE_REGION_REGEX = re.compile(
36-
r"https?://github\.com/(?P<user>.*)/(?P<repo>.*)/blob/(?P<hash>[a-zA-Z0-9]+)/(?P<path>.*)(?:\#L)(?P<linestart>[0-9]+)(?:-L)?(?P<lineend>[0-9]+)?"
36+
r"https?://github\.com/(?P<user>.*)/(?P<repo>.*)/blob/(?P<hash>[a-zA-Z0-9]+)/(?P<path>.*)/(?P<file>.*)(?:\#L)(?P<linestart>[0-9]+)(?:-L)?(?P<lineend>[0-9]+)?"
3737
)
3838

3939
GITHUB_BASE_URL = "https://github.com/"
@@ -98,21 +98,29 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
9898
_min_boundary = highlighted_line - 1 - bound_adj
9999
_max_boundary = highlighted_line - 1 + bound_adj
100100

101-
# loop through all the lines, and adjust the formatting
102-
msg = "```ansi\n"
101+
# get the file extension to format nicely
102+
file = match["file"]
103+
extension = file.split(".")[1]
104+
105+
msg = f"```{extension}\n"
103106
key = _min_boundary
104107

108+
max_digit = len(str(_max_boundary))
109+
110+
# loop through all our lines
105111
while key <= _max_boundary:
106112
curr_line_no: str = str(key + 1)
113+
spaced_line_no = f"%{max_digit}d" % int(curr_line_no)
107114

108115
# insert a space if there is no following char before the first character...
109116
if key + 1 == highlighted_line:
110-
highlighted_msg_format = f"\u001b[0;37m\u001b[4;31m{curr_line_no} {line_list[key]}\u001b[0;0m\n"
117+
highlighted_msg_format = f">{spaced_line_no} {line_list[key]}\n"
111118
msg += highlighted_msg_format
112119

113120
else:
114121
# if we hit the end of the file, just write an empty string
115-
display_str = "{} {}\n" if line_list.get(key) is not None else ""
122+
display_str = " {} {}\n" if line_list.get(key) is not None else ""
123+
curr_line_no = spaced_line_no
116124
msg += display_str.format(curr_line_no, line_list.get(key))
117125

118126
key += 1

0 commit comments

Comments
 (0)