1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-10-31 18:52:29 +00:00
starship/src/main.rs

224 lines
7.0 KiB
Rust
Raw Normal View History

#![warn(clippy::disallowed_methods)]
use clap::crate_authors;
use std::io;
use std::time::SystemTime;
use clap::{IntoApp, Parser, Subcommand};
2022-01-04 09:49:42 +00:00
use clap_complete::{generate, Shell as CompletionShell};
use rand::distributions::Alphanumeric;
use rand::Rng;
2022-01-04 09:49:42 +00:00
use starship::context::{Properties, Target};
use starship::module::ALL_MODULES;
use starship::*;
2019-04-02 04:45:49 +00:00
2022-01-04 09:49:42 +00:00
fn long_version() -> &'static str {
let ver = Box::new(crate::shadow::clap_long_version());
2022-01-04 09:49:42 +00:00
Box::leak(ver).as_str()
}
#[derive(Parser, Debug)]
#[clap(
author=crate_authors!(),
version=shadow::PKG_VERSION,
long_version=long_version(),
about="The cross-shell prompt for astronauts. ☄🌌️",
subcommand_required=true,
arg_required_else_help=true,
2022-01-04 09:49:42 +00:00
)]
struct Cli {
#[clap(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
enum Commands {
/// Create a pre-populated GitHub issue with information about your configuration
BugReport,
/// Generate starship shell completions for your shell to stdout
Completions {
#[clap(arg_enum)]
shell: CompletionShell,
},
/// Edit the starship configuration
Config {
/// Configuration key to edit
#[clap(requires = "value")]
name: Option<String>,
/// Value to place into that key
value: Option<String>,
},
/// Explains the currently showing modules
Explain(Properties),
/// Prints the shell function used to execute starship
Init {
shell: String,
#[clap(long)]
print_full_init: bool,
},
/// Prints a specific prompt module
Module {
/// The name of the module to be printed
#[clap(required = true, required_unless_present = "list")]
name: Option<String>,
/// List out all supported modules
#[clap(short, long)]
list: bool,
#[clap(flatten)]
properties: Properties,
},
/// Prints the computed starship configuration
PrintConfig {
/// Print the default instead of the computed config
#[clap(short, long)]
default: bool,
/// Configuration keys to print
name: Vec<String>,
},
/// Prints the full starship prompt
Prompt {
/// Print the right prompt (instead of the standard left prompt)
#[clap(long)]
right: bool,
/// Print the continuation prompt (instead of the standard left prompt)
#[clap(long, conflicts_with = "right")]
continuation: bool,
#[clap(flatten)]
properties: Properties,
},
/// Generate random session key
Session,
/// Prints time in milliseconds
#[clap(hide = true)]
2022-01-04 09:49:42 +00:00
Time,
/// Prints timings of all active modules
Timings(Properties),
/// Toggle a given starship module
Toggle {
/// The name of the module to be toggled
name: String,
/// The key of the config to be toggled
#[clap(default_value = "disabled")]
value: String,
},
}
2019-04-02 03:23:03 +00:00
fn main() {
// Configure the current terminal on windows to support ANSI escape sequences.
#[cfg(windows)]
let _ = ansi_term::enable_ansi_support();
logger::init();
2019-05-14 04:43:11 +00:00
feat(cli): Print arguments if argument parsing fails (#3560) * fix(#3554): Print the command line argv on clap error This is a very bare implementation that just prints the error and then a note with the arguments passed, it does this manually and doesn't use clap. I've also chosen to use `Vec`'s `Debug` implementation instead of rolling my own one because I thought it was good enough, but there might be a better way of doing all this. Altogether, I think this will be very useful to help in the diagnostic of other bugs :) * fix(#3554): Print the command line argv on clap error This is a very bare implementation that just prints the error and then a note with the arguments passed, it does this manually and doesn't use clap. I've also chosen to use `Vec`'s `Debug` implementation instead of rolling my own one because I thought it was good enough, but there might be a better way of doing all this. Altogether, I think this will be very useful to help in the diagnostic of other bugs :) EDIT: removed `dbg!`, set it to exit always. * correctness(exit): don't print argv / exit with error on help and version error kinds * fix: Avoid panicking when stdout/stderr closing unexpectedly * refactor(cli): use `use_stderr` instead of manual match for error kinds `clap` uses `use_stderr` to reliably check whether the error given is actually an error coming from user input or rather a hint to display other info (version, help, etc.) Also reworded/moved a couple of comments so that they explain better what is the thought process behind the code
2022-02-06 22:04:28 +00:00
let args = match Cli::try_parse() {
Ok(args) => args,
Err(e) => {
// if the error is not printed to stderr, this means it was not really
// an error but rather some information is going to be listed, therefore
// we won't print the arguments passed
let is_info_only = !e.use_stderr();
// print the error and void panicking in case of stdout/stderr closing unexpectedly
let _ = e.print();
// if there was no mistake by the user and we're only going to display information,
// we won't put arguments or exit with non-zero code
let exit_code = if is_info_only {
0
} else {
// print the arguments
// avoid panicking in case of stderr closing
let mut stderr = io::stderr();
use io::Write;
let _ = writeln!(
stderr,
"\nNOTE:\n passed arguments: {:?}",
// collect into a vec to format args as a slice
std::env::args().skip(1).collect::<Vec<_>>()
);
// clap exits with status 2 on error:
// https://docs.rs/clap/latest/clap/struct.Error.html#method.exit
2
};
std::process::exit(exit_code);
}
};
2022-01-04 09:49:42 +00:00
log::trace!("Parsed arguments: {:#?}", args);
2022-01-04 09:49:42 +00:00
match args.command {
Commands::Init {
shell,
print_full_init,
} => {
if print_full_init {
init::init_main(&shell).expect("can't init_main");
} else {
2022-01-04 09:49:42 +00:00
init::init_stub(&shell).expect("can't init_stub");
}
}
2022-01-04 09:49:42 +00:00
Commands::Prompt {
properties,
right,
continuation,
} => {
let target = match (right, continuation) {
(true, _) => Target::Right,
(_, true) => Target::Continuation,
(_, _) => Target::Main,
};
print::prompt(properties, target)
}
Commands::Module {
name,
list,
properties,
} => {
if list {
println!("Supported modules list");
println!("----------------------");
for modules in ALL_MODULES {
println!("{}", modules);
}
}
2022-01-04 09:49:42 +00:00
if let Some(module_name) = name {
print::module(&module_name, properties);
}
}
2022-01-04 09:49:42 +00:00
Commands::Config { name, value } => {
if let Some(name) = name {
if let Some(value) = value {
configure::update_configuration(&name, &value)
}
} else {
configure::edit_configuration()
}
}
2022-01-04 09:49:42 +00:00
Commands::PrintConfig { default, name } => configure::print_configuration(default, &name),
Commands::Toggle { name, value } => configure::toggle_configuration(&name, &value),
Commands::BugReport => bug_report::create(),
Commands::Time => {
match SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.ok()
{
Some(time) => println!("{}", time.as_millis()),
None => println!("{}", -1),
}
}
2022-01-04 09:49:42 +00:00
Commands::Explain(props) => print::explain(props),
Commands::Timings(props) => print::timings(props),
Commands::Completions { shell } => generate(
shell,
&mut Cli::command(),
2022-01-04 09:49:42 +00:00
"starship",
&mut io::stdout().lock(),
),
Commands::Session => println!(
"{}",
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect::<String>()
),
}
2019-04-02 03:23:03 +00:00
}