diff options
Diffstat (limited to 'pkgs/by-name/ts/tskm/src/interface/neorg')
-rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs | 79 | ||||
-rw-r--r-- | pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs | 18 |
2 files changed, 97 insertions, 0 deletions
diff --git a/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs new file mode 100644 index 00000000..ea11f1e2 --- /dev/null +++ b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs @@ -0,0 +1,79 @@ +use std::{ + env, + fs::{self, read_to_string, OpenOptions}, + io::Write, + process::Command, +}; + +use anyhow::{bail, Result}; + +use crate::cli::NeorgCommand; + +pub fn handle(command: NeorgCommand) -> Result<()> { + match command { + NeorgCommand::Task { id } => { + let project = id.project()?; + let path = dirs::data_local_dir() + .expect("This should exists") + .join("notes") + .join(project.get_neorg_path()?); + + fs::create_dir_all(path.parent().expect("This should exist"))?; + + { + let contents = read_to_string(&path)?; + if contents.contains(format!("% {}", id.to_uuid()?).as_str()) { + let mut options = OpenOptions::new(); + options.append(true).create(true); + + let mut file = options.open(&path)?; + file.write_all(format!("* TITLE (% {})", id.to_uuid()?).as_bytes())?; + } + } + + let editor = env::var("EDITOR").unwrap_or("nvim".to_owned()); + let status = Command::new(editor) + .args([ + path.to_str().expect("Should be a utf-8 str"), + "-c", + format!("/% {}", id.to_uuid()?).as_str(), + ]) + .status()?; + if !status.success() { + bail!("$EDITOR fail with error code: {status}"); + } + + { + let status = Command::new("git") + .args(["add", "."]) + .current_dir(&path) + .status()?; + if !status.success() { + bail!("Git add . failed!"); + } + + let status = Command::new("git") + .args([ + "commit", + "--message", + format!( + "chore({}): Update", + path.parent().expect("Should have a parent").display() + ) + .as_str(), + "--no-gpg-sign", + ]) + .current_dir(&path) + .status()?; + if !status.success() { + bail!("Git commit failed!"); + } + } + + { + id.annotate("[neorg data]]")?; + } + } + } + Ok(()) +} diff --git a/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs b/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs new file mode 100644 index 00000000..dc5cdf19 --- /dev/null +++ b/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs @@ -0,0 +1,18 @@ +use std::path::PathBuf; + +use anyhow::Result; + +use crate::task::{run_task, Project}; + +pub mod handle; +pub use handle::handle; + +impl Project { + pub(super) fn get_neorg_path(&self) -> Result<PathBuf> { + let project_path = run_task(&[ + "_get", + format!("rc.context.{}.rc.neorg_path", self.to_context_display()).as_str(), + ])?; + Ok(PathBuf::from(project_path.as_str())) + } +} |