From dd23346bda46629788b08aba2e8af3b3f13335af Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 9 Jan 2026 05:16:12 +0900 Subject: [PATCH] nix: add clap completions --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + nix/package.nix | 16 ++++++++++++++++ src/bin/ruptime.rs | 18 +++++++++++++++++- src/bin/rwho.rs | 13 ++++++++++++- 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eed4ccf..4a30d6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -201,6 +201,15 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.5.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "430b4dc2b5e3861848de79627b2bedc9f3342c7da5173a14eaa5d0f8dc18ae5d" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.5.49" @@ -815,6 +824,7 @@ dependencies = [ "bytes", "chrono", "clap", + "clap_complete", "futures-util", "nix", "sd-notify", diff --git a/Cargo.toml b/Cargo.toml index 00eed5f..29e8df7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ sd-notify = "0.4.5" serde_json = "1.0.148" uucore = { version = "0.5.0", features = ["utmpx"] } zlink = { version = "0.2.0", features = ["introspection"] } +clap_complete = "4.5.65" [lib] name = "roowho2_lib" diff --git a/nix/package.nix b/nix/package.nix index 4e9053e..3bf0736 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -2,6 +2,7 @@ lib , rustPlatform , stdenv +, buildPackages , installShellFiles , versionCheckHook @@ -20,6 +21,21 @@ rustPlatform.buildRustPackage { RUSTFLAGS = "-Zhigher-ranked-assumptions"; + nativeBuildInputs = [ installShellFiles ]; + postInstall = let + emulator = stdenv.hostPlatform.emulator buildPackages; + installShellCompletions = lib.mapCartesianProduct ({ shell, command }: '' + ( + export PATH="$out/bin:$PATH" + "${emulator}" "${command}" --completions=${shell} > "$TMP/${command}.${shell}" + ) + installShellCompletion "--${shell}" --cmd "${command}" "$TMP/${command}.${shell}" + '') { + shell = [ "bash" "zsh" "fish" ]; + command = [ "rwho" "ruptime" ]; + }; + in lib.concatStringsSep "\n" installShellCompletions; + meta = with lib; { license = licenses.mit; platforms = platforms.linux ++ platforms.darwin; diff --git a/src/bin/ruptime.rs b/src/bin/ruptime.rs index 90e1cca..19c5ead 100644 --- a/src/bin/ruptime.rs +++ b/src/bin/ruptime.rs @@ -1,6 +1,7 @@ use anyhow::Context; use chrono::{Duration, Utc}; -use clap::Parser; +use clap::{CommandFactory, Parser}; +use clap_complete::{Shell, generate}; use roowho2_lib::{proto::WhodStatusUpdate, server::varlink_api::RwhodClientProxy}; @@ -44,12 +45,27 @@ pub struct Args { /// Output in JSON format #[arg(long, short)] json: bool, + + /// Generate shell completion scripts for the specified shell + /// and print them to stdout. + #[arg(long, value_enum, hide = true)] + completions: Option, } #[tokio::main] async fn main() -> anyhow::Result<()> { let args = Args::parse(); + if let Some(shell) = args.completions { + generate( + shell, + &mut Args::command(), + "ruptime", + &mut std::io::stdout(), + ); + return Ok(()); + } + let mut conn = zlink::unix::connect("/run/roowho2/roowho2.varlink") .await .expect("Failed to connect to rwhod server"); diff --git a/src/bin/rwho.rs b/src/bin/rwho.rs index 1f01cc9..87667c1 100644 --- a/src/bin/rwho.rs +++ b/src/bin/rwho.rs @@ -1,5 +1,6 @@ use anyhow::Context; -use clap::Parser; +use clap::{CommandFactory, Parser}; +use clap_complete::{Shell, generate}; use roowho2_lib::{proto::WhodUserEntry, server::varlink_api::RwhodClientProxy}; /// Check who is logged in on local machines. @@ -29,12 +30,22 @@ pub struct Args { /// Output in JSON format #[arg(long, short)] json: bool, + + /// Generate shell completion scripts for the specified shell + /// and print them to stdout. + #[arg(long, value_enum, hide = true)] + completions: Option, } #[tokio::main] async fn main() -> anyhow::Result<()> { let args = Args::parse(); + if let Some(shell) = args.completions { + generate(shell, &mut Args::command(), "rwho", &mut std::io::stdout()); + return Ok(()); + } + let mut conn = zlink::unix::connect("/run/roowho2/roowho2.varlink") .await .expect("Failed to connect to rwhod server");