Skip to content

Commit

Permalink
Add --completion option
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Mar 16, 2018
1 parent e3495fb commit b6b85e7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export LONG_VERSION
all: test

test:
cargo test
cargo test -- --test-threads=1

watch:
cargo watch "test -- --test-threads=1"

clean:
cargo clean
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The tested version is below.
## Usage

```
ptags 0.1.6-pre
ptags 0.1.9-pre
[email protected]
A parallel universal-ctags wrapper for git repository
Expand All @@ -58,11 +58,13 @@ FLAGS:
OPTIONS:
--bin-ctags <bin_ctags> Path to ctags binary [default: ctags]
--bin-git <bin_git> Path to git binary [default: git]
--completion <completion> Generate shell completion file [possible values: bash, fish,
zsh, powershell]
-e, --exclude <exclude>... Glob pattern of exclude file ( ex. --exclude '*.rs' )
-c, --opt-ctags <opt_ctags>... Options passed to ctags
-g, --opt-git <opt_git>... Options passed to git
--opt-git-lfs <opt_git_lfs>... Options passed to git-lfs
-f, --file <output> Output filename [default: tags]
-f, --file <output> Output filename ( filename '-' means output to stdout ) [default: tags]
-t, --thread <thread> Number of threads [default: 8]
ARGS:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ build: false
test_script:
- git submodule init
- git submodule update
- cargo test
- cargo test -- -test-threads=1
50 changes: 50 additions & 0 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ pub struct Opt {
/// Glob pattern of exclude file ( ex. --exclude '*.rs' )
#[structopt(short = "e", long = "exclude", raw(number_of_values = "1"))]
pub exclude: Vec<String>,

/// Generate shell completion file
#[structopt(long = "completion",
raw(possible_values = "&[\"bash\", \"fish\", \"zsh\", \"powershell\"]"))]
pub completion: Option<String>,
}

// ---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -185,6 +190,21 @@ fn write_tags(opt: &Opt, outputs: &[Output]) -> Result<()> {
// ---------------------------------------------------------------------------------------------------------------------

pub fn run_opt(opt: &Opt) -> Result<()> {
match opt.completion {
Some(ref x) => {
let shell = match x.as_str() {
"bash" => clap::Shell::Bash,
"fish" => clap::Shell::Fish,
"zsh" => clap::Shell::Zsh,
"powershell" => clap::Shell::PowerShell,
_ => clap::Shell::Bash,
};
Opt::clap().gen_completions("ptags", shell, "./");
return Ok(());
}
None => {}
}

let files;
let time_git_files = watch_time!({
files = git_files(&opt)?;
Expand Down Expand Up @@ -230,6 +250,7 @@ pub fn run() -> Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;

#[test]
fn test_run() {
Expand Down Expand Up @@ -257,4 +278,33 @@ mod tests {
"Err(Error(Git(CommandFailed(\"aaa\", Error { repr: Os { code: 2, message: "
);
}

#[test]
fn test_run_completion() {
let args = vec!["ptags", "--completion", "bash"];
let opt = Opt::from_iter(args.iter());
let ret = run_opt(&opt);
assert!(ret.is_ok());
let args = vec!["ptags", "--completion", "fish"];
let opt = Opt::from_iter(args.iter());
let ret = run_opt(&opt);
assert!(ret.is_ok());
let args = vec!["ptags", "--completion", "zsh"];
let opt = Opt::from_iter(args.iter());
let ret = run_opt(&opt);
assert!(ret.is_ok());
let args = vec!["ptags", "--completion", "powershell"];
let opt = Opt::from_iter(args.iter());
let ret = run_opt(&opt);
assert!(ret.is_ok());

assert!(Path::new("ptags.bash").exists());
assert!(Path::new("ptags.fish").exists());
assert!(Path::new("_ptags").exists());
assert!(Path::new("_ptags.ps1").exists());
let _ = fs::remove_file("ptags.bash");
let _ = fs::remove_file("ptags.fish");
let _ = fs::remove_file("_ptags");
let _ = fs::remove_file("_ptags.ps1");
}
}

0 comments on commit b6b85e7

Please sign in to comment.