Skip to content

Commit

Permalink
feat: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Sep 22, 2022
1 parent 3e87d0b commit 4fb10dc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
47 changes: 47 additions & 0 deletions clap_complete/src/dynamic/fish.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Complete commands within fish

// For fish the behaviour should match the default as close as possible
// 1. grouping shorthand completions i.e. -a<TAB> should show other shorthands to end up with
// -ams...
// 2. only complete options when one - is typed
// Due to https://github.com/fish-shell/fish-shell/issues/7943 we need to implement this our self
//

use std::ffi::OsString;

use clap::Args;

use super::Completer;

/// Dynamic completion for Fish
pub struct Fish;

impl Completer for Fish {
type CompleteArgs = CompleteArgs;

fn completion_script(cmd: &mut clap::Command) -> Vec<u8> {
let name = cmd.get_bin_name().unwrap_or_else(|| cmd.get_name());
format!(
r#"complete -x -c {name} -a "{name} complete --commandline (commandline) --cursor (commandline -C)""#
).into_bytes()
}

fn file_name(name: &str) -> String {
format!("{name}.fish")
}

fn try_complete(args: &Self::CompleteArgs, cmd: &mut clap::Command) -> clap::error::Result<()> {
todo!()
}
}

#[derive(Args, Clone, Debug)]
/// Arguments for Fish Completion
pub struct CompleteArgs {
/// current commandline
#[arg(long)]
commandline: OsString,
/// cursor position
#[arg(long)]
cursor: usize,
}
10 changes: 9 additions & 1 deletion clap_complete/src/dynamic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//! Complete commands within shells

pub mod bash;
pub mod fish;

use std::{io::Write, path::PathBuf};

use clap::{Args, Command, Subcommand, ValueEnum};

use crate::dynamic::bash::Bash;
use bash::Bash;
use fish::Fish;

#[derive(Subcommand, Clone, Debug)]
#[command(hide = true)]
Expand All @@ -23,6 +25,7 @@ pub enum CompleteCommand {
/// Subcommand for all the shells, so each can have their own options
pub enum CompleteShell {
Bash(bash::CompleteArgs),
Fish(fish::CompleteArgs),
/// Only exception is Register, which outputs the completion script for a shell
Register(RegisterArgs),
}
Expand All @@ -44,19 +47,23 @@ pub struct RegisterArgs {
pub enum Shell {
/// Bourne Again SHell (bash)
Bash,
/// Friendly Interactive SHell (fish)
Fish,
}

impl Shell {
/// Return completion script
fn completion_script(&self, cmd: &mut Command) -> Vec<u8> {
match self {
Shell::Bash => Bash::completion_script(cmd),
Shell::Fish => Fish::completion_script(cmd),
}
}
/// The recommended file name for the registration code
fn file_name(&self, name: &str) -> String {
match self {
Shell::Bash => Bash::file_name(name),
Shell::Fish => Fish::file_name(name),
}
}
}
Expand Down Expand Up @@ -88,6 +95,7 @@ impl CompleteCommand {
let CompleteCommand::Complete(complete) = self;
match complete {
CompleteShell::Bash(args) => Bash::try_complete(args, cmd),
CompleteShell::Fish(args) => Fish::try_complete(args, cmd),
CompleteShell::Register(RegisterArgs { path, shell }) => {
let script = shell.completion_script(cmd);
if path == std::path::Path::new("-") {
Expand Down

0 comments on commit 4fb10dc

Please sign in to comment.