mirror of
https://github.com/arcan1s/arcanis.me.git
synced 2025-07-23 01:19:55 +00:00
updated papers
Refactoring of an English part is done
This commit is contained in:
@ -6,13 +6,13 @@ layout: paper
|
||||
tags: linux, development
|
||||
title: Writting own Shell completions. Bash
|
||||
short: writting-own-completions-p2
|
||||
description: <img hspace="10" hspace="10" align="right" src="/resources/papers/bash_completion.png"> Some basics of creating a completion files for own application are described in these articles.
|
||||
description: <figure class="sign"><img src="/resources/papers/bash_completion.png" alt="bash_completion"></figure> Some basics of creating a completion files for own application are described in these articles.
|
||||
---
|
||||
<h2><a name="preamble" class="anchor" href="#preamble"><span class="octicon octicon-link"></span></a>Preamble</h2>
|
||||
<p>While developing <a href="/ru/projects/netctl-gui">one of my projects</a> I have wanted to add completion files. I have already tried to create these files, but I was too lazy to read some manuals about it.</p>
|
||||
<h2><a href="#preamble" class="anchor" name="preamble"><span class="octicon octicon-link"></span></a>Preamble</h2>
|
||||
<p>While developing <a href="/ru/projects/netctl-gui" title="Netctl-gui project page">one of my projects</a> I have wanted to add completion files. I have already tried to create these files, but I was too lazy to read some manuals about it.</p>
|
||||
|
||||
<h2><a name="introduction" class="anchor" href="#introduction"><span class="octicon octicon-link"></span></a>Introduction</h2>
|
||||
<p>Bash, <a href="/ru/2014/07/17/writting-own-completions-p1">unlike zsh</a>, demands some dirty workarounds for completions. Cursory have Googled, I have not found a more or less normal tutorials, so it is based on the available <code>pacman</code> completion files in my system.</p>
|
||||
<h2><a href="#introduction" class="anchor" name="introduction"><span class="octicon octicon-link"></span></a>Introduction</h2>
|
||||
<p>Bash, <a href="/ru/2014/07/17/writting-own-completions-p1" title="Zsh completions paper">unlike zsh</a>, demands some dirty workarounds for completions. Cursory have Googled, I have not found a more or less normal tutorials, so it is based on the available <code>pacman</code> completion files in my system.</p>
|
||||
|
||||
<p>Lets consider the example of the same my application. I remind you that a part of help message is as follows:</p>
|
||||
|
||||
@ -32,7 +32,7 @@ netctl-gui [ -h | --help ] [ -e ESSID | --essid ESSID ] [ -с FILE | --config FI
|
||||
</ul>
|
||||
</p>
|
||||
|
||||
<h2><a name="file" class="anchor" href="#file"><span class="octicon octicon-link"></span></a>The file pattern</h2>
|
||||
<h2><a href="#file" class="anchor" name="file"><span class="octicon octicon-link"></span></a>The file pattern</h2>
|
||||
<p>Here <b>all</b> variables must return an array. And there no specific formats. First we declare the flags and then we describe all other variables. As I am not going to describe the functions in more detail below I remind you that <code>_netctl_profiles()</code> should be generated each time:
|
||||
|
||||
{% highlight bash %}
|
||||
@ -55,9 +55,10 @@ And finally again <b>without isolation in a separate function</b> we create a de
|
||||
{% highlight bash %}
|
||||
complete -F _netctl_gui netctl-gui
|
||||
{% endhighlight %}
|
||||
|
||||
</p>
|
||||
|
||||
<h2><a name="flags" class="anchor" href="#flags"><span class="octicon octicon-link"></span></a>Flags</h2>
|
||||
<h2><a href="#flags" class="anchor" name="flags"><span class="octicon octicon-link"></span></a>Flags</h2>
|
||||
<p>As it was said above there is no specific format, so all available flags declare by array:
|
||||
|
||||
{% highlight bash %}
|
||||
@ -75,9 +76,10 @@ _netctl_gui_arglist=(
|
||||
'--set-opts'
|
||||
)
|
||||
{% endhighlight %}
|
||||
|
||||
</p>
|
||||
|
||||
<h2><a name="variables" class="anchor" href="#variables"><span class="octicon octicon-link"></span></a>Arrays of variables</h2>
|
||||
<h2><a href="#variables" class="anchor" name="variables"><span class="octicon octicon-link"></span></a>Arrays of variables</h2>
|
||||
<p>I just give a function that looked like this in zsh:
|
||||
|
||||
{% highlight bash %}
|
||||
@ -93,9 +95,10 @@ _netctl_profiles() {
|
||||
echo $(find /etc/netctl -maxdepth 1 -type f -printf "%f\n")
|
||||
}
|
||||
{% endhighlight %}
|
||||
|
||||
</p>
|
||||
|
||||
<h2><a name="body" class="anchor" href="#body"><span class="octicon octicon-link"></span></a>Function</h2>
|
||||
<h2><a href="#body" class="anchor" name="body"><span class="octicon octicon-link"></span></a>Function</h2>
|
||||
<p>The variable <code>COMPREPLY</code> responds for completion in Bash. To keep track of the current state function <code>_get_comp_words_by_ref</code> must be called with parameters <code>cur</code> (current flag) and <code>prev</code> (previous flag, it is the state). Also some point for case are needed (variables <code>want*</code>). Function <code>compgen</code> is used for completion generation. A list of words is given after flag <code>-W</code>. (Also there is flag <code>-F</code> which requires a function as argument, but it gives warning for me.) The last argument is a current string to which you want to generate completion.</p>
|
||||
|
||||
<p>So, here is our function:
|
||||
@ -134,7 +137,8 @@ _netctl_gui() {
|
||||
true
|
||||
}
|
||||
{% endhighlight %}
|
||||
|
||||
</p>
|
||||
|
||||
<h2><a name="conclusion" class="anchor" href="#conclusion"><span class="octicon octicon-link"></span></a>Conclusion</h2>
|
||||
<p>File should be places to <code>/usr/share/bash-completion/completions/</code> with any name. You may found the example <a href="https://raw.githubusercontent.com/arcan1s/netctl-gui/master/sources/gui/bash-completions">in my repository</a>.</p>
|
||||
<h2><a href="#conclusion" class="anchor" name="conclusion"><span class="octicon octicon-link"></span></a>Conclusion</h2>
|
||||
<p>File should be places to <code>/usr/share/bash-completion/completions/</code> with any name. You may found the example <a href="https://raw.githubusercontent.com/arcan1s/netctl-gui/master/sources/gui/bash-completions" title="File" type="text/plain">in my repository</a>.</p>
|
||||
|
Reference in New Issue
Block a user