summaryrefslogtreecommitdiffstats
path: root/src/utils/variables.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-09-10 18:35:34 +0800
committerGitHub <noreply@github.com>2024-09-10 18:35:34 +0800
commite181ae9b0d4814a7c005c94837426126dde63c6c (patch)
treeed727036d586f0673198a4b30217bbe2e34a094b /src/utils/variables.rs
parent84e9515509c559ed01e4b0a67539f10cd2c065e6 (diff)
downloadaichat-e181ae9b0d4814a7c005c94837426126dde63c6c.tar.gz
refactor: extract built-in roles to embedded files (#853)
Diffstat (limited to 'src/utils/variables.rs')
-rw-r--r--src/utils/variables.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/utils/variables.rs b/src/utils/variables.rs
new file mode 100644
index 0000000..91c9912
--- /dev/null
+++ b/src/utils/variables.rs
@@ -0,0 +1,33 @@
+use super::*;
+use fancy_regex::{Captures, Regex};
+
+lazy_static::lazy_static! {
+ pub static ref RE_VARIABLE: Regex = Regex::new(r"\{\{(\w+)\}\}").unwrap();
+}
+pub fn interpolate_variables(text: &mut String) {
+ *text = RE_VARIABLE
+ .replace_all(text, |caps: &Captures<'_>| {
+ let key = &caps[1];
+ match key {
+ "__os__" => env::consts::OS.to_string(),
+ "__os_distro__" => {
+ let info = os_info::get();
+ if env::consts::OS == "linux" {
+ format!("{info} (linux)")
+ } else {
+ info.to_string()
+ }
+ }
+ "__os_family__" => env::consts::FAMILY.to_string(),
+ "__arch__" => env::consts::ARCH.to_string(),
+ "__shell__" => SHELL.name.clone(),
+ "__locale__" => sys_locale::get_locale().unwrap_or_default(),
+ "__now__" => now(),
+ "__cwd__" => env::current_dir()
+ .map(|v| v.display().to_string())
+ .unwrap_or_default(),
+ _ => format!("{{{{{}}}}}", key),
+ }
+ })
+ .to_string();
+}