Skip to content

Commit

Permalink
Add support for shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNerma committed Sep 20, 2024
1 parent bd7fd02 commit 5ca0b20
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
28 changes: 19 additions & 9 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jumpy"
version = "0.4.3"
version = "0.4.4"
edition = "2021"
authors = ["Clément Nerma <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -10,5 +10,6 @@ repository = "https://github.com/ClementNerma/Jumpy"

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
clap_complete = "4.5.28"
dirs = "5.0.1"
dunce = "1.0.4"
30 changes: 24 additions & 6 deletions src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::path::PathBuf;

use clap::{Args, Parser, Subcommand};
use clap::{Args, Parser, Subcommand, ValueEnum, ValueHint};

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
pub struct Command {
#[clap(short, long)]
#[clap(short, long, value_hint = ValueHint::FilePath)]
pub index_file: Option<PathBuf>,

#[clap(subcommand)]
Expand Down Expand Up @@ -46,17 +46,20 @@ pub enum Action {

#[clap(about = "Get the path of the index file")]
Path(Path),

#[clap(about = "Generate completions for a given shell")]
Completions(Completions),
}

#[derive(Args)]
pub struct Add {
#[clap()]
#[clap(value_hint = ValueHint::DirPath)]
pub path: String,
}

#[derive(Args)]
pub struct Inc {
#[clap()]
#[clap(value_hint = ValueHint::DirPath)]
pub path: String,

#[clap(long, help = "Give the maximum score to this directory")]
Expand All @@ -68,7 +71,7 @@ pub struct Query {
#[clap()]
pub query: String,

#[clap(short, long)]
#[clap(short, long, value_hint = ValueHint::DirPath)]
pub after: Option<String>,

#[clap(short, long)]
Expand All @@ -86,7 +89,7 @@ pub struct List {

#[derive(Args)]
pub struct Del {
#[clap()]
#[clap(value_hint = ValueHint::DirPath)]
pub path: String,
}

Expand All @@ -108,3 +111,18 @@ pub struct Path {
)]
pub lossily: bool,
}

#[derive(Args)]
pub struct Completions {
#[clap(help = "Shell to generate completions for")]
pub for_shell: CompletionShellName,
}

#[derive(Clone, Copy, ValueEnum)]
pub enum CompletionShellName {
Bash,
Zsh,
Fish,
Elvish,
PowerShell,
}
29 changes: 23 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
mod cmd;
mod index;

use clap::Parser;
use std::{fs, io::stdout};

use crate::index::IndexEntry;
use clap::{CommandFactory, Parser};

use crate::{
cmd::*,
index::{Index, IndexEntry},
};

static INDEX_FILENAME: &str = "jumpy.db";

Expand All @@ -19,10 +24,6 @@ fn fail(message: &str) -> ! {
}

fn main() {
use cmd::*;
use index::Index;
use std::fs;

let cmd = Command::parse();

let index_file = cmd.index_file.unwrap_or_else(|| {
Expand Down Expand Up @@ -128,6 +129,22 @@ fn main() {
}
}
},

Action::Completions(Completions { for_shell }) => {
use clap_complete::*;

let shell = match for_shell {
CompletionShellName::Bash => Shell::Bash,
CompletionShellName::Zsh => Shell::Zsh,
CompletionShellName::Fish => Shell::Fish,
CompletionShellName::Elvish => Shell::Elvish,
CompletionShellName::PowerShell => Shell::PowerShell,
};

let cmd = &mut Command::command();

aot::generate(shell, cmd, cmd.get_name().to_string(), &mut stdout());
}
}

let updated = index.encode();
Expand Down

0 comments on commit 5ca0b20

Please sign in to comment.