summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorNathan West <Lucretiel@users.noreply.github.com>2020-06-03 11:25:51 -0400
committerGitHub <noreply@github.com>2020-06-03 11:25:51 -0400
commitc04a0eb1d18012530e9e43a0cad6e10118d2d6a3 (patch)
tree693f390c7c33d56805ca1816e390e04f678c6bfb /src/main.rs
parenta62712d33dc8b4373324a0d4c11a0ea48feb8921 (diff)
feat: add shell completion generation command (`starship completions`) (#881)
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index c3d1d47cb..363080360 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+use std::io;
use std::time::SystemTime;
#[macro_use]
@@ -19,7 +20,7 @@ mod segment;
mod utils;
use crate::module::ALL_MODULES;
-use clap::{App, AppSettings, Arg, SubCommand};
+use clap::{App, AppSettings, Arg, Shell, SubCommand};
fn main() {
pretty_env_logger::init();
@@ -71,7 +72,7 @@ fn main() {
.long("print-full-init")
.help("Print the main initialization script (as opposed to the init stub)");
- let matches =
+ let mut app =
App::new("starship")
.about("The cross-shell prompt for astronauts. ☄🌌️")
// pull the version number from Cargo.toml
@@ -139,7 +140,21 @@ fn main() {
.subcommand(
SubCommand::with_name("explain").about("Explains the currently showing modules"),
)
- .get_matches();
+ .subcommand(
+ SubCommand::with_name("completions")
+ .about("Generate starship shell completions for your shell to stdout")
+ .arg(
+ Arg::with_name("shell")
+ .takes_value(true)
+ .possible_values(&Shell::variants())
+ .help("the shell to generate completions for")
+ .value_name("SHELL")
+ .required(true)
+ .env("STARSHIP_SHELL"),
+ ),
+ );
+
+ let matches = app.clone().get_matches();
match matches.subcommand() {
("init", Some(sub_m)) => {
@@ -183,6 +198,15 @@ fn main() {
}
}
("explain", Some(sub_m)) => print::explain(sub_m.clone()),
- _ => {}
+ ("completions", Some(sub_m)) => {
+ let shell: Shell = sub_m
+ .value_of("shell")
+ .expect("Shell name missing.")
+ .parse()
+ .expect("Invalid shell");
+
+ app.gen_completions_to("starship", shell, &mut io::stdout().lock());
+ }
+ (command, _) => unreachable!("Invalid subcommand: {}", command),
}
}