about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSoispha <soispha@vhack.eu>2023-12-28 18:34:19 +0100
committerSoispha <soispha@vhack.eu>2023-12-28 19:20:55 +0100
commit727f17e7e4c59b4342fbca80d5dbdd379c4a3f52 (patch)
treec2a529cb3689f2317c42fcc78c9b85d9218f6bfa
parentfix(sys/svcs/xdg/termfilechooser): Remove as river does not support it (diff)
downloadnixos-config-727f17e7e4c59b4342fbca80d5dbdd379c4a3f52.tar.gz
nixos-config-727f17e7e4c59b4342fbca80d5dbdd379c4a3f52.zip
fix(hm/pkgs/src/neorg): Move to script as completion generation function
-rw-r--r--.gitignore1
-rw-r--r--hm/soispha/pkgs/scripts.nix15
-rwxr-xr-xhm/soispha/pkgs/scripts/wrappers/neorg/neorg (renamed from hm/soispha/pkgs/scripts/wrappers/neorg)2
-rw-r--r--hm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.c78
-rwxr-xr-xhm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.sh12
-rw-r--r--hm/soispha/pkgs/scripts/wrappers/neorg_id_completion_function1
6 files changed, 102 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore
index 9a899cc6..28ba539c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 .direnv
+.ccls-cache
 /result
diff --git a/hm/soispha/pkgs/scripts.nix b/hm/soispha/pkgs/scripts.nix
index 0c58d83a..8e98bccb 100644
--- a/hm/soispha/pkgs/scripts.nix
+++ b/hm/soispha/pkgs/scripts.nix
@@ -71,7 +71,7 @@
   };
   neorg-scr = sysLib.writeShellScriptWithLibraryAndKeepPath {
     name = "neorg";
-    src = ./scripts/wrappers/neorg;
+    src = ./scripts/wrappers/neorg/neorg;
     dependencies = with pkgs; [
       cocogitto
       git-crypt
@@ -87,10 +87,15 @@
       ALL_PROJECTS_COMMA = "${config.soispha.taskwarrior.projects.projects_comma}";
       ALL_PROJECTS_PIPE = "${config.soispha.taskwarrior.projects.projects_pipe}";
       ALL_WORKSPACES = "${lib.strings.concatStringsSep "|" (builtins.attrNames config.programs.nixvim.plugins.neorg.modules."core.dirman".config.workspaces)}";
-      ID_GENERATION_FUNCTION =
-        # It's just impossible to correctly quote this function when it's in any language that
-        # has special treatment for backslashes
-        ./scripts/wrappers/neorg_id_completion_function;
+      ID_GENERATION_FUNCTION = "${sysLib.writeShellScriptWithLibrary {
+        name = "neorg_id_function";
+        src = ./scripts/wrappers/neorg/neorg_id_function.sh;
+        dependencies = with pkgs; [
+          taskwarrior
+          gawk
+          findutils # xargs
+        ];
+      }}/bin/neorg_id_function";
 
       # TODO: Replace the hard-coded path here with some reference <2023-10-20>
       TASK_PROJECT_FILE = "/home/soispha/repos/nix/nixos-config/hm/soispha/conf/taskwarrior/projects/default.nix";
diff --git a/hm/soispha/pkgs/scripts/wrappers/neorg b/hm/soispha/pkgs/scripts/wrappers/neorg/neorg
index 50e888d3..fdbb2ec6 100755
--- a/hm/soispha/pkgs/scripts/wrappers/neorg
+++ b/hm/soispha/pkgs/scripts/wrappers/neorg/neorg
@@ -51,7 +51,7 @@ COMMANDS:
                             Starts the task (ID) but only after it stooped
                             the previous active task, if it existed.
 ARGUMENTS:
-    ID | *([0-9]) := [[$(cat %ID_GENERATION_FUNCTION)]]
+    ID | *([0-9]) := [[%ID_GENERATION_FUNCTION]]
                             The function displays all possible IDs of the eligable tasks.
 
     WS := %ALL_WORKSPACES
