Skip to content

Commit

Permalink
add shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
bck01215 committed Jun 20, 2024
1 parent 8328a2e commit 039c811
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 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 @@ -22,3 +22,4 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
urlencoding = "2.1.3"
openssl = { version = "0.10", features = ["vendored"] }
clap_complete = "4.5.6"
24 changes: 20 additions & 4 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use crate::cli::config::get_config_dir;
use ansi_term::Colour;
use clap::{Parser, Subcommand};
use clap::{Command, CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Generator, Shell};
use dialoguer::{theme::ColorfulTheme, Select};
use std::io;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(name = "elasticnow", about = "ElasticNow time tracking CLI", version)]
pub struct Args {
// If provided, outputs the completion file for given shell
#[arg(long = "generate", value_enum)]
generator: Option<Shell>,

#[command(subcommand)]
pub cmd: Commands,
pub cmd: Option<Commands>,
}

#[derive(Subcommand, Debug, Clone)]
Expand Down Expand Up @@ -56,7 +62,13 @@ pub enum Commands {
}

pub fn get_args() -> Args {
Args::parse()
let args = Args::parse();
if let Some(shell) = args.generator {
let mut cmd = Args::command();
print_completions(shell, &mut cmd);
std::process::exit(0);
}
args
}

pub fn choose_options(mut options: Vec<String>) -> String {
Expand All @@ -77,3 +89,7 @@ pub fn write_short_description() -> String {
.interact()
.unwrap()
}

pub fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::process::exit;

use ansi_term::Colour;
use elasticnow::cli::{self, config};
use elasticnow::elasticnow::elasticnow::{ElasticNow, SearchResult};
Expand All @@ -12,25 +14,28 @@ async fn main() {
.init();
let args = cli::args::get_args();
match args.cmd {
cli::args::Commands::Timetrack {
Some(cli::args::Commands::Timetrack {
new,
comment,
time_worked,
search,
bin,
} => {
}) => {
run_timetrack(new, comment, time_worked, search, bin).await;
}
cli::args::Commands::Setup {
Some(cli::args::Commands::Setup {
id,
instance,
sn_instance,
sn_username,
sn_password,
bin,
} => {
}) => {
run_setup(id, instance, sn_instance, sn_username, sn_password, bin).await;
}
_ => {
std::process::exit(1);
}
}
}

Expand Down

0 comments on commit 039c811

Please sign in to comment.