Skip to content

Commit

Permalink
Generate CLI completions (#253)
Browse files Browse the repository at this point in the history
Generate and ship CLI completions for a few common shells.
  • Loading branch information
9999years authored May 22, 2024
1 parent 38f2d6c commit 26a5dde
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ backoff = { version = "0.4.0", default-features = false }
camino = "1.1.4"
# Clap 4.4 is the last version supporting Rust 1.72.
clap = { version = "~4.4", features = ["derive", "wrap_help", "env", "string"] }
clap_complete = "~4.4"
clearscreen = "2.0.1"
command-group = { version = "2.1.0", features = ["tokio", "with-tokio"] }
crossterm = { version = "0.27.0", features = ["event-stream"] }
Expand Down
16 changes: 16 additions & 0 deletions nix/packages/ghciwatch.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
stdenv,
libiconv,
darwin,
buildPackages,
haskell,
haskellPackages,
ghc,
Expand All @@ -12,6 +13,7 @@
rustPlatform,
rust-analyzer,
mdbook,
installShellFiles,
# Versions of GHC to include in the environment for integration tests.
# These should be attributes of `haskell.compiler`.
ghcVersions ? null,
Expand Down Expand Up @@ -103,6 +105,9 @@
inherit cargoArtifacts;
};

can-run-ghciwatch = stdenv.hostPlatform.emulatorAvailable buildPackages;
run-ghciwatch = "${stdenv.hostPlatform.emulator buildPackages} $out/bin/ghciwatch";

releaseArgs =
commonArgs
// {
Expand All @@ -114,6 +119,17 @@
# Only build `ghciwatch`, not the test macros.
cargoBuildCommand = "cargoWithProfile build";

nativeBuildInputs = (commonArgs.nativeBuildInputs or []) ++ [installShellFiles];

postInstall =
(commonArgs.postInstall or "")
+ lib.optionalString can-run-ghciwatch ''
installShellCompletion --cmd ghciwatch \
--bash <(${run-ghciwatch} --completions bash) \
--fish <(${run-ghciwatch} --completions fish) \
--zsh <(${run-ghciwatch} --completions zsh)
'';

passthru = {
inherit GHC_VERSIONS checks devShell user-manual user-manual-tar-xz;
};
Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::Duration;
use camino::Utf8PathBuf;
use clap::builder::ValueParserFactory;
use clap::Parser;
use clap_complete::Shell;
use tracing_subscriber::fmt::format::FmtSpan;

use crate::clap::FmtSpanParserFactory;
Expand Down Expand Up @@ -100,6 +101,10 @@ pub struct Opts {
#[arg(long, hide = true)]
pub generate_markdown_help: bool,

/// Generate shell completions for the given shell.
#[arg(long)]
pub completions: Option<Shell>,

/// Lifecycle hooks and commands to run at various points.
#[command(flatten)]
pub hooks: crate::hooks::HookOpts,
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::time::Duration;

use clap::CommandFactory;
use clap::Parser;
use ghciwatch::cli;
use ghciwatch::run_ghci;
Expand All @@ -30,6 +31,12 @@ async fn main() -> miette::Result<()> {
return Ok(());
}

if let Some(shell) = opts.completions {
let mut command = cli::Opts::command();
clap_complete::generate(shell, &mut command, "ghciwatch", &mut std::io::stdout());
return Ok(());
}

std::env::set_var("IN_GHCIWATCH", "1");

let (ghci_sender, ghci_receiver) = mpsc::channel(32);
Expand Down

0 comments on commit 26a5dde

Please sign in to comment.