From 4381fd505d05e06d4e4a5bd6f72aaf19bb982f57 Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Tue, 30 Jul 2024 14:01:49 -0400 Subject: [PATCH] feat: add shell completions Auto-generated with clap. --- Cargo.lock | 10 +++++++++ Cargo.toml | 8 ++++++- build.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++ docs/Compiling.md | 20 ++++++++++++++++++ nix/default.nix | 9 ++++++++ src/cli.rs | 3 +-- 6 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 build.rs diff --git a/Cargo.lock b/Cargo.lock index ec3139e..fba4d87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -476,6 +476,15 @@ dependencies = [ "strsim 0.11.0", ] +[[package]] +name = "clap_complete" +version = "4.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b4be9c4c4b1f30b78d8a750e0822b6a6102d97e62061c583a6c1dea2dfb33ae" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.18" @@ -1722,6 +1731,7 @@ dependencies = [ "cfg-if", "chrono", "clap", + "clap_complete", "color-eyre", "ctrlc", "dirs", diff --git a/Cargo.toml b/Cargo.toml index df70fa6..0f4e802 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -179,4 +179,10 @@ schemars = { version = "0.8.21", optional = true } # -- PATCH -- # temp fix for tracing-appender/time -time = "0.3.37" \ No newline at end of file +time = "0.3.37" + +[build-dependencies] +clap = { version = "4.5.9", features = ["derive"] } +clap_complete = "4.5.2" +serde = { version = "1.0.204", features = ["derive"] } +serde_json = "1.0.134" \ No newline at end of file diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..a23d3b3 --- /dev/null +++ b/build.rs @@ -0,0 +1,53 @@ +// Importing from Ironbar modules brings in lots of things not used by the build script +// we can just globally suppress those. +#![allow(unused, dead_code)] + +#[path = "src/cli.rs"] +mod cli; + +#[path = "src/error.rs"] +mod error; + +#[path = "src/ipc"] +mod ipc { + #[path = "commands.rs"] + mod commands; + + #[path = "responses.rs"] + mod responses; + + pub use commands::Command; + pub use responses::Response; +} + +use clap::Command; +use clap::CommandFactory; +use clap_complete::generate_to; +use clap_complete::Shell::{Bash, Fish, Zsh}; +use cli::Args; +use std::fs; +use std::path::PathBuf; + +const NAME: &str = "ironbar"; + +fn generate_shell_completions(mut cmd: Command) -> std::io::Result<()> { + const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR"); + let comp_dir = PathBuf::from(MANIFEST_DIR).join("target/completions"); + + fs::create_dir_all(&comp_dir)?; + + for shell in [Bash, Fish, Zsh] { + generate_to(shell, &mut cmd, NAME, &comp_dir)?; + } + + Ok(()) +} + +fn main() -> std::io::Result<()> { + let mut cmd = Args::command(); + cmd.set_bin_name(NAME); + + generate_shell_completions(cmd)?; + + Ok(()) +} diff --git a/docs/Compiling.md b/docs/Compiling.md index 278fda3..a198bf4 100644 --- a/docs/Compiling.md +++ b/docs/Compiling.md @@ -117,6 +117,26 @@ cargo build --release --no-default-features \ | **Other** | | | schema | Enables JSON schema support and the CLI `--print-schema` flag. | +## Shell completions + +Compiling Ironbar will produce shell completions for bash, zsh and fish; these can be found in `target/completions`. + +You can install these as follows: + +Bash: +```shell +install -Dm644 completions/ironbar.bash /usr/share/bash-completion/completions/ironbar +``` + +Zsh: +```shell +install -Dm644 completions/_ironbar /usr/share/zsh/site-functions/_ironbar +``` + +Fish: +```shell +install -Dm644 completions/ironbar.fish /usr/share/fish/vendor_completions.d/ironbar.fish +``` ## Speeding up compiling diff --git a/nix/default.nix b/nix/default.nix index e9c5dff..f819579 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -20,6 +20,7 @@ luajit, luajitPackages, pkg-config, + installShellFiles, adwaita-icon-theme, hicolor-icon-theme, rustPlatform, @@ -45,6 +46,7 @@ pkg-config wrapGAppsHook gobject-introspection + installShellFiles ]; buildInputs = [ @@ -89,6 +91,13 @@ ) ''; + postInstall = '' + installShellCompletion --cmd ironbar \ + --bash target/completions/ironbar.bash \ + --fish target/completions/ironbar.fish \ + --zsh target/completions/_ironbar + ''; + passthru = { updateScript = gnome.updateScript { packageName = pname; diff --git a/src/cli.rs b/src/cli.rs index a273f96..261a864 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,5 @@ use crate::error::ExitCode; -use crate::ipc::commands::Command; -use crate::ipc::responses::Response; +use crate::ipc::{Command, Response}; use clap::{Parser, ValueEnum}; use serde::{Deserialize, Serialize}; use std::process::exit;