From c89939b5d14e581e1aeaa940d81843192e0abc79 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Fri, 19 Nov 2021 23:34:40 +0000 Subject: Switch to clap-generated completions The current completions required a lot of domain-specific knowledge about each individual shell and their completion functionality. Much of which is sparsely documented. While clap does not generate perfect completions, since parameters like `-e` are missing completions, it does a reasonable job while requiring no work on writing these completions. Since access to `cli.rs` isn't possible from the `build.rs`, these completions aren't always generated on build. Instead a test verifies that there has been no changes to these completions and provides a simple code sample for re-generating them. This should provide a simple solution with minimal overhead. --- alacritty/src/cli.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'alacritty/src') diff --git a/alacritty/src/cli.rs b/alacritty/src/cli.rs index ce0563ff..d939aff5 100644 --- a/alacritty/src/cli.rs +++ b/alacritty/src/cli.rs @@ -246,6 +246,13 @@ pub enum SocketMessage { mod tests { use super::*; + #[cfg(target_os = "linux")] + use std::fs::File; + #[cfg(target_os = "linux")] + use std::io::Read; + + #[cfg(target_os = "linux")] + use clap::Shell; use serde_yaml::mapping::Mapping; #[test] @@ -334,4 +341,32 @@ mod tests { let class = parse_class("one,two,three"); assert!(class.is_err()); } + + #[cfg(target_os = "linux")] + #[test] + fn completions() { + let mut clap = Options::clap(); + + for (shell, file) in &[ + (Shell::Bash, "alacritty.bash"), + (Shell::Fish, "alacritty.fish"), + (Shell::Zsh, "_alacritty"), + ] { + let mut generated = Vec::new(); + clap.gen_completions_to("alacritty", *shell, &mut generated); + let generated = String::from_utf8_lossy(&generated); + + let mut completion = String::new(); + let mut file = File::open(format!("../extra/completions/{}", file)).unwrap(); + file.read_to_string(&mut completion).unwrap(); + + assert_eq!(generated, completion); + } + + // NOTE: Use this to generate new completions. + // + // clap.gen_completions("alacritty", Shell::Bash, "../extra/completions/"); + // clap.gen_completions("alacritty", Shell::Fish, "../extra/completions/"); + // clap.gen_completions("alacritty", Shell::Zsh, "../extra/completions/"); + } } -- cgit