@@ -82,6 +82,17 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
82
82
code = code .splitlines ()
83
83
code_block_dict : dict [str , dict [int , str ]] = {"lines" : {}}
84
84
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
+
85
96
j = 0
86
97
for i in code :
87
98
# populate the dict
@@ -94,13 +105,22 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
94
105
if highlighted_line - 1 not in line_list :
95
106
return None
96
107
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
100
120
101
121
# get the file extension to format nicely
102
122
file = match ["file" ]
103
- extension = file .split ("." )[1 ]
123
+ extension = file .rsplit ("." )[- 1 ]
104
124
105
125
msg = f"```{ extension } \n "
106
126
key = _min_boundary
@@ -127,10 +147,14 @@ async def format_highlight_block(self, url: str, line_adjustment: int = 10) -> d
127
147
128
148
msg += "\n ```"
129
149
150
+ path = match ["path" ]
151
+ file_path = f"{ path } /{ file } "
152
+
130
153
github_dict = {
131
154
"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 ,
134
158
"msg" : msg ,
135
159
}
136
160
@@ -161,6 +185,14 @@ async def on_message(self, message: discord.Message) -> None:
161
185
_max = code_segment ["max" ]
162
186
code_fmt = code_segment ["msg" ]
163
187
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
+
164
196
def check (reaction : discord .Reaction , user : discord .User ) -> bool :
165
197
return (
166
198
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:
169
201
try :
170
202
await self .bot .wait_for ("reaction_add" , check = check , timeout = self .highlight_timeout )
171
203
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 } "
173
209
await message .channel .send (msg , suppress_embeds = True )
174
210
175
211
except asyncio .TimeoutError :
0 commit comments