From f0d04a9c14885234dbd9d57813e8f4f4d6b1ea2c Mon Sep 17 00:00:00 2001 From: Knight Date: Thu, 25 Aug 2016 23:47:14 +0800 Subject: [PATCH] Add basic autocompletion for Zsh --- README.md | 13 +++ src/rustup-cli/zsh/_rustup | 172 +++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 src/rustup-cli/zsh/_rustup diff --git a/README.md b/README.md index f053e1255b..3e04b36bbe 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,19 @@ you are ready to Rust. If you decide Rust isn't your thing, you can completely remove it from your system by running `rustup self uninstall`. +#### Enable tab completion for Zsh + +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/`, +then add the following line in your `~/.zshrc` before `compinit`: + +```zsh +fpath+=~/.zfunc +``` + +#### Enable tab completion for Bash + +Waiting for [#278](https://github.com/rust-lang-nursery/rustup.rs/issues/278) + ## How rustup works `rustup` is a *toolchain multiplexer*. It installs and manages many diff --git a/src/rustup-cli/zsh/_rustup b/src/rustup-cli/zsh/_rustup new file mode 100644 index 0000000000..fb9c9f413e --- /dev/null +++ b/src/rustup-cli/zsh/_rustup @@ -0,0 +1,172 @@ +#compdef rustup + +typeset -A opt_args + +_rustup() { + +_arguments \ + '(- 1 *)'{-h,--help}'[show help message]' \ + '(- 1 *)'{-v,--verbose}'[use verbose output]' \ + '(- 1 *)'{-V,--version}'[show version information]' \ + '1: :_rustup_cmds' \ + '*:: :->args' + +case $state in + args) + case $words[1] in + component) + local -a subcommands=( + 'list:list installed and available components' + 'add:add a component to a toolchain' + 'remove:remove a component from a toolchain' + 'help:prints this message or the help of the given subcommand(s)' + ) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '1: :{_describe 'subcommands' subcommands}' \ + ;; + + default) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + ': :_get_local_toolchains' + ;; + + doc) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '--book:the Rust Programming Language book' \ + '--std:standard library API documentation' \ + ;; + + help) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + ': :_rustup_cmds' \ + ;; + + man) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '--toolchain: :_get_local_toolchains' \ + ;; + + override) + local -a subcommands=( + 'list:list directory toolchain overrides' + 'set:set the override toolchain for a directory' + 'unset:remove the override toolchain for a directory' + 'help:prints this message or the help of the given subcommand(s)' + ) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '1: :{_describe 'subcommands' subcommands}' \ + ;; + + run) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '1: :_get_local_toolchains' \ + '2:command' \ + ;; + + + self) + local -a subcommands=( + 'update:download and install updates to rustup' + 'uninstall:uninstall rustup' + 'upgrade-data:upgrade the internal data format' + 'help:prints this message or the help of the given subcommand(s)' + ) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '1: :{_describe 'subcommands' subcommands}' \ + ;; + + set) + local -a subcommands=( + 'default-host:The triple used to identify toolchains' \ + 'help:prints this message or the help of the given subcommand(s)' + ) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '1: :{_describe 'subcommands' subcommands}' \ + ;; + show) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + ;; + + target) + local -a subcommands=( + 'list:list installed and available targets' + 'add:add a target to a toolchain' + 'remove:remove a target from a toolchain' + 'help:prints this message or the help of the given subcommand(s)' + ) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '1: :{_describe 'subcommands' subcommands}' \ + ;; + + toolchain) + local -a subcommands=( + 'list:list installed toolchains' + 'install:install or update a toolchain' + 'uninstall:uninstall a toolchain' + 'link:symlink a custom toolchain to a directory' + 'help:prints this message or the help of the given subcommand(s)' + ) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '1: :{_describe 'subcommands' subcommands}' \ + ;; + + update) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + '*: :_get_local_toolchains' + ;; + + which) + _arguments \ + '(-h, --help)'{-h,--help}'[show help message]' \ + ':command' + ;; + esac + ;; +esac +} + +_get_local_toolchains() { + local -a toolchains=() + rustup toolchain list 2>/dev/null | while read line + do + toolchains+="${line%%-*}" + done + _describe 'toolchains' toolchains +} + +_rustup_cmds(){ +local -a commands=( +'show:show the active and installed toolchains' +'update:update Rust toolchains' +'default:set the default toolchain' +'toolchain:modify or query the installed toolchains' +'target:modify a toolchains supported targets' +'component:modify a toolchains installed components' +'override:modify directory toolchain overrides' +'run:run a command with an environment configured for a given toolchain' +'which:display which binary will be run for a given command' +'doc:open the documentation for the current toolchain' +'man:view the man page for a given command' +'self:modify the rustup installation' +'set:alter rustup settings' +'help:prints this message or the help of the given subcommand(s)' +) + +_describe 'command' commands + +} + +_rustup