use anyhow::Result;
use std::path::{Path, PathBuf};
fn record() -> PathBuf {
PathBuf::from(super::home_dir()).join(".bctx/created-files")
}
fn entries() -> Vec<String> {
std::fs::read_to_string(record())
.map(|s| s.lines().map(String::from).collect())
.unwrap_or_default()
}
fn save(all: &[String]) -> Result<()> {
std::fs::create_dir_all(record().parent().expect("record has a parent"))?;
let mut text = all.join("\n");
if !text.is_empty() {
text.push('\n');
}
std::fs::write(record(), text)?;
Ok(())
}
pub(crate) fn note(path: &Path) -> Result<()> {
let p = path.to_string_lossy().into_owned();
let mut all = entries();
if !all.contains(&p) {
all.push(p);
save(&all)?;
}
Ok(())
}
pub(crate) fn was_created(path: &Path) -> bool {
entries().contains(&path.to_string_lossy().into_owned())
}
pub(crate) fn forget(path: &Path) -> Result<()> {
let p = path.to_string_lossy().into_owned();
save(
&entries()
.into_iter()
.filter(|e| e != &p)
.collect::<Vec<_>>(),
)
}