summaryrefslogtreecommitdiff
path: root/scripts/completions/bash/completion_jv.sh
blob: be5afc6c644788f6f012a38557c89079ff63e7ec (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash
# The JustEnoughVCS CommandLine Completion

_jv_completion() {
    local cur prev words cword
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    words=("${COMP_WORDS[@]}")
    cword=$COMP_CWORD

    # Current
    local cmd="${words[0]}"
    local subcmd="${words[1]}"
    local subsubcmd="${words[2]}"

    # Subcommands
    local base_commands="create init direct unstain account update \
                         sheet status here import export in out \
                         move mv docs exit use sheets accounts \
                         as make drop track hold throw login \
                         jump align"

    # Subcommands - Account
    local account_commands="list as add remove movekey mvkey mvk genpub help"

    # Subcommands - Sheet
    local sheet_commands="list use exit make drop help align"

    # Subcommands - Sheet
    local sheet_commands="list use exit make drop help align"

    # Completion subcommands
    if [[ $cword -eq 1 ]]; then
        COMPREPLY=($(compgen -W "$base_commands" -- "$cur"))
        return 0
    fi

    # Completion account
    if [[ "$subcmd" == "account" || "$subcmd" == "acc" ]]; then
        if [[ $cword -eq 2 ]]; then
            COMPREPLY=($(compgen -W "$account_commands" -- "$cur"))
            return 0
        fi

        case "$subsubcmd" in
            "as"|"remove"|"mvkey"|"mvk"|"movekey"|"genpub")
                if [[ $cword -eq 3 ]]; then
                    # Use jv account list --raw
                    local accounts
                    accounts=$($cmd account list --raw 2>/dev/null)
                    COMPREPLY=($(compgen -W "$accounts" -- "$cur"))
                elif [[ $cword -eq 4 && ("$subsubcmd" == "mvkey" || "$subsubcmd" == "mvk" || "$subsubcmd" == "movekey" || "$subsubcmd" == "genpub") ]]; then
                    COMPREPLY=($(compgen -f -- "$cur"))
                fi
                ;;
            "add")
                if [[ $cword -eq 3 ]]; then
                    # No completion for account name, let user type it
                    COMPREPLY=()
                elif [[ $cword -eq 4 && "$cur" == -* ]]; then
                    # Complete --keygen option
                    COMPREPLY=($(compgen -W "--keygen" -- "$cur"))
                fi
                ;;
            "-"|"rm")
                if [[ $cword -eq 3 ]]; then
                    local accounts
                    accounts=$($cmd account list --raw 2>/dev/null)
                    COMPREPLY=($(compgen -W "$accounts" -- "$cur"))
                fi
                ;;
        esac
        return 0
    fi

    # Completion sheet
    if [[ "$subcmd" == "sheet" || "$subcmd" == "sh" ]]; then
        if [[ $cword -eq 2 ]]; then
            COMPREPLY=($(compgen -W "$sheet_commands" -- "$cur"))
            return 0
        fi

        case "$subsubcmd" in
            # Use jv sheet list --raw --all/--other
            "use"|"drop")
                if [[ $cword -eq 3 ]]; then
                    local sheets
                    sheets=$($cmd sheet list --raw 2>/dev/null)
                    COMPREPLY=($(compgen -W "$sheets" -- "$cur"))
                fi
                ;;
            "make")
                if [[ $cword -eq 3 ]]; then
                    local all_sheets
                    all_sheets=$($cmd sheet list --all --raw 2>/dev/null)
                    COMPREPLY=($(compgen -W "$all_sheets" -- "$cur"))
                fi
                ;;
            "align")
                if [[ $cword -eq 3 ]]; then
                    local align_items="lost moved erased"
                    local unsolved_items
                    unsolved_items=$($cmd sheet align --unsolved --raw 2>/dev/null)
                    COMPREPLY=($(compgen -W "$align_items $unsolved_items" -- "$cur"))
                elif [[ $cword -eq 4 ]]; then
                    local item="${words[3]}"
                    local align_operations=""
                    local created_items
                    created_items=$($cmd sheet align --created --raw 2>/dev/null)

                    if [[ "$item" == "lost" ]]; then
                        align_operations="confirm"
                    elif [[ "$item" == lost:* ]]; then
                        align_operations="confirm $created_items"
                    elif [[ "$item" == "moved" || "$item" == moved:* ]]; then
                        align_operations="local remote"
                    elif [[ "$item" == "erased" || "$item" == erased:* ]]; then
                        align_operations="confirm"
                    else
                        align_operations="local remote confirm $created_items"
                    fi

                    COMPREPLY=($(compgen -W "$align_operations" -- "$cur"))
                fi
                ;;
        esac
        return 0
    fi

    # Completion align
    if [[ "$subcmd" == "align" ]]; then
        if [[ $cword -eq 2 ]]; then
            local align_items="lost moved erased"
            local unsolved_items
            unsolved_items=$($cmd sheet align --unsolved --raw 2>/dev/null)
            COMPREPLY=($(compgen -W "$align_items $unsolved_items" -- "$cur"))
        elif [[ $cword -eq 3 ]]; then
            local item="${words[2]}"
            local align_operations=""
            local created_items
            created_items=$($cmd sheet align --created --raw 2>/dev/null)

            if [[ "$item" == "lost" ]]; then
                align_operations="confirm"
            elif [[ "$item" == lost:* ]]; then
                align_operations="confirm $created_items"
            elif [[ "$item" == "moved" || "$item" == moved:* ]]; then
                align_operations="local remote"
            elif [[ "$item" == "erased" || "$item" == erased:* ]]; then
                align_operations="confirm"
            else
                align_operations="local remote confirm $created_items"
            fi

            COMPREPLY=($(compgen -W "$align_operations" -- "$cur"))
        fi
        return 0
    fi

    # Completion login
    if [[ "$subcmd" == "login" ]]; then
        if [[ $cword -eq 2 ]]; then
            local accounts
            accounts=$($cmd account list --raw 2>/dev/null)
            COMPREPLY=($(compgen -W "$accounts" -- "$cur"))
        elif [[ $cword -eq 3 ]]; then
            local ip_history
            ip_history=$($cmd _ip_history 2>/dev/null)
            COMPREPLY=($(compgen -W "$ip_history" -- "$cur"))
        fi
        return 0
    fi

    # Completion direct
    if [[ "$subcmd" == "direct" ]]; then
        if [[ $cword -eq 2 ]]; then
            local ip_history
            ip_history=$($cmd _ip_history 2>/dev/null)
            COMPREPLY=($(compgen -W "$ip_history" -- "$cur"))
        fi
        return 0
    fi

    # aliases
    case "$subcmd" in
        "as")
            if [[ $cword -eq 2 ]]; then
                local accounts
                accounts=$($cmd account list --raw 2>/dev/null)
                COMPREPLY=($(compgen -W "$accounts" -- "$cur"))
            fi
            ;;
        "use")
            if [[ $cword -eq 2 ]]; then
                local sheets
                sheets=$($cmd sheet list --raw 2>/dev/null)
                COMPREPLY=($(compgen -W "$sheets" -- "$cur"))
            fi
            ;;
        "make")
            if [[ $cword -eq 2 ]]; then
                local all_sheets
                all_sheets=$($cmd sheet list --all --raw 2>/dev/null)
                COMPREPLY=($(compgen -W "$all_sheets" -- "$cur"))
            fi
            ;;
        "drop")
            if [[ $cword -eq 2 ]]; then
                local sheets
                sheets=$($cmd sheet list --raw 2>/dev/null)
                COMPREPLY=($(compgen -W "$sheets" -- "$cur"))
            fi
            ;;
        "docs")
            if [[ $cword -eq 2 ]]; then
                local docs
                docs=$($cmd docs list --raw 2>/dev/null)
                COMPREPLY=($(compgen -W "$docs" -- "$cur"))
            fi
            ;;
        "move"|"mv")
            COMPREPLY=($(compgen -f -- "$cur"))
            ;;
        "import"|"export"|"in"|"out"|"track"|"hold"|"throw")
            COMPREPLY=($(compgen -f -- "$cur"))
            ;;
    esac
}

complete -F _jv_completion jv