From f7aa8942e051b2f97949708802f1c90333d2512d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Fri, 25 Aug 2023 18:18:45 +0200 Subject: [PATCH] feat: add shell completions --- Cargo.lock | 42 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + flake.nix | 7 +++++++ src/cli.rs | 8 +++++++- src/main.rs | 11 +++++++++-- 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a46a4b..78b6613 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -284,6 +284,47 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "586a385f7ef2f8b4d86bddaa0c094794e7ccbfe5ffef1f434fe928143fc783a5" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_complete_command" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "183495371ea78d4c9ff638bfc6497d46fed2396e4f9c50aebc1278a4a9919a3d" +dependencies = [ + "clap", + "clap_complete", + "clap_complete_fig", + "clap_complete_nushell", +] + +[[package]] +name = "clap_complete_fig" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9bae21b3f6eb417ad3054c8b1094aa0542116eba4979b1b271baefbfa6b965" +dependencies = [ + "clap", + "clap_complete", +] + +[[package]] +name = "clap_complete_nushell" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d02bc8b1a18ee47c4d2eec3fb5ac034dc68ebea6125b1509e9ccdffcddce66e" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.3.2" @@ -1123,6 +1164,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "clap_complete_command", "humantime", "libc", "notify-rust", diff --git a/Cargo.toml b/Cargo.toml index 1209e08..8c2f0e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] anyhow = "1.0.71" clap = { version = "4.3.4", features = ["derive"] } +clap_complete_command = "0.5.1" humantime = "2.1.0" libc = "0.2.147" notify-rust = "4.8.0" diff --git a/flake.nix b/flake.nix index 1306df7..ea2459e 100644 --- a/flake.nix +++ b/flake.nix @@ -14,7 +14,14 @@ { packages.default = naersk-lib.buildPackage { src = ./.; + nativeBuildInputs = with pkgs; [ installShellFiles ]; meta.mainProgram = "timers"; + postInstall = '' + installShellCompletion --cmd timers \ + --bash <($out/bin/timers completions bash) \ + --fish <($out/bin/timers completions fish) \ + --zsh <($out/bin/timers completions zsh) \ + ''; }; devShells.default = with pkgs; mkShell { buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy rust-analyzer ]; diff --git a/src/cli.rs b/src/cli.rs index 5490a22..08daac2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,5 @@ use crate::daemon::{Answer, AnswerErr, Command as OtherCommand}; -use crate::run_path; +use crate::helper::run_path; use anyhow::{Context, Result}; use clap::{Parser, Subcommand}; use std::net::Shutdown; @@ -56,6 +56,12 @@ pub enum Command { #[command(subcommand)] #[clap(visible_alias = "p")] Pomodoro(PomodoroCommand), + + /// Shell completions + Completions { + #[arg(value_enum)] + shell: clap_complete_command::Shell, + } } #[derive(Debug, Subcommand)] diff --git a/src/main.rs b/src/main.rs index 5d6286d..2249f05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,14 +7,17 @@ mod timer; use crate::cli::{send_command, Cli, Command as CliCommand}; use crate::daemon::{Command as DaemonCommand, Daemon}; -use crate::helper::run_path; +use clap::CommandFactory; use clap::Parser; use cli::PomodoroCommand; fn main() -> Result<(), anyhow::Error> { let args = Cli::parse(); let daemon_command = match args.command { - CliCommand::Daemon { no_notify, pid_file } => { + CliCommand::Daemon { + no_notify, + pid_file, + } => { return Daemon::new(args.socket, pid_file, no_notify)?.run(); } CliCommand::Add { name, duration } => { @@ -39,6 +42,10 @@ fn main() -> Result<(), anyhow::Error> { PomodoroCommand::List => DaemonCommand::PomodoroList, PomodoroCommand::Toggle => DaemonCommand::PomodoroToggle, }, + CliCommand::Completions { shell } => { + shell.generate(&mut Cli::command(), &mut std::io::stdout()); + return Ok(()); + } }; let answer = send_command(&args.socket, daemon_command)?; print!("{}", answer);