summaryrefslogtreecommitdiff
path: root/scripts/completions/powershell/completion_jv.ps1
blob: 0c854e3714240e28eda6c94b81d740713f438cfe (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
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
# The JustEnoughVCS CommandLine Completion

Register-ArgumentCompleter -Native -CommandName jv -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)

    $words = $commandAst.CommandElements | ForEach-Object { $_.ToString() }
    $currentIndex = $words.IndexOf($wordToComplete)
    if ($currentIndex -lt 0) { $currentIndex = $words.Count }

    $cmd = "jv"
    $subcmd = if ($words.Count -gt 1) { $words[1] } else { $null }
    $subsubcmd = if ($words.Count -gt 2) { $words[2] } else { $null }

    # Base commands
    $baseCommands = @(
        "create", "init", "direct", "unstain", "account", "update",
        "sheet", "status", "here", "move", "mv", "docs", "exit", "use", "sheets", "accounts",
        "as", "make", "drop", "track", "hold", "throw", "login",
        "jump", "align", "info"
    )

    # Account subcommands
    $accountCommands = @("list", "as", "add", "remove", "movekey", "mvkey", "mvk", "genpub", "help")

    # Sheet subcommands
    $sheetCommands = @("list", "use", "exit", "make", "drop", "help", "align")

    # Completion for main command
    if ($currentIndex -eq 1) {
        return $baseCommands | Where-Object { $_ -like "$wordToComplete*" }
    }

    # Completion for account command
    if ($subcmd -eq "account" -or $subcmd -eq "acc") {
        if ($currentIndex -eq 2) {
            return $accountCommands | Where-Object { $_ -like "$wordToComplete*" }
        }

        switch ($subsubcmd) {
            { @("as", "remove", "mvkey", "mvk", "movekey", "genpub") -contains $_ } {
                if ($currentIndex -eq 3) {
                    $accounts = & $cmd account list --raw 2>$null
                    return $accounts | Where-Object { $_ -like "$wordToComplete*" }
                } elseif ($currentIndex -eq 4 -and (@("mvkey", "mvk", "movekey", "genpub") -contains $subsubcmd)) {
                    # File completion for key files
                    return Get-ChildItem -Name -File -Path "." | Where-Object { $_ -like "$wordToComplete*" }
                }
            }
            "add" {
                if ($currentIndex -eq 3) {
                    # No completion for account name
                    return @()
                } elseif ($currentIndex -eq 4 -and $wordToComplete.StartsWith("-")) {
                    # Complete --keygen option
                    if ("--keygen" -like "$wordToComplete*") {
                        return "--keygen"
                    }
                }
            }
            { @("-", "rm") -contains $_ } {
                if ($currentIndex -eq 3) {
                    $accounts = & $cmd account list --raw 2>$null
                    return $accounts | Where-Object { $_ -like "$wordToComplete*" }
                }
            }
        }
        return @()
    }

    # Completion for sheet command
    if ($subcmd -eq "sheet" -or $subcmd -eq "sh") {
        if ($currentIndex -eq 2) {
            return $sheetCommands | Where-Object { $_ -like "$wordToComplete*" }
        }

        switch ($subsubcmd) {
            { @("use", "drop") -contains $_ } {
                if ($currentIndex -eq 3) {
                    $sheets = & $cmd sheet list --raw 2>$null
                    return $sheets | Where-Object { $_ -like "$wordToComplete*" }
                }
            }
            "make" {
                if ($currentIndex -eq 3) {
                    $allSheets = & $cmd sheet list --all --raw 2>$null
                    return $allSheets | Where-Object { $_ -like "$wordToComplete*" }
                }
            }
            "align" {
                if ($currentIndex -eq 3) {
                    $alignItems = @("lost", "moved", "erased")
                    $unsolvedItems = & $cmd sheet align --unsolved --raw 2>$null
                    $completions = $alignItems + $unsolvedItems
                    return $completions | Where-Object { $_ -like "$wordToComplete*" }
                } elseif ($currentIndex -eq 4) {
                    $item = $words[3]
                    $alignOperations = @()
                    $createdItems = & $cmd sheet align --created --raw 2>$null

                    if ($item -eq "lost") {
                        $alignOperations = @("confirm")
                    } elseif ($item -like "lost:*") {
                        $alignOperations = @("confirm") + $createdItems
                    } elseif ($item -eq "moved" -or $item -like "moved:*") {
                        $alignOperations = @("local", "remote", "break")
                    } elseif ($item -eq "erased" -or $item -like "erased:*") {
                        $alignOperations = @("confirm")
                    } else {
                        $alignOperations = @("local", "remote", "confirm", "break") + $createdItems
                    }

                    return $alignOperations | Where-Object { $_ -like "$wordToComplete*" }
                }
            }
        }
        return @()
    }

    # Completion for align command
    if ($subcmd -eq "align") {
        if ($currentIndex -eq 2) {
            $alignItems = @("lost", "moved", "erased")
            $unsolvedItems = & $cmd sheet align --unsolved --raw 2>$null
            $completions = $alignItems + $unsolvedItems
            return $completions | Where-Object { $_ -like "$wordToComplete*" }
        } elseif ($currentIndex -eq 3) {
            $item = $words[2]
            $alignOperations = @()
            $createdItems = & $cmd sheet align --created --raw 2>$null

            if ($item -eq "lost") {
                $alignOperations = @("confirm")
            } elseif ($item -like "lost:*") {
                $alignOperations = @("confirm") + $createdItems
            } elseif ($item -eq "moved" -or $item -like "moved:*") {
                $alignOperations = @("local", "remote", "break")
            } elseif ($item -eq "erased" -or $item -like "erased:*") {
                $alignOperations = @("confirm")
            } else {
                $alignOperations = @("local", "remote", "confirm", "break") + $createdItems
            }

            return $alignOperations | Where-Object { $_ -like "$wordToComplete*" }
        }
        return @()
    }

    # Completion for login command
    if ($subcmd -eq "login") {
        if ($currentIndex -eq 2) {
            $accounts = & $cmd account list --raw 2>$null
            return $accounts | Where-Object { $_ -like "$wordToComplete*" }
        } elseif ($currentIndex -eq 3) {
            $ipHistory = & $cmd _ip_history 2>$null
            return $ipHistory | Where-Object { $_ -like "$wordToComplete*" }
        }
        return @()
    }

    # Completion for direct command
    if ($subcmd -eq "direct") {
        if ($currentIndex -eq 2) {
            $ipHistory = & $cmd _ip_history 2>$null
            return $ipHistory | Where-Object { $_ -like "$wordToComplete*" }
        }
        return @()
    }

    # Completion for info command
    if ($subcmd -eq "info") {
        if ($currentIndex -eq 2) {
            # File completion for the file argument
            return Get-ChildItem -Name -File -Path "." | Where-Object { $_ -like "$wordToComplete*" }
        }
        return @()
    }

    # Aliases completion
    switch ($subcmd) {
        "as" {
            if ($currentIndex -eq 2) {
                $accounts = & $cmd account list --raw 2>$null
                return $accounts | Where-Object { $_ -like "$wordToComplete*" }
            }
        }
        "use" {
            if ($currentIndex -eq 2) {
                $sheets = & $cmd sheet list --raw 2>$null
                return $sheets | Where-Object { $_ -like "$wordToComplete*" }
            }
        }
        "make" {
            if ($currentIndex -eq 2) {
                $allSheets = & $cmd sheet list --all --raw 2>$null
                return $allSheets | Where-Object { $_ -like "$wordToComplete*" }
            }
        }
        "drop" {
            if ($currentIndex -eq 2) {
                $sheets = & $cmd sheet list --raw 2>$null
                return $sheets | Where-Object { $_ -like "$wordToComplete*" }
            }
        }
        "docs" {
            if ($currentIndex -eq 2) {
                $docs = & $cmd docs list --raw 2>$null
                return $docs | Where-Object { $_ -like "$wordToComplete*" }
            }
        }
        { @("move", "mv", "track", "hold", "throw") -contains $_ } {
            # File completion for file operations
            return Get-ChildItem -Name -File -Path "." | Where-Object { $_ -like "$wordToComplete*" }
        }
    }

    return @()
}