2020-08-07 19:13:12 +00:00
|
|
|
use clap::{crate_authors, crate_version};
|
2020-06-03 15:25:51 +00:00
|
|
|
use std::io;
|
2019-12-19 22:38:06 +00:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2020-06-03 15:25:51 +00:00
|
|
|
use clap::{App, AppSettings, Arg, Shell, SubCommand};
|
2020-09-28 20:38:50 +00:00
|
|
|
use rand::distributions::Alphanumeric;
|
|
|
|
use rand::Rng;
|
2020-10-17 09:09:27 +00:00
|
|
|
use starship::module::ALL_MODULES;
|
|
|
|
use starship::*;
|
2019-04-02 04:45:49 +00:00
|
|
|
|
2019-04-02 03:23:03 +00:00
|
|
|
fn main() {
|
2020-09-28 20:38:50 +00:00
|
|
|
logger::init();
|
2019-05-14 04:43:11 +00:00
|
|
|
|
2019-07-02 20:12:53 +00:00
|
|
|
let status_code_arg = Arg::with_name("status_code")
|
|
|
|
.short("s")
|
|
|
|
.long("status")
|
|
|
|
.value_name("STATUS_CODE")
|
|
|
|
.help("The status code of the previously run command")
|
|
|
|
.takes_value(true);
|
|
|
|
|
|
|
|
let path_arg = Arg::with_name("path")
|
|
|
|
.short("p")
|
|
|
|
.long("path")
|
|
|
|
.value_name("PATH")
|
|
|
|
.help("The path that the prompt should render for")
|
|
|
|
.takes_value(true);
|
|
|
|
|
2019-07-03 12:03:02 +00:00
|
|
|
let shell_arg = Arg::with_name("shell")
|
|
|
|
.value_name("SHELL")
|
|
|
|
.help(
|
2019-12-11 20:31:30 +00:00
|
|
|
"The name of the currently running shell\nCurrently supported options: bash, zsh, fish, powershell, ion",
|
2019-07-03 12:03:02 +00:00
|
|
|
)
|
|
|
|
.required(true);
|
|
|
|
|
2019-08-08 17:25:30 +00:00
|
|
|
let cmd_duration_arg = Arg::with_name("cmd_duration")
|
|
|
|
.short("d")
|
|
|
|
.long("cmd-duration")
|
|
|
|
.value_name("CMD_DURATION")
|
2019-12-19 22:38:06 +00:00
|
|
|
.help("The execution duration of the last command, in milliseconds")
|
2019-08-08 17:25:30 +00:00
|
|
|
.takes_value(true);
|
|
|
|
|
2019-08-17 19:33:19 +00:00
|
|
|
let keymap_arg = Arg::with_name("keymap")
|
|
|
|
.short("k")
|
|
|
|
.long("keymap")
|
|
|
|
.value_name("KEYMAP")
|
2019-08-31 07:59:18 +00:00
|
|
|
// fish/zsh only
|
|
|
|
.help("The keymap of fish/zsh")
|
2019-08-17 19:33:19 +00:00
|
|
|
.takes_value(true);
|
|
|
|
|
2019-08-12 17:42:33 +00:00
|
|
|
let jobs_arg = Arg::with_name("jobs")
|
|
|
|
.short("j")
|
|
|
|
.long("jobs")
|
|
|
|
.value_name("JOBS")
|
|
|
|
.help("The number of currently running jobs")
|
|
|
|
.takes_value(true);
|
|
|
|
|
2019-08-20 01:44:53 +00:00
|
|
|
let init_scripts_arg = Arg::with_name("print_full_init")
|
|
|
|
.long("print-full-init")
|
|
|
|
.help("Print the main initialization script (as opposed to the init stub)");
|
|
|
|
|
2020-09-21 17:06:15 +00:00
|
|
|
let mut app = App::new("starship")
|
|
|
|
.about("The cross-shell prompt for astronauts. ☄🌌️")
|
|
|
|
// pull the version number from Cargo.toml
|
|
|
|
.version(crate_version!())
|
|
|
|
// pull the authors from Cargo.toml
|
|
|
|
.author(crate_authors!())
|
|
|
|
.after_help("https://github.com/starship/starship")
|
|
|
|
.setting(AppSettings::SubcommandRequiredElseHelp)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("init")
|
|
|
|
.about("Prints the shell function used to execute starship")
|
|
|
|
.arg(&shell_arg)
|
|
|
|
.arg(&init_scripts_arg),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("prompt")
|
|
|
|
.about("Prints the full starship prompt")
|
|
|
|
.arg(&status_code_arg)
|
|
|
|
.arg(&path_arg)
|
|
|
|
.arg(&cmd_duration_arg)
|
|
|
|
.arg(&keymap_arg)
|
|
|
|
.arg(&jobs_arg),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("module")
|
|
|
|
.about("Prints a specific prompt module")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("name")
|
|
|
|
.help("The name of the module to be printed")
|
|
|
|
.required(true)
|
|
|
|
.required_unless("list"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("list")
|
|
|
|
.short("l")
|
|
|
|
.long("list")
|
|
|
|
.help("List out all supported modules"),
|
|
|
|
)
|
|
|
|
.arg(&status_code_arg)
|
|
|
|
.arg(&path_arg)
|
|
|
|
.arg(&cmd_duration_arg)
|
|
|
|
.arg(&keymap_arg)
|
|
|
|
.arg(&jobs_arg),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("config")
|
|
|
|
.alias("configure")
|
|
|
|
.about("Edit the starship configuration")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("name")
|
|
|
|
.help("Configuration key to edit")
|
|
|
|
.required(false)
|
|
|
|
.requires("value"),
|
|
|
|
)
|
|
|
|
.arg(Arg::with_name("value").help("Value to place into that key")),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("bug-report").about(
|
2019-12-14 23:40:12 +00:00
|
|
|
"Create a pre-populated GitHub issue with information about your configuration",
|
2020-09-21 17:06:15 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("time")
|
|
|
|
.about("Prints time in milliseconds")
|
|
|
|
.settings(&[AppSettings::Hidden]),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("explain").about("Explains the currently showing modules"),
|
|
|
|
)
|
|
|
|
.subcommand(SubCommand::with_name("timings").about("Prints timings of all active modules"))
|
|
|
|
.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"),
|
|
|
|
),
|
2020-09-28 20:38:50 +00:00
|
|
|
)
|
|
|
|
.subcommand(SubCommand::with_name("session").about("Generate random session key"));
|
2020-06-03 15:25:51 +00:00
|
|
|
|
|
|
|
let matches = app.clone().get_matches();
|
2019-04-02 04:45:49 +00:00
|
|
|
|
2019-06-06 12:18:00 +00:00
|
|
|
match matches.subcommand() {
|
2019-07-03 12:03:02 +00:00
|
|
|
("init", Some(sub_m)) => {
|
|
|
|
let shell_name = sub_m.value_of("shell").expect("Shell name missing.");
|
2019-08-20 01:44:53 +00:00
|
|
|
if sub_m.is_present("print_full_init") {
|
2019-08-22 19:57:32 +00:00
|
|
|
init::init_main(shell_name).expect("can't init_main");
|
2019-08-20 01:44:53 +00:00
|
|
|
} else {
|
2019-08-22 19:57:32 +00:00
|
|
|
init::init_stub(shell_name).expect("can't init_stub");
|
2019-08-20 01:44:53 +00:00
|
|
|
}
|
2019-07-03 12:03:02 +00:00
|
|
|
}
|
2019-06-06 12:18:00 +00:00
|
|
|
("prompt", Some(sub_m)) => print::prompt(sub_m.clone()),
|
|
|
|
("module", Some(sub_m)) => {
|
2019-08-20 04:42:25 +00:00
|
|
|
if sub_m.is_present("list") {
|
|
|
|
println!("Supported modules list");
|
|
|
|
println!("----------------------");
|
|
|
|
for modules in ALL_MODULES {
|
|
|
|
println!("{}", modules);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(module_name) = sub_m.value_of("name") {
|
|
|
|
print::module(module_name, sub_m.clone());
|
|
|
|
}
|
2019-06-06 12:18:00 +00:00
|
|
|
}
|
2020-04-26 13:58:39 +00:00
|
|
|
("config", Some(sub_m)) => {
|
|
|
|
if let Some(name) = sub_m.value_of("name") {
|
|
|
|
if let Some(value) = sub_m.value_of("value") {
|
|
|
|
configure::update_configuration(name, value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
configure::edit_configuration()
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 23:40:12 +00:00
|
|
|
("bug-report", Some(_)) => bug_report::create(),
|
2019-12-19 22:38:06 +00:00
|
|
|
("time", _) => {
|
|
|
|
match SystemTime::now()
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
.ok()
|
|
|
|
{
|
|
|
|
Some(time) => println!("{}", time.as_millis()),
|
|
|
|
None => println!("{}", -1),
|
|
|
|
}
|
|
|
|
}
|
2020-01-02 04:19:08 +00:00
|
|
|
("explain", Some(sub_m)) => print::explain(sub_m.clone()),
|
2020-09-21 17:06:15 +00:00
|
|
|
("timings", Some(sub_m)) => print::timings(sub_m.clone()),
|
2020-06-03 15:25:51 +00:00
|
|
|
("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());
|
|
|
|
}
|
2020-09-28 20:38:50 +00:00
|
|
|
("session", _) => println!(
|
|
|
|
"{}",
|
|
|
|
rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(16)
|
|
|
|
.collect::<String>()
|
|
|
|
),
|
2020-06-03 15:25:51 +00:00
|
|
|
(command, _) => unreachable!("Invalid subcommand: {}", command),
|
2019-06-06 12:18:00 +00:00
|
|
|
}
|
2019-04-02 03:23:03 +00:00
|
|
|
}
|