Skip to content

Commit 75e691a

Browse files
authored
Merge pull request #17 from frozolotl/strip-ansi
Strip ANSI escape codes
2 parents 95d8359 + c5b147d commit 75e691a

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

Cargo.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bot/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ poise = { version = "0.6", git = "https://github.com/serenity-rs/poise", default
1111
"cache",
1212
] }
1313
protocol = { path = "../protocol" }
14+
rusqlite = { version = "0.29", features = ["bundled"] }
1415
serde = { version = "1", features = ["derive"] }
1516
serenity = { version = "0.12", default_features = false, features = [
1617
"rustls_backend",
1718
] }
18-
rusqlite = { version = "0.29", features = ["bundled"] }
19+
strip-ansi-escapes = "0.2.0"
1920
thiserror = "1"
2021
tokio = { version = "1", features = ["rt", "macros", "sync"] }
2122
tracing = "0.1"

bot/src/bot.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,33 @@ And some text.
231231
)
232232
}
233233

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+
234261
struct Rest;
235262

236263
#[async_trait]
@@ -257,12 +284,12 @@ impl<'a> poise::PopArgument<'a> for Rest {
257284
async fn render(
258285
ctx: Context<'_>,
259286
#[description = "Flags"] flags: RenderFlags,
260-
#[description = "Code to render"] code: poise::prefix_argument::CodeBlock,
287+
#[description = "Code to render"] code: CodeBlock,
261288
_ignored: Rest,
262289
) -> Result<(), PoiseError> {
263290
let pool = &ctx.data().pool;
264291

265-
let mut source = code.code;
292+
let mut source = code.source;
266293
source.insert_str(0, &flags.preamble.preamble());
267294

268295
let mut progress = String::new();
@@ -374,12 +401,12 @@ async fn source(ctx: Context<'_>) -> Result<(), PoiseError> {
374401
#[poise::command(prefix_command, track_edits, broadcast_typing)]
375402
async fn ast(
376403
ctx: Context<'_>,
377-
#[description = "Code to parse"] code: poise::prefix_argument::CodeBlock,
404+
#[description = "Code to parse"] code: CodeBlock,
378405
_ignored: Rest,
379406
) -> Result<(), PoiseError> {
380407
let pool = &ctx.data().pool;
381408

382-
let res = pool.lock().await.ast(code.code).await;
409+
let res = pool.lock().await.ast(code.source).await;
383410

384411
match res {
385412
Ok(ast) => {

0 commit comments

Comments
 (0)