Skip to main content

aurora_modules/
note.rs

1use aurora_core::{AuroraResult, Pipeline, Value};
2use std::fs;
3use std::io::Write;
4use std::path::PathBuf;
5use std::time::{SystemTime, UNIX_EPOCH};
6
7fn notes_path() -> PathBuf {
8    let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".into());
9    PathBuf::from(home).join(".cache").join("aurora").join("notes.txt")
10}
11
12fn ensure_dir(path: &PathBuf) -> AuroraResult<()> {
13    if let Some(parent) = path.parent() {
14        fs::create_dir_all(parent)
15            .map_err(|e| aurora_core::AuroraError::Io(e))?;
16    }
17    Ok(())
18}
19
20pub fn note_add(text: &[String]) -> AuroraResult<Pipeline> {
21    let path = notes_path();
22    ensure_dir(&path)?;
23
24    let timestamp = SystemTime::now()
25        .duration_since(UNIX_EPOCH)
26        .map(|d| d.as_secs())
27        .unwrap_or(0);
28
29    let line = format!("[{}] {}\n", timestamp, text.join(" "));
30
31    let mut file = fs::OpenOptions::new()
32        .create(true)
33        .append(true)
34        .open(&path)
35        .map_err(|e| aurora_core::AuroraError::Io(e))?;
36
37    file.write_all(line.as_bytes())
38        .map_err(|e| aurora_core::AuroraError::Io(e))?;
39
40    Ok(Pipeline::table(
41        vec!["action".into(), "note".into(), "file".into()],
42        vec![vec![
43            Value::String("add".into()),
44            Value::String(text.join(" ")),
45            Value::String(path.to_string_lossy().into()),
46        ]],
47    ))
48}