about summary refs log tree commit diff stats
path: root/hm/soispha/pkgs/scripts/wrappers/neorg
blob: 8f707d27ee81feedce250aaaede049a63ff5edec (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env dash

# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="1.7.0" . %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
ARGUMENTS:
    ID | *([0-9]) := [[$(cat %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
}
# 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"
}
# }}}

# 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" ] || WORKSPACE_open_neorg_workspace_prompt;
    PROJECT_open_context_in_browser "$current_context";
}
PROJECT_open_context_in_browser() {
    context="$1";
    [ "$context" ] || die "BUG: No context supplied to PROJECT_open_context_in_browser"

    firefox -P "$context"
}
# }}}
# 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" ] || (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
}
# }}}

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" ] || (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" ] || (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" ] || (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
            ;;
        *)
            die "Command '$1' does not exist! Please look at:\n $NAME --help";
            exit 0;
            ;;
    esac
done

CONTEXT_open_current_task_context;
# vim: ft=sh