Skip to content

Commit

Permalink
update clap
Browse files Browse the repository at this point in the history
  • Loading branch information
dwuggh committed Jan 22, 2022
1 parent 7f70024 commit 81d1de5
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 378 deletions.
599 changes: 286 additions & 313 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Expand Up @@ -10,8 +10,8 @@ edition = "2018"
log = "0.4"
env_logger = "0.8"
reqwest = { version = "0.11", features = ["json", "socks"] }
clap = "3.0.0-beta.4"
clap_generate = "3.0.0-beta.4"
clap = "3.0"
clap_complete = "3.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
ansi_term = "0.12"
Expand All @@ -29,8 +29,8 @@ warp = "0.3"
scraper = "0.12"

[build-dependencies]
clap = "3.0.0-beta.4"
clap_generate = "3.0.0-beta.4"
clap = "3.0"
clap_complete = "3.0"

[workspace]
members = [
Expand Down
12 changes: 6 additions & 6 deletions build.rs
@@ -1,4 +1,4 @@
use clap_generate::generate_to;
use clap_complete::generate_to;

include!("src/cli.rs");

Expand All @@ -7,9 +7,9 @@ fn main() {

app.set_bin_name("dw");
let outdir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("completions/");
generate_to::<Bash, _, _>(&mut app, "dw", &outdir);
generate_to::<Zsh, _, _>(&mut app, "dw", &outdir);
generate_to::<PowerShell, _, _>(&mut app, "dw", &outdir);
generate_to::<Elvish, _, _>(&mut app, "dw", &outdir);
generate_to::<Fish, _, _>(&mut app, "dw", &outdir);
generate_to(Shell::Bash, &mut app, "dw", &outdir).unwrap();
generate_to(Shell::Zsh, &mut app, "dw", &outdir).unwrap();
generate_to(Shell::PowerShell, &mut app, "dw", &outdir).unwrap();
generate_to(Shell::Fish, &mut app, "dw", &outdir).unwrap();
generate_to(Shell::Elvish, &mut app, "dw", &outdir).unwrap();
}
17 changes: 8 additions & 9 deletions completions/_dw
Expand Up @@ -15,13 +15,13 @@ _dw() {

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'--generate-shell-completion=[generate shell completion]: :(bash zsh powershell fish elvish)' \
'-f+[use file]' \
'--file=[use file]' \
'-o+[origin language of the querying text]' \
'--lang-origin=[origin language of the querying text]' \
'-t+[the language to be translated into]' \
'--lang-target=[the language to be translated into]' \
'--generate-shell-completion=[generate shell completion]: :(bash elvish fish powershell zsh)' \
'-f+[use file]: : ' \
'--file=[use file]: : ' \
'-o+[origin language of the querying text]: : ' \
'--lang-origin=[origin language of the querying text]: : ' \
'-t+[the language to be translated into]: : ' \
'--lang-target=[the language to be translated into]: : ' \
'--format=[response format]: :(md ansi)' \
'-h[Print help information]' \
'--help[Print help information]' \
Expand All @@ -32,7 +32,6 @@ _dw() {
'--lang-code[display all available language codes]' \
'*::INPUT -- input:' \
&& ret=0

}

(( $+functions[_dw_commands] )) ||
Expand All @@ -41,4 +40,4 @@ _dw_commands() {
_describe -t commands 'dw commands' commands "$@"
}

_dw "$@"
_dw "$@"
9 changes: 3 additions & 6 deletions completions/dw.bash
Expand Up @@ -9,26 +9,24 @@ _dw() {
for i in ${COMP_WORDS[@]}
do
case "${i}" in
dw)
"$1")
cmd="dw"
;;

*)
;;
esac
done

case "${cmd}" in
dw)
opts=" -h -V -f -o -t --help --version --generate-shell-completion --server --standalone --file --lang-origin --lang-target --lang-code --format <INPUT>... "
opts="-h -V -f -o -t --help --version --generate-shell-completion --server --standalone --file --lang-origin --lang-target --lang-code --format <INPUT>..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in

--generate-shell-completion)
COMPREPLY=($(compgen -W "bash zsh powershell fish elvish" -- "${cur}"))
COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}"))
return 0
;;
--file)
Expand Down Expand Up @@ -66,7 +64,6 @@ _dw() {
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;

esac
}

Expand Down
14 changes: 7 additions & 7 deletions completions/dw.elv
Expand Up @@ -2,21 +2,21 @@
use builtin;
use str;

