From b0991484624c0ddd4bdd45eb64619144f189b42a Mon Sep 17 00:00:00 2001
From: johannst <johannst@users.noreply.github.com>
Date: Sat, 18 Nov 2023 11:39:35 +0000
Subject: deploy: 947ca7041270c1a0bf8ac39387e611931f606aa1

---
 tools/fish.html | 142 ++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 112 insertions(+), 30 deletions(-)

(limited to 'tools/fish.html')

diff --git a/tools/fish.html b/tools/fish.html
index 1b16736..9f1a556 100644
--- a/tools/fish.html
+++ b/tools/fish.html
@@ -200,10 +200,16 @@ shell restart</li>
     -x  export to ENV
     -u  unexport from ENV
 </code></pre>
+<h3 id="special-variables-ref"><a class="header" href="#special-variables-ref">Special Variables <a href="https://fishshell.com/docs/current/language.html#special-variables">ref</a></a></h3>
+<pre><code class="language-sh">$status      # exit code of last command
+$pipestatus  # list of exit codes of pipe chain
+
+$CMD_DURATION   # runtime of last command in ms
+</code></pre>
 <h3 id="lists"><a class="header" href="#lists">Lists</a></h3>
 <p>In <code>fish</code> all variables are lists (start with index <code>1</code>, but lists can't
 contain lists.</p>
-<pre><code class="language-text">set foo a b c d
+<pre><code class="language-sh">set foo a b c d
 
 echo $foo[1]      # a
 echo $foo[-1]     # d
@@ -211,40 +217,39 @@ echo $foo[2..3]   # b c
 echo $foo[1 3]    # a c
 </code></pre>
 <p><code>$</code> can be seen as dereference operator.</p>
-<pre><code class="language-text">set foo a; set a 1337
+<pre><code class="language-sh">set foo a; set a 1337
 echo $$foo  # outputs 1337
 </code></pre>
 <p>Cartesian product.</p>
-<pre><code class="language-text">echo file.{h,cc}
+<pre><code class="language-sh">echo file.{h,cc}
 # file.h file.cc
 
 echo {a,b}{1,2}
 # a1 b1 a2 b2
 </code></pre>
-<h3 id="special-variables-lists"><a class="header" href="#special-variables-lists">Special Variables (Lists)</a></h3>
-<pre><code class="language-text">$status      # exit code of last command
-$pipestatus  # list of exit codes of pipe chain
-
-$CMD_DURATION   # runtime of last command in ms
-</code></pre>
-<h4 id="path"><a class="header" href="#path"><code>*PATH</code></a></h4>
+<h4 id="path-ref"><a class="header" href="#path-ref"><code>*PATH</code> <a href="https://fishshell.com/docs/current/language.html#path-variables">ref</a></a></h4>
 <p>Lists ending with <code>PATH</code> are automatically split at <code>:</code> when used and joined
-with <code>:</code> when exported to the environment.</p>
-<pre><code class="language-text">set -x BLA_PATH a:b:c:d
-echo $BLA_PATH              # a b c d
-env | grep BLA_PATH         # BLA_PATH=a:b:c:d
+with <code>:</code> when quoted or exported to the environment.</p>
+<pre><code class="language-sh">set -x BLA_PATH a:b:c:d
+echo $BLA_PATH           # a b c d
+echo &quot;$BLA_PATH&quot;         # a:b:c:d          (quoted)
+env | grep BLA_PATH      # BLA_PATH=a:b:c:d (env)
+
+set FOO_PATH x y z
+echo $FOO_PATH           # x y z
+echo &quot;$FOO_PATH&quot;         # x:y:z
 </code></pre>
 <h2 id="command-handling"><a class="header" href="#command-handling">Command Handling</a></h2>
-<pre><code class="language-text"># sub-commands are not run in quotes
+<pre><code class="language-sh"># sub-commands are not run in quotes
 echo &quot;ls output: &quot;(ls)
 </code></pre>
 <h3 id="io-redirection"><a class="header" href="#io-redirection">I/O redirection</a></h3>
-<pre><code class="language-text"># 'noclobber', fail if 'log' already exists
+<pre><code class="language-sh"># 'noclobber', fail if 'log' already exists
 echo foo &gt;? log
 </code></pre>
 <h2 id="control-flow"><a class="header" href="#control-flow">Control Flow</a></h2>
 <h3 id="if--else"><a class="header" href="#if--else"><code>if</code> / <code>else</code></a></h3>
-<pre><code class="language-text">if grep foo bar
+<pre><code class="language-sh">if grep foo bar
     # do sth
 else if grep foobar bar
     # do sth else
@@ -253,7 +258,7 @@ else
 end
 </code></pre>
 <h3 id="switch"><a class="header" href="#switch"><code>switch</code></a></h3>
-<pre><code class="language-text">switch (echo foo)
+<pre><code class="language-sh">switch (echo foo)
 case 'foo*'
     # do start with foo
 case bar dudel
@@ -263,18 +268,18 @@ case '*'
 end
 </code></pre>
 <h3 id="while-loop"><a class="header" href="#while-loop"><code>while</code> Loop</a></h3>
-<pre><code class="language-text">while true
+<pre><code class="language-sh">while true
     echo foo
 end
 </code></pre>
 <h3 id="for-loop"><a class="header" href="#for-loop"><code>for</code> Loop</a></h3>
-<pre><code class="language-text">for f in (ls)
+<pre><code class="language-sh">for f in (ls)
     echo $f
 end
 </code></pre>
 <h2 id="functions"><a class="header" href="#functions">Functions</a></h2>
 <p>Function arguments are passed via <code>$argv</code> list.</p>
-<pre><code class="language-text">function fn_foo
+<pre><code class="language-sh">function fn_foo
     echo $argv
 end
 </code></pre>
@@ -285,39 +290,116 @@ the function lazily if found.</p>
 <p>This is the preferred way over monolithically defining all functions in a
 startup script.</p>
 <h3 id="helper"><a class="header" href="#helper">Helper</a></h3>
-<pre><code class="language-text">functions         # list al functions
+<pre><code class="language-sh">functions         # list al functions
 functions foo     # describe function 'foo'
 functions -e foo  # erase function 'foo'
 
 funced foo        # edit function 'foo'
                   # '-e vim' to edit in vim
 </code></pre>
+<h3 id="argument-parsing-and-completion"><a class="header" href="#argument-parsing-and-completion">Argument parsing and completion</a></h3>
+<p><code>argparse</code> puts options into variables of name <code>_flag_NAME</code>.</p>
+<p>References:</p>
+<ul>
+<li><a href="https://fishshell.com/docs/current/language.html#argument-handling">Argument Handling</a></li>
+<li><a href="https://fishshell.com/docs/current/cmds/argparse.html"><code>argparse</code></a></li>
+<li><a href="https://fishshell.com/docs/current/completions.html">Writing your own completions</a></li>
+<li><a href="https://fishshell.com/docs/current/cmds/complete.html"><code>complete</code></a></li>
+</ul>
+<pre><code class="language-sh">function moose --d &quot;my moose fn&quot;
+    # h/help   : short / long option (boolean)
+    # color    : only long option (boolean)
+    # u/user=  : option with required argument, only last specified is taken
+    # f/file+= : option with required argument, can be specified multiple times
+    #
+    argparse h/help color= u/user= f/file=+ -- $argv
+    or return
+
+    if set -ql _flag_help
+        echo &quot;usage ...&quot;
+        return 0
+    end
+
+    set -ql _flag_file
+    and echo &quot;file=$_flag_file | cnt:&quot; (count $_flag_file)
+
+    set -ql _flag_color
+    and echo &quot;color=$_flag_color&quot;
+
+    set -ql _flag_user
+    and echo &quot;user=$_flag_user&quot;
+end
+
+# Delete all previous defined completions for 'moose'.
+complete -c moose -e
+
+# Don't complete files for command.
+complete -c moose --no-files
+
+# Help completion.
+#   -n specifies a conditions. The completion is only active if the command
+#      returns 0.
+complete -c moose -s h -l help -n &quot;not __fish_contains_opt -s h help&quot; \
+    --description &quot;Print usage help and exit&quot;
+
+# File completion.
+#   -F force complete files (overwrite --no-files).
+#   -r requires argument.
+complete -c moose -s f -l file -F -r \
+    --description &quot;Specify file (can be passed multiple times)&quot;
+
+# Color completion.
+#    -a options for completion.
+#    -x short for -r and --no-files (-f)
+complete -c moose -x -l color -a &quot;red blue&quot; \
+    --description &quot;Specify a color.&quot;
+
+# User completion.
+#    -a options for completion. Call a function to generate arguments.
+complete -c moose -x -s u -l user -a &quot;(__fish_complete_users)&quot; \
+    --description &quot;Specify a user&quot;
+</code></pre>
 <h2 id="prompt"><a class="header" href="#prompt">Prompt</a></h2>
 <p>The prompt is defined by the output of the <code>fish_prompt</code> function.</p>
-<pre><code class="language-text">function fish_prompt
+<pre><code class="language-sh">function fish_prompt
     set -l cmd_ret
     echo &quot;&gt; &quot;(pwd) $cmd_ret&quot; &quot;
 end
 </code></pre>
 <blockquote>
-<p>Use <code>set_color</code> to manipulate terminal colors.</p>
+<p>Use <code>set_color</code> to manipulate terminal colors and <code>set_color -c</code> to print the
+current colors.</p>
 </blockquote>
 <h2 id="useful-builtins"><a class="header" href="#useful-builtins">Useful Builtins</a></h2>
-<pre><code class="language-text"># history
+<p>List all builtins with <code>builtins -n</code>.</p>
+<pre><code class="language-sh"># history
 history search &lt;str&gt;   # search history for &lt;str&gt;
 history merge          # merge histories from fish sessions
 
 # list
-count $var            # count elements in list
+count $var             # count elements in list
+
+contains /bin $PATH    # return 0 (true) 1 (false)
+contains -i /bin $PATH # additionally print index on stdout
 
 # string
 string split SEP STRING
+
+# math
+math -b hex 4096       # output dec as hex
+math 0x1000            # output hex as dec
+math &quot;log2(1024)&quot;      # call functions
+math -s0 7/3           # integer division (by default float)
+
+# status
+status -f              # abs path of current file
 </code></pre>
 <h2 id="keymaps"><a class="header" href="#keymaps">Keymaps</a></h2>
-<pre><code class="language-text">  Shift-Tab ........... tab-completion with search
-  Alt-Up / Alt-Down ... search history with token under the cursor
-  Alt-l ............... list content of dir under cursor
-  Alt-p ............... append '2&gt;&amp;1 | less;' to current cmdline
+<pre><code class="language-text">  Shift-Tab .............. tab-completion with search
+  Alt-Up / Alt-Down ...... search history with token under the cursor
+  Alt-l .................. list content of dir under cursor
+  Alt-p .................. append '2&gt;&amp;1 | less;' to current cmdline
+  Alt-Left / Alt - Right . prevd / nextd, walk dir history
 </code></pre>
 <h2 id="debug"><a class="header" href="#debug">Debug</a></h2>
 <pre><code class="language-text">  status print-stack-trace .. prints function stacktrace (can be used in scripts)
-- 
cgit v1.2.3