2019-09-30 12:10:35 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
use rayon::prelude::*;
|
2019-10-09 01:43:28 +00:00
|
|
|
use std::fmt::Write as FmtWrite;
|
2019-09-30 12:10:35 +00:00
|
|
|
use std::io::{self, Write};
|
|
|
|
|
|
|
|
use crate::context::Context;
|
|
|
|
use crate::module::Module;
|
|
|
|
use crate::module::ALL_MODULES;
|
|
|
|
use crate::modules;
|
|
|
|
|
|
|
|
pub fn prompt(args: ArgMatches) {
|
|
|
|
let context = Context::new(args);
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut handle = stdout.lock();
|
2019-10-09 01:43:28 +00:00
|
|
|
write!(handle, "{}", get_prompt(context)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_prompt(context: Context) -> String {
|
|
|
|
let config = context.config.get_root_config();
|
|
|
|
let mut buf = String::new();
|
2019-09-30 12:10:35 +00:00
|
|
|
|
|
|
|
// Write a new line before the prompt
|
|
|
|
if config.add_newline {
|
2019-10-09 01:43:28 +00:00
|
|
|
writeln!(buf).unwrap();
|
2019-09-30 12:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut prompt_order: Vec<&str> = Vec::new();
|
|
|
|
|
|
|
|
// Write out a custom prompt order
|
|
|
|
for module in config.prompt_order {
|
|
|
|
if ALL_MODULES.contains(&module) {
|
|
|
|
prompt_order.push(module);
|
|
|
|
} else {
|
|
|
|
log::debug!(
|
|
|
|
"Expected prompt_order to contain value from {:?}. Instead received {}",
|
|
|
|
ALL_MODULES,
|
|
|
|
module,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let modules = &prompt_order
|
|
|
|
.par_iter()
|
2019-10-05 14:10:16 +00:00
|
|
|
.filter(|module| !context.is_module_disabled_in_config(module))
|
2019-09-30 12:10:35 +00:00
|
|
|
.map(|module| modules::handle(module, &context)) // Compute modules
|
|
|
|
.flatten()
|
|
|
|
.collect::<Vec<Module>>(); // Remove segments set to `None`
|
|
|
|
|
2019-10-06 15:46:46 +00:00
|
|
|
let mut print_without_prefix = true;
|
2019-10-24 10:31:17 +00:00
|
|
|
let printable = modules.iter();
|
2019-09-30 12:10:35 +00:00
|
|
|
|
2019-10-06 15:46:46 +00:00
|
|
|
for module in printable {
|
|
|
|
// Skip printing the prefix of a module after the line_break
|
|
|
|
if print_without_prefix {
|
|
|
|
let module_without_prefix = module.to_string_without_prefix();
|
2019-10-09 01:43:28 +00:00
|
|
|
write!(buf, "{}", module_without_prefix).unwrap()
|
2019-10-06 15:46:46 +00:00
|
|
|
} else {
|
2019-10-09 01:43:28 +00:00
|
|
|
write!(buf, "{}", module).unwrap();
|
2019-10-06 15:46:46 +00:00
|
|
|
}
|
2019-09-30 12:10:35 +00:00
|
|
|
|
2019-10-06 15:46:46 +00:00
|
|
|
print_without_prefix = module.get_name() == "line_break"
|
|
|
|
}
|
2019-10-09 01:43:28 +00:00
|
|
|
|
|
|
|
buf
|
2019-09-30 12:10:35 +00: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);
|
|
|
|
}
|