summaryrefslogtreecommitdiffstats
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.rs66
-rw-r--r--pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs21
2 files changed, 63 insertions, 24 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
index a9a46ee7..12a0180d 100644
--- a/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs
+++ b/pkgs/by-name/ts/tskm/src/interface/neorg/handle.rs
@@ -1,33 +1,59 @@
+// 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, OpenOptions},
+ fs::{self, read_to_string, File, OpenOptions},
io::Write,
process::Command,
};
-use anyhow::{bail, Result};
+use anyhow::{bail, Context, Result};
-use crate::cli::NeorgCommand;
+use crate::{cli::NeorgCommand, state::State};
-pub fn handle(command: NeorgCommand) -> Result<()> {
+pub async fn handle(command: NeorgCommand, state: &mut State) -> Result<()> {
match command {
- NeorgCommand::Task { id } => {
- let project = id.project()?;
- let path = dirs::data_local_dir()
+ NeorgCommand::Task { task } => {
+ let project = task.project(state).await?;
+ let base = dirs::data_local_dir()
.expect("This should exists")
- .join("notes")
- .join(project.get_neorg_path()?);
+ .join("tskm/notes");
+ let path = base.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 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(true);
+ options.append(true).create(false);
let mut file = options.open(&path)?;
- file.write_all(format!("* TITLE (% {})", id.to_uuid()?).as_bytes())?;
+ 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()))?;
}
}
@@ -36,7 +62,7 @@ pub fn handle(command: NeorgCommand) -> Result<()> {
.args([
path.to_str().expect("Should be a utf-8 str"),
"-c",
- format!("/% {}", id.to_uuid()?).as_str(),
+ format!("/% {}", task.uuid()).as_str(),
])
.status()?;
if !status.success() {
@@ -46,7 +72,7 @@ pub fn handle(command: NeorgCommand) -> Result<()> {
{
let status = Command::new("git")
.args(["add", "."])
- .current_dir(&path)
+ .current_dir(path.parent().expect("Will exist"))
.status()?;
if !status.success() {
bail!("Git add . failed!");
@@ -56,14 +82,10 @@ pub fn handle(command: NeorgCommand) -> Result<()> {
.args([
"commit",
"--message",
- format!(
- "chore({}): Update",
- path.parent().expect("Should have a parent").display()
- )
- .as_str(),
+ format!("chore({}): Update", project.get_neorg_path()?.display()).as_str(),
"--no-gpg-sign",
])
- .current_dir(&path)
+ .current_dir(path.parent().expect("Will exist"))
.status()?;
if !status.success() {
bail!("Git commit failed!");
@@ -71,7 +93,7 @@ pub fn handle(command: NeorgCommand) -> Result<()> {
}
{
- id.annotate("[neorg data]")?;
+ task.mark_neorg_data(state).await?;
}
}
}
diff --git a/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs b/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs
index dc5cdf19..6bed1e39 100644
--- a/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs
+++ b/pkgs/by-name/ts/tskm/src/interface/neorg/mod.rs
@@ -1,18 +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::{run_task, Project};
+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(),
])?;
- Ok(PathBuf::from(project_path.as_str()))
+
+ let final_path = project_path
+ .strip_prefix('/')
+ .unwrap_or(project_path.as_str());
+
+ Ok(PathBuf::from(final_path))
}
}