forge
Explore
Log in

Don't you dare go Hollow

Add nushell completion support (#2009)

Browse files at this commit

746a7e81b79f8b663e0ab393ab2f192340da38e2UnverifiedArtrix and Ivan MolodetskikhParent: 51b6a49
* 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

---------
Cargo.lockmodified+11 -0
@@ -643,6 +643,16 @@ dependencies = [
643 "clap",643 "clap",
644 ]644 ]
645 645
646+[[package]]
647+name = "clap_complete_nushell"
648+version = "4.5.8"
649+source = "registry+https://github.com/rust-lang/crates.io-index"
650+checksum = "0a0c951694691e65bf9d421d597d68416c22de9632e884c28412cb8cd8b73dce"
651+dependencies = [
652+ "clap",
653+ "clap_complete",
654+]
655+
646 [[package]]656 [[package]]
647 name = "clap_derive"657 name = "clap_derive"
648 version = "4.5.41"658 version = "4.5.41"
@@ -2267,6 +2277,7 @@ dependencies = [
2267 "calloop-wayland-source 0.4.0",2277 "calloop-wayland-source 0.4.0",
2268 "clap",2278 "clap",
2269 "clap_complete",2279 "clap_complete",
2280+ "clap_complete_nushell",
2270 "directories",2281 "directories",
2271 "drm-ffi",2282 "drm-ffi",
2272 "fastrand",2283 "fastrand",
Cargo.tomlmodified+1 -0
@@ -60,6 +60,7 @@ bytemuck = { version = "1.23.1", features = ["derive"] }
60 calloop = { version = "0.14.2", features = ["executor", "futures-io"] }60 calloop = { version = "0.14.2", features = ["executor", "futures-io"] }
61 clap = { workspace = true, features = ["string"] }61 clap = { workspace = true, features = ["string"] }
62 clap_complete = "4.5.55"62 clap_complete = "4.5.55"
63+clap_complete_nushell = "4.5.8"
63 directories = "6.0.0"64 directories = "6.0.0"
64 drm-ffi = "0.9.0"65 drm-ffi = "0.9.0"
65 fastrand = "2.3.0"66 fastrand = "2.3.0"
flake.nixmodified+1 -0
@@ -122,6 +122,7 @@
122 installShellCompletion --cmd niri \122 installShellCompletion --cmd niri \
123 --bash <($out/bin/niri completions bash) \123 --bash <($out/bin/niri completions bash) \
124 --fish <($out/bin/niri completions fish) \124 --fish <($out/bin/niri completions fish) \
125+ --nushell <($out/bin/niri completions nushell) \
125 --zsh <($out/bin/niri completions zsh)126 --zsh <($out/bin/niri completions zsh)
126 127
127 install -Dm644 resources/niri.desktop -t $out/share/wayland-sessions128 install -Dm644 resources/niri.desktop -t $out/share/wayland-sessions
src/cli.rsmodified+26 -1
@@ -56,7 +56,7 @@ pub enum Sub {
56 /// Cause a panic to check if the backtraces are good.56 /// Cause a panic to check if the backtraces are good.
57 Panic,57 Panic,
58 /// Generate shell completions.58 /// Generate shell completions.
59- Completions { shell: Shell },59+ Completions { shell: CompletionShell },
60 }60 }
61 61
62 #[derive(Subcommand)]62 #[derive(Subcommand)]
@@ -108,3 +108,28 @@ pub enum Msg {
108 /// Print the overview state.108 /// Print the overview state.
109 OverviewState,109 OverviewState,
110 }110 }
111+
112+#[derive(Clone, Debug, clap::ValueEnum)]
113+pub enum CompletionShell {
114+ Bash,
115+ Elvish,
116+ Fish,
117+ PowerShell,
118+ Zsh,
119+ Nushell,
120+}
121+
122+impl TryFrom<CompletionShell> for Shell {
123+ type Error = &'static str;
124+
125+ fn try_from(shell: CompletionShell) -> Result<Self, Self::Error> {
126+ match shell {
127+ CompletionShell::Bash => Ok(Shell::Bash),
128+ CompletionShell::Elvish => Ok(Shell::Elvish),
129+ CompletionShell::Fish => Ok(Shell::Fish),
130+ CompletionShell::PowerShell => Ok(Shell::PowerShell),
131+ CompletionShell::Zsh => Ok(Shell::Zsh),
132+ CompletionShell::Nushell => Err("Nushell should be handled separately"),
133+ }
134+ }
135+}
src/main.rsmodified+22 -2
@@ -10,8 +10,10 @@ use std::process::Command;
10 use std::{env, mem};10 use std::{env, mem};
11 11
12 use clap::{CommandFactory, Parser};12 use clap::{CommandFactory, Parser};
13+use clap_complete::Shell;
14+use clap_complete_nushell::Nushell;
13 use directories::ProjectDirs;15 use directories::ProjectDirs;
14-use niri::cli::{Cli, Sub};16+use niri::cli::{Cli, CompletionShell, Sub};
15 #[cfg(feature = "dbus")]17 #[cfg(feature = "dbus")]
16 use niri::dbus;18 use niri::dbus;
17 use niri::ipc::client::handle_msg;19 use niri::ipc::client::handle_msg;
@@ -108,7 +110,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
108 }110 }
109 Sub::Panic => cause_panic(),111 Sub::Panic => cause_panic(),
110 Sub::Completions { shell } => {112 Sub::Completions { shell } => {
111- clap_complete::generate(shell, &mut Cli::command(), "niri", &mut io::stdout());113+ match shell {
114+ CompletionShell::Nushell => {
115+ clap_complete::generate(
116+ Nushell,
117+ &mut Cli::command(),
118+ "niri",
119+ &mut io::stdout(),
120+ );
121+ }
122+ other => {
123+ let generator = Shell::try_from(other).unwrap();
124+ clap_complete::generate(
125+ generator,
126+ &mut Cli::command(),
127+ "niri",
128+ &mut io::stdout(),
129+ );
130+ }
131+ }
112 return Ok(());132 return Ok(());
113 }133 }
114 }134 }