From 746a7e81b79f8b663e0ab393ab2f192340da38e2 Mon Sep 17 00:00:00 2001 From: Artrix Date: Sun, 13 Jul 2025 23:29:26 -0700 Subject: Add nushell completion support (#2009) * Add nushell completion support Adds `clap_complete_nushell` crate and implements it into the `niri completions` command. * Add nushell to flake.nix autocompletions * Convert to `TryFrom` * Fix linting errors * Move types down --------- Co-authored-by: Ivan Molodetskikh --- src/cli.rs | 27 ++++++++++++++++++++++++++- src/main.rs | 24 ++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index cb3cc0ab..933712c2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -56,7 +56,7 @@ pub enum Sub { /// Cause a panic to check if the backtraces are good. Panic, /// Generate shell completions. - Completions { shell: Shell }, + Completions { shell: CompletionShell }, } #[derive(Subcommand)] @@ -108,3 +108,28 @@ pub enum Msg { /// Print the overview state. OverviewState, } + +#[derive(Clone, Debug, clap::ValueEnum)] +pub enum CompletionShell { + Bash, + Elvish, + Fish, + PowerShell, + Zsh, + Nushell, +} + +impl TryFrom for Shell { + type Error = &'static str; + + fn try_from(shell: CompletionShell) -> Result { + match shell { + CompletionShell::Bash => Ok(Shell::Bash), + CompletionShell::Elvish => Ok(Shell::Elvish), + CompletionShell::Fish => Ok(Shell::Fish), + CompletionShell::PowerShell => Ok(Shell::PowerShell), + CompletionShell::Zsh => Ok(Shell::Zsh), + CompletionShell::Nushell => Err("Nushell should be handled separately"), + } + } +} diff --git a/src/main.rs b/src/main.rs index 0eeb57bf..73d60bb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,8 +10,10 @@ use std::process::Command; use std::{env, mem}; use clap::{CommandFactory, Parser}; +use clap_complete::Shell; +use clap_complete_nushell::Nushell; use directories::ProjectDirs; -use niri::cli::{Cli, Sub}; +use niri::cli::{Cli, CompletionShell, Sub}; #[cfg(feature = "dbus")] use niri::dbus; use niri::ipc::client::handle_msg; @@ -108,7 +110,25 @@ fn main() -> Result<(), Box> { } Sub::Panic => cause_panic(), Sub::Completions { shell } => { - clap_complete::generate(shell, &mut Cli::command(), "niri", &mut io::stdout()); + match shell { + CompletionShell::Nushell => { + clap_complete::generate( + Nushell, + &mut Cli::command(), + "niri", + &mut io::stdout(), + ); + } + other => { + let generator = Shell::try_from(other).unwrap(); + clap_complete::generate( + generator, + &mut Cli::command(), + "niri", + &mut io::stdout(), + ); + } + } return Ok(()); } } -- cgit