about summary refs log tree commit diff stats
path: root/hm/soispha/pkgs/scripts/wrappers/neorg
blob: c7e08c15663cfbe9e310a132940c5c275109a96a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env dash

# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="1.4.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 [--help|--version] [<neorg workspace>]

Options:
    --help      | -h
                            Display this help and exit.
    --version   | -v
                            Display version and copyright information and exit.
    --task  ID  | -t ID
                            Open the neorg project associated with the current context and
                            the uuid of the task with id ID.
                            If no context is set, drops you to the selection prompt
    <neorg workspace>
                            The neorg workspace to open at startup, an empty value drops
                            you at a prompt to enter the workspace yourself.
                            If unset (and no other options apply), opens the current task
                            context.
EOF
}
get_current_context() {
    current_context="$(task _get rc.context)";
    printf "%s\n" "$current_context";
}
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"
}
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"
}

open_neorg_workspace() {
    nvim -c "NeorgStart" -s "$(ptmp ":Neorg workspace $1\n")"
}
open_neorg_workspace_prompt() {
    nvim -c "NeorgStart" -s "$(ptmp ":Neorg workspace ")"
}
open_current_task_context() {
    current_context="$(get_current_context)"
    if [ "$current_context" ]; then
        context_path="$(get_current_context_path "$current_context")";

        extended_neorg_project_dir="$(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";
        open_neorg_workspace_prompt;
    fi
}
open_current_task_context_at_task_id() {
    task_id="$1";
    current_context="$(get_current_context)"
    if [ "$current_context" ]; then
        context_path="$(get_current_context_path "$current_context")";
        extended_neorg_project_dir="$(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";
        open_neorg_workspace_prompt;
    fi
}

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
        "--task" | "-t")
            shift 1;
            task_id="$1";
            [ "$task_id" ] || die "You must have a task_id set! See --help for more"
            open_current_task_context_at_task_id "$task_id";
            exit 0;
            ;;
        *)
            open_neorg_workspace "$1";
            exit 0;
            ;;
    esac
done

open_current_task_context;

# vim: ft=sh