nexus_core/app/
scripts.rs1#![allow(
5 clippy::cast_possible_truncation,
6 clippy::cast_possible_wrap,
7 clippy::cast_precision_loss,
8 clippy::cast_sign_loss
9)]
10use super::App;
11
12impl App {
13 pub fn refresh_scripts(&mut self) {
17 let dir = self.space.scripts_dir(&self.active_space.name);
18 let _ = std::fs::create_dir_all(&dir);
19 self.scripts_cache = match std::fs::read_dir(&dir) {
20 Err(_) => Vec::new(),
21 Ok(entries) => entries
22 .flatten()
23 .filter(|e| e.path().is_file())
24 .filter_map(|e| {
25 let meta = e.metadata().ok()?;
26 let modified = meta
27 .modified()
28 .ok()
29 .and_then(|t| {
30 chrono::DateTime::from_timestamp(
31 t.duration_since(std::time::UNIX_EPOCH).ok()?.as_secs() as i64,
32 0,
33 )
34 })
35 .map(|dt| dt.to_rfc3339())
36 .unwrap_or_default();
37 Some(super::ScriptMeta {
38 name: e.file_name().to_string_lossy().to_string(),
39 size: meta.len(),
40 modified,
41 })
42 })
43 .collect(),
44 };
45 self.scripts_cache.sort_by(|a, b| a.name.cmp(&b.name));
46 }
47
48 pub fn ensure_script_file(&mut self, name: &str) -> anyhow::Result<std::path::PathBuf> {
52 use anyhow::Context as _;
53 let dir = self.space.scripts_dir(&self.active_space.name);
54 std::fs::create_dir_all(&dir).with_context(|| format!("creating {}", dir.display()))?;
55 let path = dir.join(name);
56 if !path.exists() {
57 std::fs::write(&path, "").with_context(|| format!("creating {}", path.display()))?;
58 }
59 self.refresh_scripts();
60 Ok(path)
61 }
62
63 pub fn rename_script_file(&mut self, from: &str, to: &str) -> anyhow::Result<()> {
67 use anyhow::Context as _;
68 let dir = self.space.scripts_dir(&self.active_space.name);
69 let from_path = dir.join(from);
70 let to_path = dir.join(to);
71 if to_path.exists() {
72 anyhow::bail!("{to} already exists");
73 }
74 std::fs::rename(&from_path, &to_path).with_context(|| {
75 format!("renaming {} to {}", from_path.display(), to_path.display())
76 })?;
77 self.refresh_scripts();
78 Ok(())
79 }
80
81 pub fn delete_script_file(&mut self, name: &str) -> anyhow::Result<bool> {
84 use anyhow::Context as _;
85 let dir = self.space.scripts_dir(&self.active_space.name);
86 let path = dir.join(name);
87 if path.exists() {
88 std::fs::remove_file(&path).with_context(|| format!("removing {}", path.display()))?;
89 self.refresh_scripts();
90 Ok(true)
91 } else {
92 Ok(false)
93 }
94 }
95}