diff --git a/hm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.c b/hm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.c
new file mode 100644
index 00000000..a635b4b0
--- /dev/null
+++ b/hm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.c
@@ -0,0 +1,78 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+// It's just impossible to correctly quote this function when it's in any
+// language that has special treatment for backslashes
+
+char *exec_command(const char *command) {
+  FILE *fp;
+  char *buffer = 0;
+  long length;
+  char path[1035];
+
+  /* Open the command for reading. */
+  fp = popen(command, "r");
+  if (fp == NULL) {
+    fprintf(stderr, "Failed to execute '%s' because of: %s\n", command,
+            strerror(errno));
+    exit(1);
+  }
+
+  fseek(fp, 0, SEEK_END);
+  length = ftell(fp);
+  fseek(fp, 0, SEEK_SET);
+  buffer = malloc(length + 1);
+  if (buffer) {
+    if (!fread(buffer, 1, length, fp)) {
+      fprintf(stderr, "Failed to read output of command '%s' because of: %s\n",
+              command, strerror(errno));
+      exit(1);
+    }
+  }
+
+  pclose(fp);
+  buffer[length] = '\0';
+  return buffer;
+}
+
+int main() {
+  char *context = exec_command("task _get rc.context");
+  printf("%s\n", context);
+
+  char *filter = malloc(100); // should never be bigger than this number
+                              //
+  // Check if the context is not empty
+  if (strlen(context) > 1) {
+    sprintf(filter, "project:%s", context);
+  } else {
+    filter = "0-10000";
+  }
+  char *task_ids = malloc(1000);
+  sprintf(task_ids, "task %s _ids", filter);
+  free(filter);
+  char *ids = exec_command(task_ids);
+  free(task_ids);
+
+  if (strlen(ids) > 1) {
+    char *task_zshids = malloc(1000);
+    sprintf(task_zshids, "task _zshids %s", ids);
+    char *zshids = exec_command(task_zshids);
+    free(task_zshids);
+
+    char *awk = malloc(1000);
+    sprintf(awk,
+            "awk -F: -v q=\"'\" '{gsub(/'\''/, q \"\\\" q q ); print $1 \": \" "
+            "q $2 q}' %s",
+            zshids);
+    char *output = exec_command(awk);
+    free(awk);
+    printf("%s\n", output);
+    free(output);
+  } else {
+    // No task match the filter
+    return 0;
+  }
+  return 0;
+}
diff --git a/hm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.sh b/hm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.sh
new file mode 100755
index 00000000..413a18de
--- /dev/null
+++ b/hm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.sh
@@ -0,0 +1,12 @@
+#! /bin/sh
+context="$(task _get rc.context)";
+if [ "$context" ]; then
+    filter="project:$context";
+else
+    filter="0-10000";
+fi
+tasks="$(task "$filter" _ids)";
+
+if [ "$tasks" ]; then
+    echo "$tasks" | xargs task _zshids | awk -F: -v q="'" '{gsub(/'\''/, q "\\" q q ); print $1 ":" q $2 q}';
+fi
diff --git a/hm/soispha/pkgs/scripts/wrappers/neorg_id_completion_function b/hm/soispha/pkgs/scripts/wrappers/neorg_id_completion_function
deleted file mode 100644
index e9163d2e..00000000
--- a/hm/soispha/pkgs/scripts/wrappers/neorg_id_completion_function
+++ /dev/null
@@ -1 +0,0 @@
-(wc="$(task "$(con="$(task _get rc.context)"; if [ "$con" ]; then echo "project:$con"; else echo "0-10000"; fi)" _ids)"; if [ "$wc" ]; then echo "$wc"; else echo "0"; fi ) | xargs task _zshids | awk -F: -v q="'" '{gsub(/'\''/, q "\" q q ); print $1 ":" q $2 q}'