-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial implementation of completion for fish
- Loading branch information
1 parent
0d975e3
commit 80979f0
Showing
11 changed files
with
250 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "uutils-args-complete" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Terts Diepraam"] | ||
license = "MIT" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
use crate::{Command, ValueHint}; | ||
|
||
pub fn render(c: &Command) -> String { | ||
let mut out = String::new(); | ||
let name = &c.name; | ||
for arg in &c.args { | ||
let mut line = format!("complete -c {name}"); | ||
for short in &arg.short { | ||
line.push_str(&format!(" -s {short}")); | ||
} | ||
for long in &arg.long { | ||
line.push_str(&format!(" -l {long}")); | ||
} | ||
line.push_str(&format!(" -d '{}'", arg.help)); | ||
if let Some(value) = &arg.value { | ||
line.push_str(&render_value_hint(value)); | ||
} | ||
out.push_str(&line); | ||
out.push('\n'); | ||
} | ||
out | ||
} | ||
|
||
fn render_value_hint(value: &ValueHint) -> String { | ||
match value { | ||
ValueHint::Strings(s) => { | ||
let joined = s.join(", "); | ||
format!(" -a {{ {joined} }}") | ||
} | ||
_ => todo!(), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::render; | ||
use crate::{Arg, Command}; | ||
|
||
#[test] | ||
fn short() { | ||
let c = Command { | ||
name: "test".into(), | ||
args: vec![Arg { | ||
short: vec!["a".into()], | ||
help: "some flag".into(), | ||
..Arg::default() | ||
}], | ||
}; | ||
assert_eq!(render(&c), "complete -c test -s a -d 'some flag'\n",) | ||
} | ||
|
||
#[test] | ||
fn long() { | ||
let c = Command { | ||
name: "test".into(), | ||
args: vec![Arg { | ||
long: vec!["all".into()], | ||
help: "some flag".into(), | ||
..Arg::default() | ||
}], | ||
}; | ||
assert_eq!(render(&c), "complete -c test -l all -d 'some flag'\n",) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
mod fish; | ||
|
||
pub struct Command { | ||
pub name: String, | ||
pub args: Vec<Arg>, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct Arg { | ||
pub short: Vec<String>, | ||
pub long: Vec<String>, | ||
pub help: String, | ||
pub value: Option<ValueHint>, | ||
} | ||
|
||
pub enum ValueHint { | ||
Strings(Vec<String>), | ||
Unknown, | ||
// Other, | ||
AnyPath, | ||
// FilePath, | ||
// DirPath, | ||
// ExecutablePath, | ||
// CommandName, | ||
// CommandString, | ||
// CommandWithArguments, | ||
// Username, | ||
// Hostname, | ||
} | ||
|
||
pub fn render(c: &Command, shell: &str) -> String { | ||
match shell { | ||
"fish" => fish::render(c), | ||
"sh" | "zsh" | "bash" | "csh" => panic!("shell '{shell}' completion is not supported yet!"), | ||
_ => panic!("unknown shell '{shell}'!"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
use crate::{ | ||
argument::{ArgType, Argument}, | ||
flags::{Flags, Flag}, | ||
}; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub fn complete(args: &[Argument]) -> TokenStream { | ||
let mut arg_specs = Vec::new(); | ||
|
||
for Argument { help, arg_type, .. } in args { | ||
let ArgType::Option { | ||
flags, | ||
hidden: false, | ||
.. | ||
} = arg_type | ||
else { | ||
continue; | ||
}; | ||
|
||
let Flags { short, long, .. } = flags; | ||
if short.is_empty() && long.is_empty() { | ||
continue; | ||
} | ||
let short: Vec<_> = short.iter().map(|Flag { flag, .. }| quote!(String::from(#flag))).collect(); | ||
let long: Vec<_> = long.iter().map(|Flag { flag, .. }| quote!(String::from(#flag))).collect(); | ||
|
||
arg_specs.push(quote!( | ||
Arg { | ||
short: vec![#(#short),*], | ||
long: vec![#(#long),*], | ||
help: String::from(#help), | ||
value: None, | ||
} | ||
)) | ||
} | ||
|
||
quote!(Command { | ||
name: String::from(option_env!("CARGO_BIN_NAME").unwrap_or(env!("CARGO_PKG_NAME"))), | ||
args: vec![#(#arg_specs),*] | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.