summaryrefslogtreecommitdiffstats
path: root/src/config/mod.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-03-27 11:20:00 +0800
committerGitHub <noreply@github.com>2024-03-27 11:20:00 +0800
commit68a1d98a3dd583c506447e8035f07ea05408f4dd (patch)
tree8edf274b19b5323b55355ccaf733ea68dbcd45e4 /src/config/mod.rs
parent4cfd6c8e9fd516167317de439c8136a7481dc2bb (diff)
downloadaichat-68a1d98a3dd583c506447e8035f07ea05408f4dd.tar.gz
feat: add repl command `.save session` (#382)
Diffstat (limited to 'src/config/mod.rs')
-rw-r--r--src/config/mod.rs39
1 files changed, 27 insertions, 12 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 2bde0f7..b18bc74 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -423,7 +423,7 @@ impl Config {
}
}
- pub fn sys_info(&self) -> Result<String> {
+ pub fn system_info(&self) -> Result<String> {
let display_path = |path: &Path| path.display().to_string();
let wrap = self
.wrap
@@ -485,7 +485,7 @@ impl Config {
} else if let Some(role) = &self.role {
role.export()
} else {
- self.sys_info()
+ self.system_info()
}
}
@@ -647,17 +647,18 @@ impl Config {
session.name = Text::new("Session name:").prompt()?;
}
}
- let session_path = Self::session_file(session.name())?;
- let sessions_dir = session_path.parent().ok_or_else(|| {
- anyhow!("Unable to save session file to {}", session_path.display())
- })?;
- if !sessions_dir.exists() {
- create_dir_all(sessions_dir).with_context(|| {
- format!("Failed to create session_dir '{}'", sessions_dir.display())
- })?;
- }
- session.save(&session_path)?;
+ Self::save_session_to_file(&mut session)?;
+ }
+ }
+ Ok(())
+ }
+
+ pub fn save_session(&mut self, name: &str) -> Result<()> {
+ if let Some(session) = self.session.as_mut() {
+ if !name.is_empty() {
+ session.name = name.to_string();
}
+ Self::save_session_to_file(session)?;
}
Ok(())
}
@@ -870,6 +871,20 @@ impl Config {
.with_context(|| format!("Failed to create/append {}", path.display()))
}
+ fn save_session_to_file(session: &mut Session) -> Result<()> {
+ let session_path = Self::session_file(session.name())?;
+ let sessions_dir = session_path
+ .parent()
+ .ok_or_else(|| anyhow!("Unable to save session file to {}", session_path.display()))?;
+ if !sessions_dir.exists() {
+ create_dir_all(sessions_dir).with_context(|| {
+ format!("Failed to create session_dir '{}'", sessions_dir.display())
+ })?;
+ }
+ session.save(&session_path)?;
+ Ok(())
+ }
+
fn load_config(config_path: &Path) -> Result<Self> {
let ctx = || format!("Failed to load config at {}", config_path.display());
let content = read_to_string(config_path).with_context(ctx)?;