2022-02-25 04:31:01 +00:00
|
|
|
#![warn(clippy::disallowed_methods)]
|
2021-07-16 19:20:59 +00:00
|
|
|
|
2021-01-22 19:14:51 +00:00
|
|
|
use clap::crate_authors;
|
2020-06-03 15:25:51 +00:00
|
|
|
use std::io;
|
2023-03-03 10:46:16 +00:00
|
|
|
use std::path::PathBuf;
|
2022-02-27 18:37:43 +00:00
|
|
|
use std::thread::available_parallelism;
|
2019-12-19 22:38:06 +00:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2022-09-29 21:51:56 +00:00
|
|
|
use clap::{CommandFactory, Parser, Subcommand};
|
2022-01-04 09:49:42 +00:00
|
|
|
use clap_complete::{generate, Shell as CompletionShell};
|
2020-09-28 20:38:50 +00:00
|
|
|
use rand::distributions::Alphanumeric;
|
|
|
|
use rand::Rng;
|
2023-04-14 00:29:21 +00:00
|
|
|
use starship::context::{Context, Properties, Target};
|
2020-10-17 09:09:27 +00:00
|
|
|
use starship::module::ALL_MODULES;
|
|
|
|
use starship::*;
|
2019-04-02 04:45:49 +00:00
|
|
|
|
2022-01-04 09:49:42 +00:00
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[clap(
|
|
|
|
author=crate_authors!(),
|
|
|
|
version=shadow::PKG_VERSION,
|
2022-03-20 01:01:57 +00:00
|
|
|
long_version=shadow::CLAP_LONG_VERSION,
|
2022-02-19 21:24:11 +00:00
|
|
|
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 {
|
2022-09-29 21:51:56 +00:00
|
|
|
#[clap(value_enum)]
|
2022-01-04 09:49:42 +00:00
|
|
|
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
|
2022-04-28 20:27:09 +00:00
|
|
|
#[clap(required_unless_present("list"))]
|
2022-01-04 09:49:42 +00:00
|
|
|
name: Option<String>,
|
|
|
|
/// List out all supported modules
|
|
|
|
#[clap(short, long)]
|
|
|
|
list: bool,
|
|
|
|
#[clap(flatten)]
|
|
|
|
properties: Properties,
|
|
|
|
},
|
2022-08-02 09:51:15 +00:00
|
|
|
/// Prints a preset config
|
|
|
|
Preset {
|
|
|
|
/// The name of preset to be printed
|
|
|
|
#[clap(required_unless_present("list"), value_enum)]
|
|
|
|
name: Option<print::Preset>,
|
2023-03-03 10:46:16 +00:00
|
|
|
/// Output the preset to a file instead of stdout
|
|
|
|
#[clap(short, long, conflicts_with = "list")]
|
|
|
|
output: Option<PathBuf>,
|
2022-08-02 09:51:15 +00:00
|
|
|
/// List out all preset names
|
|
|
|
#[clap(short, long)]
|
|
|
|
list: bool,
|
|
|
|
},
|
2022-01-04 09:49:42 +00:00
|
|
|
/// 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,
|
2023-01-12 03:25:36 +00:00
|
|
|
/// Print the prompt with the specified profile name (instead of the standard left prompt)
|
2022-01-04 09:49:42 +00:00
|
|
|
#[clap(long, conflicts_with = "right")]
|
2023-01-12 03:25:36 +00:00
|
|
|
profile: Option<String>,
|
|
|
|
/// Print the continuation prompt (instead of the standard left prompt)
|
|
|
|
#[clap(long, conflicts_with = "right", conflicts_with = "profile")]
|
2022-01-04 09:49:42 +00:00
|
|
|
continuation: bool,
|
|
|
|
#[clap(flatten)]
|
|
|
|
properties: Properties,
|
|
|
|
},
|
|
|
|
/// Generate random session key
|
|
|
|
Session,
|
|
|
|
/// Prints time in milliseconds
|
2022-02-19 21:24:11 +00:00
|
|
|
#[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,
|
|
|
|
},
|
2022-04-01 15:14:05 +00:00
|
|
|
#[cfg(feature = "config-schema")]
|
|
|
|
/// Generate a schema for the starship configuration as JSON-schema
|
|
|
|
ConfigSchema,
|
2022-01-04 09:49:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 03:23:03 +00:00
|
|
|
fn main() {
|
2020-11-11 19:24:43 +00:00
|
|
|
// Configure the current terminal on windows to support ANSI escape sequences.
|
|
|
|
#[cfg(windows)]
|
2022-09-04 16:44:54 +00:00
|
|
|
let _ = nu_ansi_term::enable_ansi_support();
|
2020-09-28 20:38:50 +00:00
|
|
|
logger::init();
|
2022-02-27 18:37:43 +00:00
|
|
|
init_global_threadpool();
|
2019-05-14 04:43:11 +00:00
|
|
|
|
2023-01-04 08:22:02 +00:00
|
|
|
// Delete old log files
|
|
|
|
rayon::spawn(|| {
|
|
|
|
let log_dir = logger::get_log_dir();
|
|
|
|
logger::cleanup_log_files(log_dir);
|
|
|
|
});
|
|
|
|
|
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);
|
2019-08-17 19:33:19 +00:00
|
|
|
|
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");
|
2019-08-20 01:44:53 +00:00
|
|
|
} else {
|
2022-01-04 09:49:42 +00:00
|
|
|
init::init_stub(&shell).expect("can't init_stub");
|
2019-08-20 01:44:53 +00:00
|
|
|
}
|
2019-07-03 12:03:02 +00:00
|
|
|
}
|
2022-01-04 09:49:42 +00:00
|
|
|
Commands::Prompt {
|
|
|
|
properties,
|
|
|
|
right,
|
2023-01-12 03:25:36 +00:00
|
|
|
profile,
|
2022-01-04 09:49:42 +00:00
|
|
|
continuation,
|
|
|
|
} => {
|
2023-01-12 03:25:36 +00:00
|
|
|
let target = match (right, profile, continuation) {
|
|
|
|
(true, _, _) => Target::Right,
|
|
|
|
(_, Some(profile_name), _) => Target::Profile(profile_name),
|
|
|
|
(_, _, true) => Target::Continuation,
|
|
|
|
(_, _, _) => Target::Main,
|
2022-01-04 09:49:42 +00:00
|
|
|
};
|
2023-12-30 16:29:37 +00:00
|
|
|
print::prompt(properties, target);
|
2022-01-04 09:49:42 +00:00
|
|
|
}
|
|
|
|
Commands::Module {
|
|
|
|
name,
|
|
|
|
list,
|
|
|
|
properties,
|
|
|
|
} => {
|
|
|
|
if list {
|
2019-08-20 04:42:25 +00:00
|
|
|
println!("Supported modules list");
|
|
|
|
println!("----------------------");
|
|
|
|
for modules in ALL_MODULES {
|
2022-12-17 17:01:27 +00:00
|
|
|
println!("{modules}");
|
2019-08-20 04:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-04 09:49:42 +00:00
|
|
|
if let Some(module_name) = name {
|
|
|
|
print::module(&module_name, properties);
|
2019-08-20 04:42:25 +00:00
|
|
|
}
|
2019-06-06 12:18:00 +00:00
|
|
|
}
|
2023-03-03 10:46:16 +00:00
|
|
|
Commands::Preset { name, list, output } => print::preset_command(name, output, list),
|
2022-01-04 09:49:42 +00:00
|
|
|
Commands::Config { name, value } => {
|
2023-04-14 00:29:21 +00:00
|
|
|
let context = Context::default();
|
2022-01-04 09:49:42 +00:00
|
|
|
if let Some(name) = name {
|
|
|
|
if let Some(value) = value {
|
2023-12-30 16:29:37 +00:00
|
|
|
configure::update_configuration(&context, &name, &value);
|
2020-04-26 13:58:39 +00:00
|
|
|
}
|
2023-04-14 00:29:21 +00:00
|
|
|
} else if let Err(reason) = configure::edit_configuration(&context, None) {
|
2022-12-17 17:01:27 +00:00
|
|
|
eprintln!("Could not edit configuration: {reason}");
|
2022-05-02 16:43:27 +00:00
|
|
|
std::process::exit(1);
|
2020-04-26 13:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-14 00:29:21 +00:00
|
|
|
Commands::PrintConfig { default, name } => {
|
|
|
|
configure::print_configuration(&Context::default(), default, &name);
|
|
|
|
}
|
|
|
|
Commands::Toggle { name, value } => {
|
2023-12-30 16:29:37 +00:00
|
|
|
configure::toggle_configuration(&Context::default(), &name, &value);
|
2023-04-14 00:29:21 +00:00
|
|
|
}
|
2022-01-04 09:49:42 +00:00
|
|
|
Commands::BugReport => bug_report::create(),
|
|
|
|
Commands::Time => {
|
2019-12-19 22:38:06 +00:00
|
|
|
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,
|
2022-02-19 21:24:11 +00:00
|
|
|
&mut Cli::command(),
|
2022-01-04 09:49:42 +00:00
|
|
|
"starship",
|
|
|
|
&mut io::stdout().lock(),
|
|
|
|
),
|
|
|
|
Commands::Session => println!(
|
2020-09-28 20:38:50 +00:00
|
|
|
"{}",
|
|
|
|
rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(16)
|
2020-12-25 18:08:07 +00:00
|
|
|
.map(char::from)
|
2020-09-28 20:38:50 +00:00
|
|
|
.collect::<String>()
|
|
|
|
),
|
2022-04-01 15:14:05 +00:00
|
|
|
#[cfg(feature = "config-schema")]
|
|
|
|
Commands::ConfigSchema => print::print_schema(),
|
2019-06-06 12:18:00 +00:00
|
|
|
}
|
2019-04-02 03:23:03 +00:00
|
|
|
}
|
2022-02-27 18:37:43 +00:00
|
|
|
|
2022-02-28 16:27:33 +00:00
|
|
|
/// Initialize global `rayon` thread pool
|
2022-02-27 18:37:43 +00:00
|
|
|
fn init_global_threadpool() {
|
|
|
|
// Allow overriding the number of threads
|
|
|
|
let num_threads = std::env::var("STARSHIP_NUM_THREADS")
|
|
|
|
.ok()
|
|
|
|
.and_then(|s| s.parse().ok())
|
|
|
|
// Default to the number of logical cores,
|
|
|
|
// but restrict the number of threads to 8
|
|
|
|
.unwrap_or_else(|| available_parallelism().map(usize::from).unwrap_or(1).min(8));
|
|
|
|
|
|
|
|
rayon::ThreadPoolBuilder::new()
|
|
|
|
.num_threads(num_threads)
|
|
|
|
.build_global()
|
|
|
|
.expect("Failed to initialize worker thread pool");
|
|
|
|
}
|