From 0bedbe55aca667bc3894a88451d6e525b928322c Mon Sep 17 00:00:00 2001 From: Stefan Gehr Date: Sun, 14 Jan 2024 13:23:34 +0100 Subject: [PATCH 1/2] Add completions command The command generates the shell completions file for juliaup. For example, to generate the completions for Bash and save them to a file you can run ``` juliaup completions bash > ~/.local/share/bash-completion/completions/juliaup ``` Then you can source this file in your bashrc. --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/bin/juliaup.rs | 2 ++ src/cli.rs | 2 ++ src/command_completions.rs | 22 ++++++++++++++++++++++ src/lib.rs | 1 + 6 files changed, 38 insertions(+) create mode 100644 src/command_completions.rs diff --git a/Cargo.lock b/Cargo.lock index 0df08633..fdfd5e21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -245,6 +245,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97aeaa95557bd02f23fbb662f981670c3d20c5a26e69f7354b28f57092437fcd" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.7" @@ -968,6 +977,7 @@ dependencies = [ "built", "chrono", "clap", + "clap_complete", "cli-table", "cluFlock", "console", diff --git a/Cargo.toml b/Cargo.toml index 20bbbd88..9bc43de6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ codegen-units = 1 [dependencies] clap = { version = "4.3.19", features = ["derive"] } +clap_complete = "4.3.19" dirs = "5.0.1" dunce = "1.0.4" serde = { version = "1.0.175", features = ["derive"] } diff --git a/src/bin/juliaup.rs b/src/bin/juliaup.rs index 0626a369..badea44a 100644 --- a/src/bin/juliaup.rs +++ b/src/bin/juliaup.rs @@ -2,6 +2,7 @@ use anyhow::{Context, Result}; use clap::Parser; use juliaup::cli::{ConfigSubCmd, Juliaup, OverrideSubCmd, SelfSubCmd}; use juliaup::command_api::run_command_api; +use juliaup::command_completions::run_command_completions; #[cfg(not(windows))] use juliaup::command_config_symlinks::run_command_config_symlinks; use juliaup::command_config_versionsdbupdate::run_command_config_versionsdbupdate; @@ -111,5 +112,6 @@ fn main() -> Result<()> { #[cfg(not(feature = "selfupdate"))] SelfSubCmd::Uninstall {} => run_command_selfuninstall_unavailable(), }, + Juliaup::Completions { shell } => run_command_completions(&shell), } } diff --git a/src/cli.rs b/src/cli.rs index 5310eccd..4c6a7e18 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -47,6 +47,8 @@ pub enum Juliaup { Info {}, #[clap(subcommand, name = "self")] SelfSubCmd(SelfSubCmd), + /// Generate tab-completion scripts for your shell + Completions { shell: String }, // This is used for the cron jobs that we create. By using this UUID for the command // We can identify the cron jobs that were created by juliaup for uninstall purposes #[cfg(feature = "selfupdate")] diff --git a/src/command_completions.rs b/src/command_completions.rs new file mode 100644 index 00000000..291ee04a --- /dev/null +++ b/src/command_completions.rs @@ -0,0 +1,22 @@ +use crate::cli; +use anyhow::bail; +use anyhow::Result; +use clap::CommandFactory; +use clap_complete::Shell; +use cli::Juliaup; +use std::io; +use std::str::FromStr; + +pub fn run_command_completions(shell: &str) -> Result<()> { + if let Ok(shell) = Shell::from_str(shell) { + clap_complete::generate( + shell, + &mut Juliaup::command(), + "juliaup", + &mut io::stdout().lock(), + ); + } else { + bail!("'{}' is not a supported shell.", shell) + } + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index 6b5c2f72..92675479 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ use anyhow::Context; pub mod cli; pub mod command_add; pub mod command_api; +pub mod command_completions; pub mod command_config_backgroundselfupdate; pub mod command_config_modifypath; pub mod command_config_startupselfupdate; From de02146c4b968d654e540f7ca8ad2b7388e91fc5 Mon Sep 17 00:00:00 2001 From: Stefan Gehr Date: Sun, 14 Jan 2024 13:26:54 +0100 Subject: [PATCH 2/2] Add info about the completions command to the README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b02b8d2c..4fc6e709 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Here are some of the things you can do with `juliaup`: - `juliaup override set --path foo/bar lts` sets a directory override for the path `foo/bar` to the `lts` channel. - `juliaup override unset --path foo/bar` removes a directory override for the path `foo/bar`. - `juliaup override unset --nonexistent` removes all directory overrides for paths that no longer exist. +- `juliaup completions bash > ~/.local/share/bash-completion/completions/juliaup` generates Bash completions for `juliaup` and saves them to a file. To use them, simply source this file in your `~/.bashrc`. Other supported shells are `zsh`, `fish`, `elvish` and `powershell`. - `juliaup` shows you what other commands are available. The available system provided channels are: