1use anyhow::Result;
2
3use super::App;
4use crate::db::Space as SpaceRow;
5
6impl App {
7 pub fn switch_to_default_space(&mut self) -> Result<()> {
10 let default_id = self.db.default_space_id()?;
11 let row = self
12 .db
13 .list_spaces()?
14 .into_iter()
15 .find(|s| s.id == default_id)
16 .ok_or_else(|| anyhow::anyhow!("default space {default_id:?} no longer exists"))?;
17 self.set_active_space(row);
18 Ok(())
19 }
20
21 pub fn set_active_space(&mut self, row: SpaceRow) {
25 self.active_space = row;
26 self.session = None;
27 self.messages.clear();
28 self.refresh_memory_snapshot();
29 self.context_total = None;
30 self.push_viewport_reset();
31 self.cleanup_incognito_images();
32 self.rescan_files();
33 self.refresh_toolbox();
34 self.push_status(format!("space: {}", self.active_space.name));
35 }
36
37 pub fn instructions_path_for_space(&self, name: &str) -> Option<std::path::PathBuf> {
42 let path = self.space.instructions_path(name);
43 if !path.exists() {
44 let _ = std::fs::write(
45 &path,
46 format!("<!-- instructions for the \"{name}\" space -->\n"),
47 );
48 }
49 Some(path)
50 }
51
52 pub fn memory_path_for_space(&self, name: &str) -> Option<std::path::PathBuf> {
56 let path = self.space.memory_path(name);
57 if !path.exists() {
58 let _ = std::fs::write(
59 &path,
60 format!(
61 "<!-- memory for the \"{name}\" space — numbered facts, one per line -->\n"
62 ),
63 );
64 }
65 Some(path)
66 }
67}