diff options
author | Soispha <soispha@vhack.eu> | 2023-12-28 18:34:19 +0100 |
---|---|---|
committer | Soispha <soispha@vhack.eu> | 2023-12-28 19:20:55 +0100 |
commit | 727f17e7e4c59b4342fbca80d5dbdd379c4a3f52 (patch) | |
tree | c2a529cb3689f2317c42fcc78c9b85d9218f6bfa /hm/soispha/pkgs/scripts/wrappers/neorg | |
parent | fix(sys/svcs/xdg/termfilechooser): Remove as river does not support it (diff) | |
download | nixos-config-727f17e7e4c59b4342fbca80d5dbdd379c4a3f52.zip |
fix(hm/pkgs/src/neorg): Move to script as completion generation function
Diffstat (limited to 'hm/soispha/pkgs/scripts/wrappers/neorg')
-rwxr-xr-x | hm/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.c | 78 | ||||
-rwxr-xr-x | hm/soispha/pkgs/scripts/wrappers/neorg/neorg_id_function.sh | 12 |
3 files changed, 91 insertions, 1 deletions
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 |