aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/ts/tskm/src/interface/neorg
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/ts/tskm/src/interface/neorg')
-rw-r--r--pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs101
-rw-r--r--pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs35
2 files changed, 136 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..12a0180d
--- /dev/null
+++ b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs
@@ -0,0 +1,101 @@
+// nixos-config - My current NixOS configuration
+//
+// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// This file is part of my nixos-config.
+//
+// You should have received a copy of the License along with this program.
+// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+use std::{
+ env,
+ fs::{self, read_to_string, File, OpenOptions},
+ io::Write,
+ process::Command,
+};
+
+use anyhow::{bail, Context, Result};
+
+use crate::{cli::NeorgCommand, state::State};
+
+pub async fn handle(command: NeorgCommand, state: &mut State) -> Result<()> {
+ match command {
+ NeorgCommand::Task { task } => {
+ let project = task.project(state).await?;
+ let base = dirs::data_local_dir()
+ .expect("This should exists")
+ .join("tskm/notes");
+ let path = base.join(project.get_neorg_path()?);
+
+ fs::create_dir_all(path.parent().expect("This should exist"))?;
+
+ {
+ let contents = if path.exists() {
+ read_to_string(&path)
+ .with_context(|| format!("Failed to read file: '{}'", path.display()))?
+ } else {
+ File::create(&path)
+ .with_context(|| format!("Failed to create file: '{}'", path.display()))?;
+ String::new()
+ };
+
+ if !contents.contains(format!("% {}", task.uuid()).as_str()) {
+ let mut options = OpenOptions::new();
+ options.append(true).create(false);
+
+ let mut file = options.open(&path)?;
+ file.write_all(
+ format!("* {} (% {})", task.description(state).await?, task.uuid())
+ .as_bytes(),
+ )
+ .with_context(|| {
+ format!("Failed to write task uuid to file: '{}'", path.display())
+ })?;
+ file.flush()
+ .with_context(|| format!("Failed to flush file: '{}'", path.display()))?;
+ }
+ }
+
+ 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!("/% {}", task.uuid()).as_str(),
+ ])
+ .status()?;
+ if !status.success() {
+ bail!("$EDITOR fail with error code: {status}");
+ }
+
+ {
+ let status = Command::new("git")
+ .args(["add", "."])
+ .current_dir(path.parent().expect("Will exist"))
+ .status()?;
+ if !status.success() {
+ bail!("Git add . failed!");
+ }
+
+ let status = Command::new("git")
+ .args([
+ "commit",
+ "--message",
+ format!("chore({}): Update", project.get_neorg_path()?.display()).as_str(),
+ "--no-gpg-sign",
+ ])
+ .current_dir(path.parent().expect("Will exist"))
+ .status()?;
+ if !status.success() {
+ bail!("Git commit failed!");
+ }
+ }
+
+ {
+ task.mark_neorg_data(state).await?;
+ }
+ }
+ }
+ 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..6bed1e39
--- /dev/null
+++ b/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs
@@ -0,0 +1,35 @@
+// nixos-config - My current NixOS configuration
+//
+// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// This file is part of my nixos-config.
+//
+// You should have received a copy of the License along with this program.
+// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+
+use std::path::PathBuf;
+
+use anyhow::Result;
+
+use crate::task::{Project, run_task};
+
+pub mod handle;
+pub use handle::handle;
+
+impl Project {
+ /// Return the stored neorg path of this project.
+ /// The returned path will never start with a slash (/).
+ 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(),
+ ])?;
+
+ let final_path = project_path
+ .strip_prefix('/')
+ .unwrap_or(project_path.as_str());
+
+ Ok(PathBuf::from(final_path))
+ }
+}