Skip to content

Commit dda6b94

Browse files
authored
Bulk code-highlighting support (#34)
* * Fix improper line display * Add bulk-highlighting * Use the final file extension as our way of formatting (via rsplit()) * Add a message if the selected lines exceed the max message size * ignore type
1 parent f2cd99b commit dda6b94

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

modules/github.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
8282
code = code.splitlines()
8383
code_block_dict: dict[str, dict[int, str]] = {"lines": {}}
8484

85+
line_start = match["linestart"]
86+
line_end = match["lineend"]
87+
88+
bulk = False # are we highlighting a specific block of code?
89+
90+
line_start = int(line_start)
91+
line_end = int(line_end) if line_end is not None else 0
92+
93+
if line_end != 0:
94+
bulk = True
95+
8596
j = 0
8697
for i in code:
8798
# populate the dict
@@ -94,13 +105,22 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
94105
if highlighted_line - 1 not in line_list:
95106
return None
96107

97-
bound_adj = line_adjustment # adjustment for upper and lower bound display
98-
_min_boundary = highlighted_line - 1 - bound_adj
99-
_max_boundary = highlighted_line - 1 + bound_adj
108+
bound_adj = line_adjustment # adjustment for upper and lower bound display.
109+
_min_boundary = highlighted_line - 1 - bound_adj if bulk is False else line_start - 1
110+
_max_boundary = highlighted_line - 1 + bound_adj if bulk is False else line_end - 1
111+
112+
# is our minimum greater than our maximum?
113+
if _min_boundary > _max_boundary:
114+
# re-arrange the variables so we get the proper min - max scale
115+
_min = _min_boundary
116+
_max = _max_boundary
117+
118+
_min_boundary = _max
119+
_max_boundary = _min
100120

101121
# get the file extension to format nicely
102122
file = match["file"]
103-
extension = file.split(".")[1]
123+
extension = file.rsplit(".")[-1]
104124

105125
msg = f"```{extension}\n"
106126
key = _min_boundary
@@ -127,10 +147,14 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
127147

128148
msg += "\n```"
129149

150+
path = match["path"]
151+
file_path = f"{path}/{file}"
152+
130153
github_dict = {
131154
"path": file_path,
132-
"min": _min_boundary if _min_boundary > 0 else highlighted_line, # Do not display negative numbers if <0
133-
"max": _max_boundary,
155+
"min": (_min_boundary if _min_boundary > 0 else highlighted_line - 1)
156+
+ 1, # Do not display negative numbers if <0
157+
"max": _max_boundary + 1,
134158
"msg": msg,
135159
}
136160

@@ -161,6 +185,14 @@ async def on_message(self, message: discord.Message) -> None:
161185
_max = code_segment["max"]
162186
code_fmt = code_segment["msg"]
163187

188+
max_message_size = 2002
189+
segment_len = len(code_fmt) # type: ignore
190+
191+
# is our msg too big for the embed?
192+
if segment_len > max_message_size:
193+
# set the block msg to None in this case
194+
code_fmt = None
195+
164196
def check(reaction: discord.Reaction, user: discord.User) -> bool:
165197
return (
166198
reaction.emoji == self.code_highlight_emoji and user != self.bot.user and message.id == reaction.message.id
@@ -169,7 +201,11 @@ def check(reaction: discord.Reaction, user: discord.User) -> bool:
169201
try:
170202
await self.bot.wait_for("reaction_add", check=check, timeout=self.highlight_timeout)
171203

172-
msg: str = f"Showing lines `{_min}` - `{_max}` in: `{path}`...\n{code_fmt}"
204+
if code_fmt is None:
205+
await message.channel.send("You've selected too many lines for me to display!")
206+
return
207+
208+
msg: str = f"Showing lines `{_min}-{_max}` in: `{path}`\n{code_fmt}"
173209
await message.channel.send(msg, suppress_embeds=True)
174210

175211
except asyncio.TimeoutError:

0 commit comments

Comments
 (0)