def fish_engine

cli@0.2.0.0 · cli.bend:7223

import cli@0.2.0.0/cli.bend as Cli
import Base

7223# The function every fish script shares, for function prefix id: it walks
7224# the words before the cursor as the bash engine does.
7225def fish_engine(+id: String) -> List<&2, String>:
7226  [
7227    "",
7228    "# Succeeds when the words before the cursor reach path $argv[1] and the next",
7229    "# word can be one of $argv[2..-1]: n, an option or subcommand; h, a",
7230    "# subcommand after help; v, an option's value; p<k>, positional slot k.",
7231    String.concat(["function ", id, "_is"]),
7232    "    # fish 4 deprecates -o, which fish 3 needs.",
7233    "    set -l words (commandline -xpc 2>/dev/null; or commandline -opc)",
7234    "    set -e words[1]",
7235    "    set -l path 0",
7236    String.concat(["    set -l shape (", id, "_shape 0 | string split ' ')"]),
7237    "    set -l want 0",
7238    "    set -l end 0",
7239    "    set -l help 0",
7240    "    set -l n 0",
7241    "    for word in $words",
7242    "        if test $want = 1",
7243    "            set want 0",
7244    "        else if test $end = 0; and test \"$word\" = --",
7245    "            set end 1",
7246    "        else if test $end = 0; and string match -q -r -- '^--.' $word",
7247    String.concat(["            if not string match -q -- '*=*' $word; and ", id, "_valued $path $word"]),
7248    "                set want 1",
7249    "            end",
7250    "        else if test $end = 0; and string match -q -r -- '^-.' $word",
7251    "            set -l chars (string split '' -- (string sub -s 2 -- $word))",
7252    "            for i in (seq (count $chars))",
7253    String.concat(["                if ", id, "_valued $path -$chars[$i]"]),
7254    "                    test $i = (count $chars); and set want 1",
7255    "                    break",
7256    "                end",
7257    "            end",
7258    "        else if test $shape[1] = 1",
7259    "            if test $end = 1",
7260    "                return 1",
7261    "            else if test $help = 0; and test \"$word\" = help",
7262    "                set help 1",
7263    "            else",
7264    String.concat(["                set path (", id, "_dest $path $word); or return 1"]),
7265    String.concat(["                set shape (", id, "_shape $path | string split ' ')"]),
7266    "            end",
7267    "        else",
7268    "            # n counts the slots filled. The variadic slot stays open, but a",
7269    "            # rest() value takes every later word, so nothing more completes.",
7270    "            set n (math $n + 1)",
7271    "            if test $n = $shape[2]; and test $shape[3] = 1",
7272    "                set end 1",
7273    "            else if test $n = $shape[2]",
7274    "                set n (math $n - 1)",
7275    "            end",
7276    "        end",
7277    "    end",
7278    "    test $path = $argv[1]; or return 1",
7279    "    set -l tags",
7280    "    if test $want = 1",
7281    "        set tags v",
7282    "    else",
7283    "        test $end = 0; and test $help = 0; and set -a tags n",
7284    "        test $end = 0; and test $help = 1; and set -a tags h",
7285    "        test $shape[1] = 0; and test $help = 0; and set -a tags p(math $n + 1)",
7286    "    end",
7287    "    for tag in $argv[2..-1]",
7288    "        contains -- $tag $tags; and return 0",
7289    "    end",
7290    "    return 1",
7291    "end",
7292  ]

Uses (3)

Used by (1)