diff options
| author | sigoden <sigoden@gmail.com> | 2024-03-02 21:11:28 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-02 21:11:28 +0800 |
| commit | 3c16aff59145134885f32056fa3be9bd6bed2cce (patch) | |
| tree | f285ee126374b8815840815c7334e1d48fae2791 /src/utils/mod.rs | |
| parent | d275c33632271bf9cdbc01ef005deb9ff851370b (diff) | |
| download | aichat-3c16aff59145134885f32056fa3be9bd6bed2cce.tar.gz | |
feat: add `-c/--code` to generate only code (#327)
Diffstat (limited to 'src/utils/mod.rs')
| -rw-r--r-- | src/utils/mod.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index b59b7ad..da98cc6 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -10,10 +10,16 @@ pub use self::prompt_input::*; pub use self::render_prompt::render_prompt; pub use self::tiktoken::cl100k_base_singleton; +use fancy_regex::Regex; +use lazy_static::lazy_static; use sha2::{Digest, Sha256}; use std::env; use std::process::Command; +lazy_static! { + static ref CODE_BLOCK_RE: Regex = Regex::new(r"(?ms)```\w*(.*?)```").unwrap(); +} + pub fn now() -> String { let now = chrono::Local::now(); now.to_rfc3339_opts(chrono::SecondsFormat::Secs, false) @@ -150,6 +156,18 @@ pub fn run_command(eval_str: &str) -> anyhow::Result<i32> { Ok(status.code().unwrap_or_default()) } +pub fn extract_block(input: &str) -> String { + let output: String = CODE_BLOCK_RE + .captures_iter(input) + .filter_map(|m| { + m.ok() + .and_then(|cap| cap.get(1)) + .map(|m| String::from(m.as_str())) + }) + .collect(); + output.trim().to_string() +} + #[cfg(test)] mod tests { use super::*; |
