Skip to content

Commit

Permalink
Merge pull request #1 from pants721/shell-completion
Browse files Browse the repository at this point in the history
feat: Shell completion
  • Loading branch information
pants721 authored Mar 27, 2024
2 parents f47e364 + 828f90c commit 7f8547a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ home = "^0.5.5"
clap = {version = "^4.3.21", features = [ "derive" ]}
anyhow = "^1.0.72"
colored = "^2.0.4"
clap_complete = "4.5.1"

[[bin]]
name = "sk"
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@ Skely uses the `SK_PLACEHOLDER` enviroment variable to determine a placeholder s

## Usage

### Add
Skely can add new skeletons by passing Skely a directory or file to copy. `sk add foo/` will add `foo/` as a skeleton, found at `~/.config/sk/skeletons/foo/`.

### New
To create a new project using skeleton do `sk new <PROJECT NAME>`. `sk new foo` will create project at `/foo`. Additional options can be found using `sk new --help`.

### Add
Skely can add new skeletons by passing Skely a directory or file to copy. `sk add foo/` will add `foo/` as a skeleton, found at `~/.config/sk/skeletons/foo/`.

### Remove
To remove a skeleton do `sk remove <PROJECT NAME>`.

### List
To list all configured skeletons do `sk list`.

### Completion
To generate shell completion for Skelly do `sk completion <SHELL>`. To see supported shells do `sk completion --help`. Installing completion scripts is dependent on your shell. With zsh and [oh-my-zsh](https://github.com/ohmyzsh/ohmyzsh) it would look something like this:
```zsh
mkdir ~/.oh-my-zsh/completions
sk completion zsh > ~/.oh-my-zsh/completions/_sk
omz reload
```

If you've read this far, thank you for taking interest in my software, it is much appreciated :).
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use clap_complete::{Generator, Shell};

#[derive(Parser, Debug, PartialEq)]
#[command(name = "sk")]
Expand Down Expand Up @@ -45,4 +46,9 @@ pub enum Commands {
#[arg(short, long)]
no_confirm: bool,
},
/// Generates shell completion for provided shell
Completion {
#[arg(value_enum)]
shell: Shell,
},
}
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use anyhow::{anyhow, Context, Result};
use clap::Command;
use clap::CommandFactory;
use clap_complete::generate;
use clap_complete::Generator;
use cli::{Cli, Commands};
use colored::{ColoredString, Colorize};
use std::env;
Expand All @@ -15,13 +19,22 @@ mod util;

static PLACEHOLDER_ENV_VAR: &str = "SK_PLACEHOLDER";

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}

fn main() -> Result<()> {
let args = Cli::parse();
match args.command {
Commands::Add { id, source } => add(source, id)?,
Commands::New { id, path, name } => new(id, path, name)?,
Commands::List => list()?,
Commands::Remove { id, no_confirm } => remove(id, no_confirm)?,
Commands::Completion { shell } => {
let mut cmd = Cli::command();
eprintln!("Generating completion file for {shell:?}...");
print_completions(shell, &mut cmd);
},
}

Ok(())
Expand Down

0 comments on commit 7f8547a

Please sign in to comment.