2013-05-20 5 views
8

Ich versuche, ein Autocomplete-Skript für die Verwendung mit Fisch zu erstellen; Ich portiere über ein Bash-Vervollständigungsscript für das gleiche Programm.Erstellen von Autocomplete-Skript mit Unterbefehlen

Das Programm hat drei Top-Level-Befehle, sagen foo, bar und baz und jeder hat einige Unterbefehle, sagen Sie einfach ab und c für jeden.

Was ich sehe ist, dass die Top-Level-Auto-Vervollständigen-ok-Befehle, also wenn ich f Typen ich bin foo immer automatisch zu vervollständigen, aber dann, wenn ich Registerkarte wieder getroffen, um zu sehen, was es ist sub-Befehle sind, ich sehe foo , bar, baz, a, b, c und es sollte nur a, b, c

ich als Referenz benutzen sein die git completion script, da es scheint richtig zu arbeiten. Ich verwende auch die git flow script als eine Referenz.

ich denke, das ist im git Abschluss Skript bearbeitet durch:

function __fish_git_needs_command 
    set cmd (commandline -opc) 
    if [ (count $cmd) -eq 1 -a $cmd[1] = 'git' ] 
    return 0 
    end 
    return 1 
end 

was Sinn macht, können Sie nur den Abschluss verwenden, wenn es einen einzigen arg an den Befehl, das Skript selbst; Wenn Sie dies als Bedingung (-n) für den Aufruf auf der obersten Ebene Befehle verwenden, denke ich, dass das Richtige passieren würde.

Allerdings ist das, was ich sehe, nicht der Fall. Ich kopierte diese Funktion in mein Skript, änderte "git" entsprechend und hatte kein Glück.

Die abgespeckte Skript ist wie folgt:

function __fish_prog_using_command 
    set cmd (commandline -opc) 
    set subcommands $argv 
    if [ (count $cmd) = (math (count $subcommands) + 1) ] 
    for i in (seq (count $subcommands)) 
     if not test $subcommands[$i] = $cmd[(math $i + 1)] 
     return 1 
     end 
    end 
    return 0 
    end 
    return 1 
end 

function __fish_git_needs_command 
    set cmd (commandline -opc) 
    set startsWith (echo "$cmd[1]" | grep -E 'prog$') 
    # there's got to be a better way to do this regex, fish newb alert 
    if [ (count $cmd) = 1 ] 
    # wtf, this is always false? it this the problem? 
    if [ $cmd[1] -eq $cmd[1] ] 
     return 1 
    end 
    end 

    return 0 
end 

complete --no-files -c prog -a bar -n "__fish_git_needs_command" 

complete --no-files -c prog -a foo -n "__fish_git_needs_command" 

complete --no-files -c prog -a a -n "__fish_prog_using_command foo" 
complete --no-files -c prog -a b -n "__fish_prog_using_command foo" 
complete --no-files -c prog -a c -n "__fish_prog_using_command foo" 

complete --no-files -c prog -a baz -n "__fish_git_needs_command" 

Vorschläge, wie diese Arbeit zu machen ist sehr geschätzt.

Antwort

7

Ich denke, Sie wissen, dass return 0 bedeutet, wahr und dass return 1 bedeutet falsch?

Von Ihrer Ausgabe sieht es so aus, als ob Ihre needs_command-Funktion nicht richtig funktioniert und somit Balken angezeigt wird, auch wenn sie Unterbefehle hat.

Ich habe gerade versucht, den folgenden Code und es funktioniert wie erwartet:

function __fish_prog_needs_command 
    set cmd (commandline -opc) 
    if [ (count $cmd) -eq 1 -a $cmd[1] = 'prog' ] 
    return 0 
    end 
    return 1 
end 

function __fish_prog_using_command 
    set cmd (commandline -opc) 
    if [ (count $cmd) -gt 1 ] 
    if [ $argv[1] = $cmd[2] ] 
     return 0 
    end 
    end 
    return 1 
end 

complete -f -c prog -n '__fish_prog_needs_command' -a bar 

complete -f -c prog -n '__fish_prog_needs_command' -a foo 
complete -f -c prog -n '__fish_prog_using_command foo' -a a 
complete -f -c prog -n '__fish_prog_using_command foo' -a b 
complete -f -c prog -n '__fish_prog_using_command foo' -a c 

complete -f -c prog -n '__fish_prog_needs_command' -a baz 

Ausgabe von Vollendung:

➤ prog <Tab> 
bar baz foo 
➤ prog foo <Tab> 
a b c 
➤ prog foo 

Ist das, was Sie wollen?

+0

Ja, das hat funktioniert, danke. – Michael

+0

Danke für das tolle Beispiel @terje und @Michael! Anstatt "complete -f -c prog -n" __fish_prog_using_command foo "-aa" aufzulisten, vervollständige -f -c prog -n "__fish_prog_using_command foo '-ab" und "complete -f -c prog -n" __fish_prog_using_command foo "-ac" in separaten Zeilen, ich glaube, Sie können alle drei Vervollständigungen mit einem Leerzeichen abgrenzen: 'complete -f -c prog -n '__fish_prog_using_command foo' -a" abc "' – yegeniy