diff options
| author | sigoden <sigoden@gmail.com> | 2024-03-27 11:20:00 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-27 11:20:00 +0800 |
| commit | 68a1d98a3dd583c506447e8035f07ea05408f4dd (patch) | |
| tree | 8edf274b19b5323b55355ccaf733ea68dbcd45e4 /src/config | |
| parent | 4cfd6c8e9fd516167317de439c8136a7481dc2bb (diff) | |
| download | aichat-68a1d98a3dd583c506447e8035f07ea05408f4dd.tar.gz | |
feat: add repl command `.save session` (#382)
Diffstat (limited to 'src/config')
| -rw-r--r-- | src/config/mod.rs | 39 | ||||
| -rw-r--r-- | src/config/session.rs | 3 |
2 files changed, 27 insertions, 15 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)?; diff --git a/src/config/session.rs b/src/config/session.rs index 6ab855c..801615f 100644 --- a/src/config/session.rs +++ b/src/config/session.rs @@ -241,9 +241,6 @@ impl Session { } pub fn save(&mut self, session_path: &Path) -> Result<()> { - if !self.dirty { - return Ok(()); - } self.path = Some(session_path.display().to_string()); let content = serde_yaml::to_string(&self) |
