Skip to content

Adding Shell Completions #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.lock

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

18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ categories = ["development-tools", "command-line-utilities"]
[dependencies]
anyhow = "1.0.89"
clap = { version = "4.5.20", features = ["derive"] }
clap_complete = "^4.5"
copypasta = "0.8.2"
dirs = "5.0.1"
human-panic = "2.0.2"
Expand All @@ -25,7 +26,10 @@ ratatui = "0.28.1"
serde = { version = "1.0.210", features = ["default", "derive"] }
serde_json = "1.0.128"
tui-input = "0.10.1"
ureq = { version = "2.10.1", features = ["gzip", "native-tls"], default-features = false }
ureq = { version = "2.10.1", features = [
"gzip",
"native-tls",
], default-features = false }
url = "2.5.2"
yansi = "1.0.1"

Expand Down Expand Up @@ -53,16 +57,16 @@ pkg-url = "{ repo }/releases/download/v{ version }/{ name }-macos-arm64"

[package.metadata.cross.target.x86_64-unknown-linux-gnu]
pre-build = [
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH"
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH",
]
[package.metadata.cross.target.i686-unknown-linux-gnu]
pre-build = [
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH"
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH",
]
[package.metadata.cross.target.aarch64-unknown-linux-gnu]
pre-build = [
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH"
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes install pkg-config libssl-dev:$CROSS_DEB_ARCH",
]
11 changes: 10 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::template::item::Template;
use crate::template::list::TemplateList;
use anyhow::{bail, Context, Result};
use clap::{Args, Parser, Subcommand};
use clap_complete::Shell;
use once_cell::sync::Lazy;

const LONG_ABOUT: &str = r"
Expand All @@ -18,7 +19,7 @@ You can also browse the available templates at the GitHub &
TopTal collections using the `search` command.";

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = LONG_ABOUT)]
#[command(author, version, about, long_about = LONG_ABOUT, arg_required_else_help = true)]
pub struct Cli {
/// Refresh the cache (templates are cached for 1h)
#[arg(short = 'r', long = "refresh", global = true)]
Expand Down Expand Up @@ -66,6 +67,11 @@ pub enum Commands {
Create(CommandCreate),
/// Choose templates interactively from the GitHub & TopTal collections
Search,
/// Generate completions to stdout
Completions {
/// Specify desired shell
shell: Shell,
},
}

impl Cli {
Expand All @@ -76,6 +82,9 @@ impl Cli {
Some(Commands::Search) => {
bail!("Cannot provide template arguments to 'search' command")
}
Some(Commands::Completions { shell: _ }) => {
bail!("Cannot provide template arguments to 'completions' command")
}
None => bail!("Cannot provide template arguments to an unknown command"),
};

Expand Down
15 changes: 15 additions & 0 deletions src/commands/completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::io;

use anyhow::Result;
use clap::CommandFactory;
use clap_complete::{generate, Shell};

use crate::cli::Cli;

pub fn command(shell: &Shell) -> Result<()> {
let cmd = &mut Cli::command();

generate(*shell, cmd, cmd.get_name().to_string(), &mut io::stdout());

Ok(())
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod completions;
pub mod create;
pub mod search;
2 changes: 1 addition & 1 deletion src/commands/search/framework/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// From: https://github.com/ratatui-org/rust-tui-template

///
/// Terminal UI events handler
pub mod event;

Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod tests;
mod util;

use crate::cli::{get_cli, Commands};
use crate::commands::completions;
use crate::commands::create;
use crate::commands::search;
use anyhow::{anyhow, Result};
Expand All @@ -20,6 +21,7 @@ fn main() -> Result<()> {
let result = match &get_cli().command {
Some(Commands::Create(cmd)) => create::command(cmd),
Some(Commands::Search) => search::command(),
Some(Commands::Completions { shell }) => completions::command(shell),
None => Err(anyhow!("No command specified")),
};

Expand Down
4 changes: 2 additions & 2 deletions src/template/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl Template {
}
_ => {
let url = self.value.url()?;
return match TemplateCache::get(&url)? {
match TemplateCache::get(&url)? {
Some(content) => Ok(content),
None => {
let content: String = http().get(&url)
Expand All @@ -239,7 +239,7 @@ impl Template {
TemplateCache::set(&url, content)?;
Ok(content.to_string())
}
};
}
}
}
}
Expand Down