edit:completion:arg-completer[dw] = [@words]{
fn spaces [n]{
set edit:completion:arg-completer[dw] = {|@words|
fn spaces {|n|
builtin:repeat $n ' ' | str:join ''
}
fn cand [text desc]{
edit:complex-candidate $text &display-suffix=' '(spaces (- 14 (wcswidth $text)))$desc
fn cand {|text desc|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
}
command = 'dw'
var command = 'dw'
for word $words[1..-1] {
if (str:has-prefix $word '-') {
break
}
command = $command';'$word
set command = $command';'$word
}
completions = [
var completions = [
&'dw'= {
cand --generate-shell-completion 'generate shell completion'
cand -f 'use file'
Expand Down
4 changes: 2 additions & 2 deletions completions/dw.fish
@@ -1,8 +1,8 @@
complete -c dw -l generate-shell-completion -d 'generate shell completion' -r -f -a "bash zsh powershell fish elvish"
complete -c dw -l generate-shell-completion -d 'generate shell completion' -r -f -a "{bash ,elvish ,fish ,powershell ,zsh }"
complete -c dw -s f -l file -d 'use file' -r
complete -c dw -s o -l lang-origin -d 'origin language of the querying text' -r
complete -c dw -s t -l lang-target -d 'the language to be translated into' -r
complete -c dw -l format -d 'response format' -r -f -a "md ansi"
complete -c dw -l format -d 'response format' -r -f -a "{md ,ansi }"
complete -c dw -s h -l help -d 'Print help information'
complete -c dw -s V -l version -d 'Print version information'
complete -c dw -l server -d 'server mode'
Expand Down
41 changes: 13 additions & 28 deletions src/cli.rs
@@ -1,6 +1,5 @@
use clap::{App, Arg};
use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh};
use clap_generate::{generate, Generator};
use clap_complete::{generate, Generator, Shell};

pub fn build_cli() -> App<'static> {
App::new("dw")
Expand All @@ -9,78 +8,64 @@ pub fn build_cli() -> App<'static> {
.about("A simple dictionary wrapper.")
.arg(
Arg::new("generate-shell-completion")
.about("generate shell completion")
.help("generate shell completion")
.long("generate-shell-completion")
.possible_values(&["bash", "zsh", "powershell", "fish", "elvish"])
.possible_values(Shell::possible_values())
.takes_value(true),
)
.arg(
Arg::new("server")
.about("server mode")
.help("server mode")
.long("server")
.takes_value(false),
)
.arg(
Arg::new("standalone")
.about("standalone client mode")
.help("standalone client mode")
.long("standalone")
.takes_value(false),
)
.arg(
Arg::new("INPUT")
.about("input")
.help("input")
.required(false)
.multiple_values(true),
)
.arg(
Arg::new("file")
.about("use file")
.help("use file")
.short('f')
.long("file")
.takes_value(true),
)
.arg(
Arg::new("lang-origin")
.about("origin language of the querying text")
.help("origin language of the querying text")
.short('o')
.long("lang-origin")
.takes_value(true),
)
.arg(
Arg::new("lang-target")
.about("the language to be translated into")
.help("the language to be translated into")
.short('t')
.long("lang-target")
.takes_value(true),
)
.arg(
Arg::new("lang-codes")
.about("display all available language codes")
.help("display all available language codes")
.long("lang-code"),
)
.arg(
Arg::new("format")
.about("response format")
.help("response format")
.long("format")
.possible_values(&["md", "ansi"])
.default_value("ansi"),
)
}

pub fn print_completions<G: Generator>(app: &mut App) {
generate::<G, _>(app, app.get_name().to_string(), &mut std::io::stdout());
}

pub fn build_completion(shell: &str) {
let mut app = build_cli();
match shell {
"bash" => print_completions::<Bash>(&mut app),
"elvish" => print_completions::<Elvish>(&mut app),
"fish" => print_completions::<Fish>(&mut app),
"powershell" => print_completions::<PowerShell>(&mut app),
"zsh" => print_completions::<Zsh>(&mut app),
_ => {
eprintln!("unknown generator: {}", shell);
}
}
pub fn print_completions<G: Generator>(gen: G, app: &mut App) {
generate(gen, app, app.get_name().to_string(), &mut std::io::stdout());
}
7 changes: 4 additions & 3 deletions src/main.rs
Expand Up @@ -9,7 +9,7 @@ pub mod types;
pub use types::*;
pub mod server;

use cli::{build_cli, build_completion};
use cli::{build_cli, print_completions};
use formatter::Format;
use history::History;
use std::fs::File;
Expand All @@ -27,8 +27,9 @@ async fn main() -> std::io::Result<()> {
log::debug!("get clap matches: {:?}", matches);

// info section
if let Some(shell) = matches.value_of("generate-shell-completion") {
build_completion(shell);
if let Ok(shell) = matches.value_of_t::<clap_complete::Shell>("generate-shell-completion") {
let mut app = build_cli();
print_completions(shell, &mut app);
return Ok(());
}

Expand Down

0 comments on commit 81d1de5

Please sign in to comment.