Skip to content

Commit f0d04a9

Browse files
committed
Add basic autocompletion for Zsh
1 parent 71f7482 commit f0d04a9

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ you are ready to Rust. If you decide Rust isn't your thing, you can
5858
completely remove it from your system by running `rustup self
5959
uninstall`.
6060

61+
#### Enable tab completion for Zsh
62+
63+
Copy [`src/rustup-cli/zsh/_rustup`](https://github.com/rust-lang-nursery/rustup.rs/blob/master/src/rustup-cli/zsh/_rustup) into a directory, e.g. `~/.zfunc/`,
64+
then add the following line in your `~/.zshrc` before `compinit`:
65+
66+
```zsh
67+
fpath+=~/.zfunc
68+
```
69+
70+
#### Enable tab completion for Bash
71+
72+
Waiting for [#278](https://github.com/rust-lang-nursery/rustup.rs/issues/278)
73+
6174
## How rustup works
6275

6376
`rustup` is a *toolchain multiplexer*. It installs and manages many

src/rustup-cli/zsh/_rustup

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#compdef rustup
2+
3+
typeset -A opt_args
4+
5+
_rustup() {
6+
7+
_arguments \
8+
'(- 1 *)'{-h,--help}'[show help message]' \
9+
'(- 1 *)'{-v,--verbose}'[use verbose output]' \
10+
'(- 1 *)'{-V,--version}'[show version information]' \
11+
'1: :_rustup_cmds' \
12+
'*:: :->args'
13+
14+
case $state in
15+
args)
16+
case $words[1] in
17+
component)
18+
local -a subcommands=(
19+
'list:list installed and available components'
20+
'add:add a component to a toolchain'
21+
'remove:remove a component from a toolchain'
22+
'help:prints this message or the help of the given subcommand(s)'
23+
)
24+
_arguments \
25+
'(-h, --help)'{-h,--help}'[show help message]' \
26+
'1: :{_describe 'subcommands' subcommands}' \
27+
;;
28+
29+
default)
30+
_arguments \
31+
'(-h, --help)'{-h,--help}'[show help message]' \
32+
': :_get_local_toolchains'
33+
;;
34+
35+
doc)
36+
_arguments \
37+
'(-h, --help)'{-h,--help}'[show help message]' \
38+
'--book:the Rust Programming Language book' \
39+
'--std:standard library API documentation' \
40+
;;
41+
42+
help)
43+
_arguments \
44+
'(-h, --help)'{-h,--help}'[show help message]' \
45+
': :_rustup_cmds' \
46+
;;
47+
48+
man)
49+
_arguments \
50+
'(-h, --help)'{-h,--help}'[show help message]' \
51+
'--toolchain: :_get_local_toolchains' \
52+
;;
53+
54+
override)
55+
local -a subcommands=(
56+
'list:list directory toolchain overrides'
57+
'set:set the override toolchain for a directory'
58+
'unset:remove the override toolchain for a directory'
59+
'help:prints this message or the help of the given subcommand(s)'
60+
)
61+
_arguments \
62+
'(-h, --help)'{-h,--help}'[show help message]' \
63+
'1: :{_describe 'subcommands' subcommands}' \
64+
;;
65+
66+
run)
67+
_arguments \
68+
'(-h, --help)'{-h,--help}'[show help message]' \
69+
'1: :_get_local_toolchains' \
70+
'2:command' \
71+
;;
72+
73+
74+
self)
75+
local -a subcommands=(
76+
'update:download and install updates to rustup'
77+
'uninstall:uninstall rustup'
78+
'upgrade-data:upgrade the internal data format'
79+
'help:prints this message or the help of the given subcommand(s)'
80+
)
81+
_arguments \
82+
'(-h, --help)'{-h,--help}'[show help message]' \
83+
'1: :{_describe 'subcommands' subcommands}' \
84+
;;
85+
86+
set)
87+
local -a subcommands=(
88+
'default-host:The triple used to identify toolchains' \
89+
'help:prints this message or the help of the given subcommand(s)'
90+
)
91+
_arguments \
92+
'(-h, --help)'{-h,--help}'[show help message]' \
93+
'1: :{_describe 'subcommands' subcommands}' \
94+
;;
95+
show)
96+
_arguments \
97+
'(-h, --help)'{-h,--help}'[show help message]' \
98+
;;
99+
100+
target)
101+
local -a subcommands=(
102+
'list:list installed and available targets'
103+
'add:add a target to a toolchain'
104+
'remove:remove a target from a toolchain'
105+
'help:prints this message or the help of the given subcommand(s)'
106+
)
107+
_arguments \
108+
'(-h, --help)'{-h,--help}'[show help message]' \
109+
'1: :{_describe 'subcommands' subcommands}' \
110+
;;
111+
112+
toolchain)
113+
local -a subcommands=(
114+
'list:list installed toolchains'
115+
'install:install or update a toolchain'
116+
'uninstall:uninstall a toolchain'
117+
'link:symlink a custom toolchain to a directory'
118+
'help:prints this message or the help of the given subcommand(s)'
119+
)
120+
_arguments \
121+
'(-h, --help)'{-h,--help}'[show help message]' \
122+
'1: :{_describe 'subcommands' subcommands}' \
123+
;;
124+
125+
update)
126+
_arguments \
127+
'(-h, --help)'{-h,--help}'[show help message]' \
128+
'*: :_get_local_toolchains'
129+
;;
130+
131+
which)
132+
_arguments \
133+
'(-h, --help)'{-h,--help}'[show help message]' \
134+
':command'
135+
;;
136+
esac
137+
;;
138+
esac
139+
}
140+
141+
_get_local_toolchains() {
142+
local -a toolchains=()
143+
rustup toolchain list 2>/dev/null | while read line
144+
do
145+
toolchains+="${line%%-*}"
146+
done
147+
_describe 'toolchains' toolchains
148+
}
149+
150+
_rustup_cmds(){
151+
local -a commands=(
152+
'show:show the active and installed toolchains'
153+
'update:update Rust toolchains'
154+
'default:set the default toolchain'
155+
'toolchain:modify or query the installed toolchains'
156+
'target:modify a toolchains supported targets'
157+
'component:modify a toolchains installed components'
158+
'override:modify directory toolchain overrides'
159+
'run:run a command with an environment configured for a given toolchain'
160+
'which:display which binary will be run for a given command'
161+
'doc:open the documentation for the current toolchain'
162+
'man:view the man page for a given command'
163+
'self:modify the rustup installation'
164+
'set:alter rustup settings'
165+
'help:prints this message or the help of the given subcommand(s)'
166+
)
167+
168+
_describe 'command' commands
169+
170+
}
171+
172+
_rustup

0 commit comments

Comments
 (0)