TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Shell Completions in Rust

150 pointsby JoshMcguiganabout 6 years ago

6 comments

JoshMcguiganabout 6 years ago
Author here. This blog post is the result of my puzzling over how auto-complete works in the shell, and subsequently whether shell completion scripts could be written in Rust. It also introduces a new Rust crate, shell_completion[0], which is a library that exposes some low level primitives for writing shell completion scripts in Rust. shell_completion is certainly in its infancy, so I&#x27;d be happy to have contributors of any kind (both code and opinions are welcome).<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;joshmcguigan&#x2F;shell_completion" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;joshmcguigan&#x2F;shell_completion</a>
评论 #19951887 未加载
评论 #19951192 未加载
dmixabout 6 years ago
That sounds useful. I used to write tons of ZSH scripts when I used to obsessively make my Linux desktop experience nice and automated. Writing custom completions always felt awkward for me and unintuitive (like most ZSH programming).<p>Does your shell_completion library work with ZSH? I&#x27;m not familiar with how sh&#x2F;bash differs here from ZSH but I&#x27;d image it&#x27;d be easy to make compatible.<p>I&#x27;m waiting patiently for Evlish to mature so I can use their Go-like (and Go-powered) scripting language and burn my old ZSH scripts in a fire. <a href="https:&#x2F;&#x2F;github.com&#x2F;elves&#x2F;elvish" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;elves&#x2F;elvish</a>
评论 #19951777 未加载
codetrotterabout 6 years ago
Seeing this got me curious to know if any work has been done on generating completions for applications that make use of the clap command-line argument parser.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;clap-rs&#x2F;clap" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;clap-rs&#x2F;clap</a><p><a href="https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;clap" rel="nofollow">https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;clap</a><p>Among other things I found this currently open issue that has a lot of discussion and information in it:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;clap-rs&#x2F;clap&#x2F;issues&#x2F;568" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;clap-rs&#x2F;clap&#x2F;issues&#x2F;568</a><p>It might be of interest to you as well, OP?
评论 #19951303 未加载
评论 #19954649 未加载
chubotabout 6 years ago
If I&#x27;m understanding correctly, this is a shim that:<p>1) serializes the fields the bash completion API gives you (when you hit TAB)<p>2) exports them to Rust, then<p>3) passes the Rust program&#x27;s results back to bash?<p>I&#x27;ve been working on similar problems, and I understand why that&#x27;s appealing. Bash is a pretty bad language to write completion scripts in! It&#x27;s really bad at string and array processing [1].<p>----<p>Though on a quick glance, if you&#x27;re exporting $COMP_LINE and $COMP_POINT, then it&#x27;s making the job of the completion script very hard, since you&#x27;re passing off the responsibility of parsing shell to it:<p>In other words, if you have something like:<p><pre><code> mycommand --flag arg\ with\ spaces --flag2=&lt;TAB&gt; </code></pre> Then it&#x27;s not desirable to pass the entire string. It would be better to parse it and pass:<p><pre><code> [&quot;mycommand&quot;, &quot;--flag&quot;, &quot;arg with spaces&quot;, &quot;--flag2=&quot;] # and indicate index 3 being completed </code></pre> Another common case is:<p><pre><code> echo $(dirname &lt;TAB&gt; # I don&#x27;t need echo $( to complete </code></pre> I noticed this deficiency of bash while essentially cloning the bash API for Oil: <a href="https:&#x2F;&#x2F;www.oilshell.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.oilshell.org&#x2F;</a><p>One consequence of this is that the bash-completion project (maintained separately from bash) actually has an ad hoc bash parser in bash! This is really bad -- unsurprisingly it has obvious bugs, like not being able to complete inside command subs. An ad hoc bash parser in Rust would also be bad.<p>So instead Oil parses shell for you -- since it has a shell parser! -- and then it gives you the partial argv array. So then the plugin is only responsible for what it can reasonably do.<p>-----<p>Additional, I proposed that this could be made into a protocol that other shells consume. So instead of writing bash completions in Rust, you could write zsh&#x2F;fish&#x2F;bash&#x2F;Oil completions in Rust.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;oilshell&#x2F;oil&#x2F;wiki&#x2F;Shellac-Protocol-Proposal" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;oilshell&#x2F;oil&#x2F;wiki&#x2F;Shellac-Protocol-Propos...</a><p>A lot of people are already &quot;scraping&quot; bash completions this way, i.e. to use in zsh or their own shells, so it should not be hard to come up with something &quot;standard&quot;, given those working examples.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;oilshell&#x2F;oil&#x2F;wiki&#x2F;Projects-Already-Doing-Something-Like-Shellac" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;oilshell&#x2F;oil&#x2F;wiki&#x2F;Projects-Already-Doing-...</a><p>If you&#x27;re interested in chatting about it please join <a href="https:&#x2F;&#x2F;oilshell.zulipchat.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;oilshell.zulipchat.com&#x2F;</a> .<p>I wouldn&#x27;t say it&#x27;s the #1 priority now because Oil already emulates bash quite well, but I think a bunch of people are motivated because they 1) don&#x27;t want to write completions N times, for every shell and 2) Don&#x27;t want to write completions in the bash language.<p>home page: main page: <a href="https:&#x2F;&#x2F;github.com&#x2F;oilshell&#x2F;oil&#x2F;wiki&#x2F;Shell-Autocompletion" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;oilshell&#x2F;oil&#x2F;wiki&#x2F;Shell-Autocompletion</a><p>IMO it makes sense to write completions in whatever language the command line tool &#x2F; flag parser is written in.<p>[1] <a href="https:&#x2F;&#x2F;www.oilshell.org&#x2F;blog&#x2F;2016&#x2F;11&#x2F;06.html" rel="nofollow">https:&#x2F;&#x2F;www.oilshell.org&#x2F;blog&#x2F;2016&#x2F;11&#x2F;06.html</a>
评论 #19950913 未加载
评论 #19954068 未加载
xliivabout 6 years ago
I did this <a href="https:&#x2F;&#x2F;github.com&#x2F;xliiv&#x2F;fui" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;xliiv&#x2F;fui</a> This is something different, but maybe someone could say it&#x27;s sort of &quot;completions&quot;? Fui also has a feature which allow to copy&#x2F;dump a whole form (ctrl-k) string which can be pasted to bash.
tormehabout 6 years ago
The ion shell is written in what I believe is pure Rust, and has autocompletion by default.
评论 #19950687 未加载