summaryrefslogtreecommitdiff
path: root/internal/askcli/completion.go
blob: f787849e3713c8efd2fb1f4b5d5c026cdd804fb5 (plain)
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
package askcli

import (
	"strings"
)

type fishCompletionItem struct {
	name        string
	description string
}

var askRootCompletionItems = []fishCompletionItem{
	{name: "add", description: "Create a new task"},
	{name: "list", description: "List active tasks"},
	{name: "all", description: "List all tasks"},
	{name: "ready", description: "List READY tasks"},
	{name: "info", description: "Show task details"},
	{name: "annotate", description: "Add an annotation"},
	{name: "start", description: "Start a task"},
	{name: "stop", description: "Stop a task"},
	{name: "done", description: "Mark a task complete"},
	{name: "priority", description: "Set priority"},
	{name: "tag", description: "Add or remove a tag"},
	{name: "dep", description: "Manage dependencies"},
	{name: "urgency", description: "List tasks sorted by urgency"},
	{name: "modify", description: "Modify task fields"},
	{name: "denotate", description: "Remove an annotation"},
	{name: "delete", description: "Delete a task"},
	{name: "help", description: "Show help"},
}

var askDepCompletionItems = []fishCompletionItem{
	{name: "add", description: "Add a dependency"},
	{name: "rm", description: "Remove a dependency"},
	{name: "list", description: "List dependencies"},
}

func FishCompletion() string {
	var b strings.Builder
	b.WriteString("# Fish completion for ask.\n")
	b.WriteString("# Install as ~/.config/fish/completions/ask.fish or")
	b.WriteString(" $XDG_CONFIG_HOME/fish/completions/ask.fish.\n\n")
	b.WriteString("function __ask_needs_root_completion\n")
	b.WriteString("    set -l tokens (commandline -opc)\n")
	b.WriteString("    if test (count $tokens) -le 1\n")
	b.WriteString("        return 0\n")
	b.WriteString("    end\n")
	b.WriteString("    for token in $tokens[2..-1]\n")
	b.WriteString("        if not string match -qr '^-' -- $token\n")
	b.WriteString("            return 1\n")
	b.WriteString("        end\n")
	b.WriteString("    end\n")
	b.WriteString("    return 0\n")
	b.WriteString("end\n\n")
	b.WriteString("function __ask_in_dep_context\n")
	b.WriteString("    set -l tokens (commandline -opc)\n")
	b.WriteString("    if test (count $tokens) -lt 2\n")
	b.WriteString("        return 1\n")
	b.WriteString("    end\n")
	b.WriteString("    set -l seen_dep 0\n")
	b.WriteString("    for token in $tokens[2..-1]\n")
	b.WriteString("        if string match -qr '^-' -- $token\n")
	b.WriteString("            continue\n")
	b.WriteString("        end\n")
	b.WriteString("        if test $seen_dep -eq 0\n")
	b.WriteString("            if test $token = dep\n")
	b.WriteString("                set seen_dep 1\n")
	b.WriteString("            else\n")
	b.WriteString("                return 1\n")
	b.WriteString("            end\n")
	b.WriteString("        else\n")
	b.WriteString("            return 1\n")
	b.WriteString("        end\n")
	b.WriteString("    end\n")
	b.WriteString("    test $seen_dep -eq 1\n")
	b.WriteString("end\n\n")
	b.WriteString("complete -c ask -f\n")
	b.WriteString("complete -c ask -s j -l json -d 'Emit JSON output'\n")
	for _, item := range askRootCompletionItems {
		writeFishCompletionLine(&b, "__ask_needs_root_completion", item)
	}
	for _, item := range askDepCompletionItems {
		writeFishCompletionLine(&b, "__ask_in_dep_context", item)
	}
	return b.String()
}

func writeFishCompletionLine(b *strings.Builder, condition string, item fishCompletionItem) {
	b.WriteString("complete -c ask -n '")
	b.WriteString(condition)
	b.WriteString("' -a '")
	b.WriteString(item.name)
	b.WriteString("' -d '")
	b.WriteString(strings.ReplaceAll(item.description, "'", "\\'"))
	b.WriteString("'\n")
}