Skip to content

Commit

Permalink
Add static shell completion
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPickering committed Sep 30, 2024
1 parent e2ea429 commit 1a0009b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
44 changes: 36 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ version = "2.1.0"
[dependencies]
anyhow = {workspace = true}
clap = {version = "4.4.2", features = ["derive"]}
clap_complete = { version = "4.5.29", features = ["unstable-dynamic"] }
dialoguer = {version = "0.11.0", default-features = false, features = ["password"]}
indexmap = {workspace = true}
itertools = {workspace = true}
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod collections;
pub mod complete;
pub mod generate;
pub mod history;
pub mod import;
Expand Down
34 changes: 34 additions & 0 deletions crates/cli/src/commands/complete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{Args, GlobalArgs, Subcommand};
use anyhow::anyhow;
use clap::{CommandFactory, Parser};
use clap_complete::Shell;
use std::{io, process::ExitCode};

const COMMAND_NAME: &str = "slumber";

/// Generate shell completions
#[derive(Clone, Debug, Parser)]
pub struct CompleteCommand {
/// Shell type. Default to $SHELL
#[clap(long)]
shell: Option<Shell>,
}

impl Subcommand for CompleteCommand {
async fn execute(self, _: GlobalArgs) -> anyhow::Result<ExitCode> {
let shell = self
.shell
.or_else(Shell::from_env)
.ok_or_else(|| anyhow!("No shell provided and none detected"))?;
let mut command = Args::command();

clap_complete::generate(
shell,
&mut command,
COMMAND_NAME,
&mut io::stdout(),
);

Ok(ExitCode::SUCCESS)
}
}
8 changes: 5 additions & 3 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ mod commands;
mod util;

use crate::commands::{
collections::CollectionsCommand, generate::GenerateCommand,
history::HistoryCommand, import::ImportCommand, new::NewCommand,
request::RequestCommand, show::ShowCommand,
collections::CollectionsCommand, complete::CompleteCommand,
generate::GenerateCommand, history::HistoryCommand, import::ImportCommand,
new::NewCommand, request::RequestCommand, show::ShowCommand,
};
use clap::Parser;
use std::{path::PathBuf, process::ExitCode};
Expand Down Expand Up @@ -54,6 +54,7 @@ pub struct GlobalArgs {
#[derive(Clone, Debug, clap::Subcommand)]
pub enum CliCommand {
Collections(CollectionsCommand),
Complete(CompleteCommand),
Generate(GenerateCommand),
History(HistoryCommand),
Import(ImportCommand),
Expand All @@ -67,6 +68,7 @@ impl CliCommand {
pub async fn execute(self, global: GlobalArgs) -> anyhow::Result<ExitCode> {
match self {
Self::Collections(command) => command.execute(global).await,
Self::Complete(command) => command.execute(global).await,
Self::Generate(command) => command.execute(global).await,
Self::History(command) => command.execute(global).await,
Self::Import(command) => command.execute(global).await,
Expand Down

0 comments on commit 1a0009b

Please sign in to comment.