@@ -231,6 +231,33 @@ And some text.
231
231
)
232
232
}
233
233
234
+ /// Extracts the contents of a code block.
235
+ ///
236
+ /// If the language is `ansi`, then ANSI escape codes will be stripped from the input.
237
+ struct CodeBlock {
238
+ source : String ,
239
+ }
240
+
241
+ #[ async_trait]
242
+ impl < ' a > poise:: PopArgument < ' a > for CodeBlock {
243
+ async fn pop_from (
244
+ args : & ' a str ,
245
+ attachment_index : usize ,
246
+ ctx : & serenity:: prelude:: Context ,
247
+ message : & poise:: serenity_prelude:: Message ,
248
+ ) -> Result < ( & ' a str , usize , Self ) , ( PoiseError , Option < String > ) > {
249
+ let ( rest, attachment_index, code_block) =
250
+ poise:: prefix_argument:: CodeBlock :: pop_from ( args, attachment_index, ctx, message) . await ?;
251
+
252
+ let mut source = code_block. code ;
253
+ // Strip ANSI escapes if provided.
254
+ if code_block. language . as_deref ( ) == Some ( "ansi" ) {
255
+ source = strip_ansi_escapes:: strip_str ( source) ;
256
+ }
257
+ Ok ( ( rest, attachment_index, CodeBlock { source } ) )
258
+ }
259
+ }
260
+
234
261
struct Rest ;
235
262
236
263
#[ async_trait]
@@ -257,12 +284,12 @@ impl<'a> poise::PopArgument<'a> for Rest {
257
284
async fn render (
258
285
ctx : Context < ' _ > ,
259
286
#[ description = "Flags" ] flags : RenderFlags ,
260
- #[ description = "Code to render" ] code : poise :: prefix_argument :: CodeBlock ,
287
+ #[ description = "Code to render" ] code : CodeBlock ,
261
288
_ignored : Rest ,
262
289
) -> Result < ( ) , PoiseError > {
263
290
let pool = & ctx. data ( ) . pool ;
264
291
265
- let mut source = code. code ;
292
+ let mut source = code. source ;
266
293
source. insert_str ( 0 , & flags. preamble . preamble ( ) ) ;
267
294
268
295
let mut progress = String :: new ( ) ;
@@ -374,12 +401,12 @@ async fn source(ctx: Context<'_>) -> Result<(), PoiseError> {
374
401
#[ poise:: command( prefix_command, track_edits, broadcast_typing) ]
375
402
async fn ast (
376
403
ctx : Context < ' _ > ,
377
- #[ description = "Code to parse" ] code : poise :: prefix_argument :: CodeBlock ,
404
+ #[ description = "Code to parse" ] code : CodeBlock ,
378
405
_ignored : Rest ,
379
406
) -> Result < ( ) , PoiseError > {
380
407
let pool = & ctx. data ( ) . pool ;
381
408
382
- let res = pool. lock ( ) . await . ast ( code. code ) . await ;
409
+ let res = pool. lock ( ) . await . ast ( code. source ) . await ;
383
410
384
411
match res {
385
412
Ok ( ast) => {
0 commit comments