2019-04-08 17:35:38 -04:00
|
|
|
use clap::ArgMatches;
|
2019-05-09 23:51:50 -04:00
|
|
|
use rayon::prelude::*;
|
2019-04-11 19:31:30 -04:00
|
|
|
use std::io::{self, Write};
|
2019-04-08 17:35:38 -04:00
|
|
|
|
2019-07-27 18:25:13 -04:00
|
|
|
use crate::config::Config;
|
2019-04-19 16:57:14 -04:00
|
|
|
use crate::context::Context;
|
2019-05-01 16:34:24 -04:00
|
|
|
use crate::module::Module;
|
2019-08-20 10:27:25 +05:45
|
|
|
use crate::module::ALL_MODULES;
|
2019-04-03 20:14:26 -04:00
|
|
|
use crate::modules;
|
|
|
|
|
|
2019-08-19 10:20:11 +05:45
|
|
|
// List of default prompt order
|
|
|
|
|
// NOTE: If this const value is changed then Default prompt order subheading inside
|
|
|
|
|
// prompt heading of config docs needs to be updated according to changes made here.
|
|
|
|
|
const DEFAULT_PROMPT_ORDER: &[&str] = &[
|
2019-07-02 16:12:53 -04:00
|
|
|
"username",
|
|
|
|
|
"directory",
|
|
|
|
|
"git_branch",
|
|
|
|
|
"git_status",
|
|
|
|
|
"package",
|
|
|
|
|
"nodejs",
|
|
|
|
|
"rust",
|
|
|
|
|
"python",
|
2019-08-13 15:06:10 -04:00
|
|
|
"golang",
|
2019-08-08 10:25:30 -07:00
|
|
|
"cmd_duration",
|
2019-07-02 16:12:53 -04:00
|
|
|
"line_break",
|
2019-08-12 18:42:33 +01:00
|
|
|
"jobs",
|
2019-08-12 00:46:12 -04:00
|
|
|
"battery",
|
2019-07-02 16:12:53 -04:00
|
|
|
"character",
|
|
|
|
|
];
|
|
|
|
|
|
2019-04-03 22:57:50 -04:00
|
|
|
pub fn prompt(args: ArgMatches) {
|
2019-04-19 16:57:14 -04:00
|
|
|
let context = Context::new(args);
|
2019-07-27 18:25:13 -04:00
|
|
|
let config = &context.config;
|
2019-04-15 20:54:52 -04:00
|
|
|
|
2019-04-08 17:35:38 -04:00
|
|
|
let stdout = io::stdout();
|
|
|
|
|
let mut handle = stdout.lock();
|
|
|
|
|
|
|
|
|
|
// Write a new line before the prompt
|
2019-07-27 18:25:13 -04:00
|
|
|
if config.get_as_bool("add_newline") != Some(false) {
|
|
|
|
|
writeln!(handle).unwrap();
|
|
|
|
|
}
|
2019-04-11 19:31:30 -04:00
|
|
|
|
2019-08-19 10:20:11 +05:45
|
|
|
let mut prompt_order: Vec<&str> = Vec::new();
|
|
|
|
|
|
|
|
|
|
// Write out a custom prompt order
|
|
|
|
|
if let Some(modules) = config.get_as_array("prompt_order") {
|
|
|
|
|
// if prompt_order = [] use default_prompt_order
|
|
|
|
|
if !modules.is_empty() {
|
|
|
|
|
for module in modules {
|
|
|
|
|
let str_value = module.as_str();
|
|
|
|
|
|
|
|
|
|
if let Some(value) = str_value {
|
|
|
|
|
if ALL_MODULES.contains(&value) {
|
|
|
|
|
prompt_order.push(value);
|
|
|
|
|
} else {
|
|
|
|
|
log::debug!(
|
|
|
|
|
"Expected prompt_order to contain value from {:?}. Instead received {}",
|
|
|
|
|
ALL_MODULES,
|
|
|
|
|
value,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::debug!(
|
|
|
|
|
"Expected prompt_order to be an array of strings. Instead received {} of type {}",
|
|
|
|
|
module,
|
|
|
|
|
module.type_str()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
prompt_order = DEFAULT_PROMPT_ORDER.to_vec();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
prompt_order = DEFAULT_PROMPT_ORDER.to_vec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let modules = &prompt_order
|
2019-05-09 23:51:50 -04:00
|
|
|
.par_iter()
|
2019-05-01 16:34:24 -04:00
|
|
|
.map(|module| modules::handle(module, &context)) // Compute modules
|
|
|
|
|
.flatten()
|
|
|
|
|
.collect::<Vec<Module>>(); // Remove segments set to `None`
|
|
|
|
|
|
|
|
|
|
let mut printable = modules.iter();
|
|
|
|
|
|
|
|
|
|
// Print the first module without its prefix
|
|
|
|
|
if let Some(first_module) = printable.next() {
|
|
|
|
|
let module_without_prefix = first_module.to_string_without_prefix();
|
|
|
|
|
write!(handle, "{}", module_without_prefix).unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print all remaining modules
|
|
|
|
|
printable.for_each(|module| write!(handle, "{}", module).unwrap());
|
2019-04-03 20:14:26 -04:00
|
|
|
}
|
2019-06-06 13:18:00 +01:00
|
|
|
|
|
|
|
|
pub fn module(module_name: &str, args: ArgMatches) {
|
|
|
|
|
let context = Context::new(args);
|
|
|
|
|
|
|
|
|
|
// If the module returns `None`, print an empty string
|
|
|
|
|
let module = modules::handle(module_name, &context)
|
|
|
|
|
.map(|m| m.to_string())
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
print!("{}", module);
|
|
|
|
|
}
|