about summary refs log tree commit diff stats
path: root/hm/soispha/pkgs/scripts/specific/neorg
diff options
context:
space:
mode:
Diffstat (limited to 'hm/soispha/pkgs/scripts/specific/neorg')
-rwxr-xr-xhm/soispha/pkgs/scripts/specific/neorg/neorg323
-rw-r--r--hm/soispha/pkgs/scripts/specific/neorg/neorg_id_function.c78
-rwxr-xr-xhm/soispha/pkgs/scripts/specific/neorg/neorg_id_function.sh12
3 files changed, 413 insertions, 0 deletions
diff --git a/hm/soispha/pkgs/scripts/specific/neorg/neorg b/hm/soispha/pkgs/scripts/specific/neorg/neorg
new file mode 100755
index 00000000..d1766e62
--- /dev/null
+++ b/hm/soispha/pkgs/scripts/specific/neorg/neorg
@@ -0,0 +1,323 @@
+#!/usr/bin/env dash
+
+# shellcheck source=/dev/null
+SHELL_LIBRARY_VERSION="1.10.2" . %SHELL_LIBRARY_PATH
+
+# these are used in version()
+AUTHORS="Soispha"
+YEARS="2023"
+
+NAME="neorg"
+
+help() {
+cat << EOF
+This is a quick wrapper to make starting neorg easier.
+
+USAGE:
+    $NAME [OPTIONS] [COMMAND]
+
+OPTIONS:
+    --help      | -h
+                            Display this help and exit.
+
+    --version   | -v
+                            Display version and copyright information and exit.
+COMMANDS:
+    task [ID]
+                            Open the neorg context associated with the current context and
+                            the uuid of the task with id ID. Without ID, it'll open the
+                            current context's norg file.
+                            If no context is set, drops you to the selection prompt
+
+    dmenu
+                            Select a project in dmenu mode. This will give you all projects
+                            and exectute the selected one as in 'neorg projects <selected>'
+
+    workspace [WS]
+                            The neorg workspace (WS) to open at startup, an empty value drops
+                            you at a prompt to enter the workspace yourself.
+
+    project [P]
+                            Opens the webbrowser with either the context (P) or
+                            the current active context as argument if no context is supplied
+
+    list
+                            Lists all available contexts
+
+    add
+                            Allows you to quickly add projects
+
+    fstart ID
+                            Starts the task (ID) but only after it stooped
+                            the previous active task, if it existed.
+
+    fstop
+                            Stops the current active task
+ARGUMENTS:
+    ID | *([0-9]) := [[%ID_GENERATION_FUNCTION]]
+                            The function displays all possible IDs of the eligable tasks.
+
+    WS := %ALL_WORKSPACES
+                            All possible workspaces
+
+    P := %ALL_PROJECTS_PIPE
+                            The possible project
+
+EOF
+}
+
+
+# Runs it's first argument and then the second, regardless if the first failed or succeed
+chain() {
+    eval "$1"; eval "$2"
+}
+
+# Utils {{{
+UTILS_get_current_context() {
+    current_context="$(task _get rc.context)";
+    printf "%s\n" "$current_context";
+}
+UTILS_get_current_context_path() {
+    current_context="$1";
+    context_path="$(task _get rc.context."$current_context".rc.neorg_path 2>/dev/null)";
+    if ! [ "$context_path" ]; then
+        context_path="$(grep "context.$current_context.rc.neorg_path" "%HOME_TASKRC" | awk 'BEGIN {FS="="} {print $2}')";
+        [ "$context_path" ] || die "All contexts should have a 'neorg_path' set!"
+    fi
+    printf "%s\n" "$context_path"
+}
+UTILS_get_neorg_project_dir() {
+    # Perform shell expansion of Tilde
+    neorg_project_dir="$(sed "s|~|$HOME|" "$(ptmp "%DEFAULT_NEORG_PROJECT_DIR")")";
+    printf "%s\n" "$neorg_project_dir"
+}
+UTILS_project_to_context_name() {
+    project="$1";
+    context="$(sed 's|\.|_|g' "$(ptmp "$project")")";
+    printf "%s\n" "$context";
+}
+UTILS_context_to_project_name() {
+    context="$1";
+    project="$(sed 's|_|\.|g' "$(ptmp "$context")")";
+    printf "%s\n" "$project";
+}
+# }}}
+
+# Workspace {{{
+WORKSPACE_open_neorg_workspace() {
+    nvim -c "NeorgStart" -s "$(ptmp ":Neorg workspace $1\n")"
+}
+WORKSPACE_open_neorg_workspace_prompt() {
+    nvim -c "NeorgStart" -s "$(ptmp ":Neorg workspace ")"
+}
+# }}}
+# Context {{{
+CONTEXT_open_current_task_context() {
+    current_context="$(UTILS_get_current_context)"
+    if [ "$current_context" ]; then
+        context_path="$(UTILS_get_current_context_path "$current_context")";
+
+        extended_neorg_project_dir="$(UTILS_get_neorg_project_dir)";
+        cd "$extended_neorg_project_dir" || die "(BUG?): Can not access the project dir: $extended_neorg_project_dir";
+
+        nvim "$extended_neorg_project_dir/$context_path";
+
+        git add .;
+        git commit --message="chore($(dirname "$context_path")): Update" --no-gpg-sign
+    else
+        warn "No context active";
+        WORKSPACE_open_neorg_workspace_prompt;
+    fi
+}
+CONTEXT_open_current_task_context_at_task_id() {
+    task_id="$1";
+    current_context="$(UTILS_get_current_context)"
+    if [ "$current_context" ]; then
+        context_path="$(UTILS_get_current_context_path "$current_context")";
+        extended_neorg_project_dir="$(UTILS_get_neorg_project_dir)";
+        task_uuid="$(task "$task_id" uuids)"
+
+        cd "$extended_neorg_project_dir" || die "(BUG?): Can not access the project dir: $extended_neorg_project_dir";
+
+        if ! grep -q "% $task_uuid" "$extended_neorg_project_dir/$context_path"; then
+            echo "* TITLE (% $task_uuid)" >> "$extended_neorg_project_dir/$context_path"
+        fi
+
+        nvim "$extended_neorg_project_dir/$context_path" -c "/% $task_uuid";
+
+        git add .;
+        git commit --message="chore($(dirname "$context_path")): Update" --no-gpg-sign
+    else
+        warn "No context active";
+        WORKSPACE_open_neorg_workspace_prompt;
+    fi
+}
+# }}}
+# Project {{{
+PROJECT_open_current_context_in_browser() {
+    current_context="$(UTILS_get_current_context)";
+    [ "$current_context" ] || die "No current context to use";
+    PROJECT_open_context_in_browser "$(UTILS_context_to_project_name "$current_context")";
+}
+PROJECT_open_context_in_browser() {
+    context="$1";
+    [ "$context" ] || die "BUG: No context supplied to PROJECT_open_context_in_browser"
+
+    old_context="$(UTILS_get_current_context)";
+    # We have ensured that only one task may be active
+    old_started_task="$(task +ACTIVE _ids)";
+
+    tracking="$(mktmp)";
+    task "project:$context" _ids | xargs task _zshids > "$tracking";
+    task context "$(UTILS_project_to_context_name "$context")"
+
+    while read -r description; do
+        desc="$(echo "$description" | awk -F: '{print $2}' )";
+        if [ "$desc" = "tracking" ]; then
+            task_id="$(echo "$description" | awk -F: '{print $1}' )";
+            notify-send "(Neorg)"  "Starting task $context -> $desc";
+            task start "$task_id"
+            break
+        fi
+    done < "$tracking"
+
+
+    firefox -P "$context"
+
+    task stop "$task_id"
+    [ "$old_started_task" ] && task start "$old_started_task"
+
+    if [ "$old_context" ]; then
+        task context "$old_context"
+    else
+        task context none
+    fi
+}
+# }}}
+# Dmenu {{{
+DMENU_open_context_in_browser() {
+    project="$(echo "%ALL_PROJECTS_PIPE" | rofi -sep "|" -dmenu)";
+    if [ "$project" ]; then
+        PROJECT_open_context_in_browser "$project";
+    else
+        current_context="$(UTILS_get_current_context)";
+        [ "$current_context" ] || chain 'notify-send "(Neorg) No current context"' "exit 1"
+        PROJECT_open_context_in_browser "$current_context";
+    fi
+}
+# }}}
+# List {{{
+LIST_list_all_contexts_newline() {
+    print "%ALL_PROJECTS_NEWLINE"
+}
+LIST_list_all_contexts_comma() {
+    print "%ALL_PROJECTS_COMMA"
+}
+# }}}
+# Add {{{
+ADD_open_taskwarrior_project_file() {
+    task_project_file="%TASK_PROJECT_FILE";
+    cd "$(dirname $task_project_file)" || die "BUG: task_project_file ('$task_project_file') can't be accessed"
+    git_dir="$(search_flake_base_dir)";
+    [ "$git_dir" ] || die "(BUG): No git directory?"
+    cd "$git_dir" || die "Unreachable, this MUST exists"
+    nvim "$task_project_file";
+    git add "$task_project_file";
+
+    base_task_project_file_path="$(awk "{ gsub(\"$git_dir/\", \"\", \$0); print }" "$(ptmp "$task_project_file")")"
+    git add $task_project_file;
+    if ! [ "$(git status --porcelain=v2 | awk '{print $2}' | head -c 1)" = "." ]; then
+        # TODO: Also check that only our file is staged before committing <2023-10-20>
+        git commit --verbose --message="chore($(dirname "$base_task_project_file_path")): Update"
+    fi
+}
+# }}}
+# fStart {{{
+FSTART_start_new_task() {
+    task_id="$1";
+    FSTOP_stop_current_task;
+    task start "$task_id"
+}
+# }}}
+# fStop {{{
+FSTOP_stop_current_task() {
+    # we ensured that only one task may be active
+    active="$(task +ACTIVE _ids)";
+    [ "$active" ] && task stop "$active";
+}
+# }}}
+
+for arg in "$@"; do
+    case "$arg" in
+        "--help" | "-h")
+            help;
+            exit 0;
+            ;;
+        "--version" | "-v")
+            version;
+            exit 0;
+            ;;
+    esac
+done
+
+while [ "$#" -ne 0 ]; do
+    case "$1" in
+        "t"*) # task
+            shift 1;
+            task_id="$1";
+            [ "$task_id" ] || chain CONTEXT_open_current_task_context "exit 0"
+            CONTEXT_open_current_task_context_at_task_id "$task_id";
+            exit 0;
+            ;;
+        "w"*) # workspace
+            shift 1;
+            workspace_to_open="$1";
+            # TODO: Exit with 1 on error, instead of the 0 <2023-10-20>
+            [ "$workspace_to_open" ] || chain WORKSPACE_open_neorg_workspace_prompt "exit 0";
+            WORKSPACE_open_neorg_workspace "$workspace_to_open";
+            exit 0;
+            ;;
+        "p"*) # project
+            shift 1;
+            context_to_open="$1";
+            # TODO: Exit with 1 on error, instead of the 0 <2023-10-20>
+            [ "$context_to_open" ] || chain PROJECT_open_current_context_in_browser "exit 0";
+            if ! grep -q "$context_to_open" "$(ptmp "%ALL_PROJECTS_NEWLINE")"; then
+                die "Your context ('$context_to_open') is not in the list of available contexts:
+%ALL_PROJECTS_COMMA";
+            fi
+            PROJECT_open_context_in_browser "$context_to_open";
+            exit 0;
+            ;;
+        "l"*) # list
+            LIST_list_all_contexts_newline;
+            exit 0
+            ;;
+        "a"*) # add-project
+            ADD_open_taskwarrior_project_file;
+            exit 0
+            ;;
+        "d"*) # dmenu
+            DMENU_open_context_in_browser;
+            exit 0
+            ;;
+        "fsta"*) # fstart
+            shift 1;
+            task_id="$1";
+            [ "$task_id" ] || die "No task id provided to fstart";
+            FSTART_start_new_task "$task_id";
+            exit 0
+            ;;
+        "fsto"*) # fstop
+            FSTOP_stop_current_task;
+            exit 0
+            ;;
+        *)
+            die "Command '$1' does not exist! Please look at:\n $NAME --help";
+            exit 0;
+            ;;
+    esac
+done
+
+CONTEXT_open_current_task_context;
+# vim: ft=sh
diff --git a/hm/soispha/pkgs/scripts/specific/neorg/neorg_id_function.c b/hm/soispha/pkgs/scripts/specific/neorg/neorg_id_function.c
new file mode 100644
index 00000000..a635b4b0
--- /dev/null
+++ b/hm/soispha/pkgs/scripts/specific/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/specific/neorg/neorg_id_function.sh b/hm/soispha/pkgs/scripts/specific/neorg/neorg_id_function.sh
new file mode 100755
index 00000000..413a18de
--- /dev/null
+++ b/hm/soispha/pkgs/scripts/specific/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