2019-04-08 23:35:38 +02:00
|
|
|
use clap::ArgMatches;
|
2019-05-10 05:51:50 +02:00
|
|
|
use rayon::prelude::*;
|
2019-04-12 01:31:30 +02:00
|
|
|
use std::io::{self, Write};
|
2019-04-08 23:35:38 +02:00
|
|
|
|
2019-07-28 00:25:13 +02:00
|
|
|
use crate::config::Config;
|
2019-04-19 22:57:14 +02:00
|
|
|
use crate::context::Context;
|
2019-05-01 22:34:24 +02:00
|
|
|
use crate::module::Module;
|
2019-04-04 02:14:26 +02:00
|
|
|
use crate::modules;
|
|
|
|
|
2019-07-02 22:12:53 +02:00
|
|
|
const PROMPT_ORDER: &[&str] = &[
|
|
|
|
"battery",
|
|
|
|
"username",
|
|
|
|
"directory",
|
|
|
|
"git_branch",
|
|
|
|
"git_status",
|
|
|
|
"package",
|
|
|
|
"nodejs",
|
|
|
|
"rust",
|
|
|
|
"python",
|
|
|
|
"go",
|
|
|
|
"line_break",
|
|
|
|
"character",
|
|
|
|
];
|
|
|
|
|
2019-04-04 04:57:50 +02:00
|
|
|
pub fn prompt(args: ArgMatches) {
|
2019-04-19 22:57:14 +02:00
|
|
|
let context = Context::new(args);
|
2019-07-28 00:25:13 +02:00
|
|
|
let config = &context.config;
|
2019-04-16 02:54:52 +02:00
|
|
|
|
2019-04-08 23:35:38 +02:00
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut handle = stdout.lock();
|
|
|
|
|
|
|
|
// Write a new line before the prompt
|
2019-07-28 00:25:13 +02:00
|
|
|
if config.get_as_bool("add_newline") != Some(false) {
|
|
|
|
writeln!(handle).unwrap();
|
|
|
|
}
|
2019-04-12 01:31:30 +02:00
|
|
|
|
2019-07-02 22:12:53 +02:00
|
|
|
let modules = PROMPT_ORDER
|
2019-05-10 05:51:50 +02:00
|
|
|
.par_iter()
|
2019-05-01 22:34:24 +02: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-04 02:14:26 +02:00
|
|
|
}
|
2019-06-06 14:18:00 +02: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);
|
|
|
|
}
|