2020-01-26 22:37:18 +00:00
|
|
|
use ansi_term::ANSIStrings;
|
2019-09-30 12:10:35 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
use rayon::prelude::*;
|
2020-07-07 22:45:32 +00:00
|
|
|
use std::collections::BTreeSet;
|
2020-04-11 16:37:24 +00:00
|
|
|
use std::fmt::{self, Debug, Write as FmtWrite};
|
2019-09-30 12:10:35 +00:00
|
|
|
use std::io::{self, Write};
|
2020-09-21 17:06:15 +00:00
|
|
|
use std::time::Duration;
|
2020-08-05 17:16:59 +00:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2020-01-02 04:19:08 +00:00
|
|
|
use unicode_width::UnicodeWidthChar;
|
2019-09-30 12:10:35 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::configs::PROMPT_ORDER;
|
2020-01-27 23:23:08 +00:00
|
|
|
use crate::context::{Context, Shell};
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::{StringFormatter, VariableHolder};
|
2019-09-30 12:10:35 +00:00
|
|
|
use crate::module::Module;
|
|
|
|
use crate::module::ALL_MODULES;
|
|
|
|
use crate::modules;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::segment::Segment;
|
2019-09-30 12:10:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-01-27 23:23:08 +00:00
|
|
|
// A workaround for a fish bug (see #739,#279). Applying it to all shells
|
|
|
|
// breaks things (see #808,#824,#834). Should only be printed in fish.
|
|
|
|
if let Shell::Fish = context.shell {
|
|
|
|
buf.push_str("\x1b[J"); // An ASCII control code to clear screen
|
|
|
|
}
|
2019-12-19 22:56:30 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let formatter = if let Ok(formatter) = StringFormatter::new(config.format) {
|
|
|
|
formatter
|
|
|
|
} else {
|
|
|
|
log::error!("Error parsing `format`");
|
2020-10-13 16:03:33 +00:00
|
|
|
buf.push('>');
|
2020-07-07 22:45:32 +00:00
|
|
|
return buf;
|
|
|
|
};
|
|
|
|
let modules = formatter.get_variables();
|
|
|
|
let formatter = formatter.map_variables_to_segments(|module| {
|
|
|
|
// Make $all display all modules
|
|
|
|
if module == "all" {
|
|
|
|
Some(Ok(PROMPT_ORDER
|
|
|
|
.par_iter()
|
|
|
|
.flat_map(|module| {
|
|
|
|
handle_module(module, &context, &modules)
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|module| module.segments)
|
|
|
|
.collect::<Vec<Segment>>()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()))
|
|
|
|
} else if context.is_module_disabled_in_config(&module) {
|
|
|
|
None
|
2019-10-06 15:46:46 +00:00
|
|
|
} else {
|
2020-07-07 22:45:32 +00:00
|
|
|
// Get segments from module
|
|
|
|
Some(Ok(handle_module(module, &context, &modules)
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|module| module.segments)
|
|
|
|
.collect::<Vec<Segment>>()))
|
2019-10-06 15:46:46 +00:00
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
2019-09-30 12:10:35 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
// Creates a root module and prints it.
|
|
|
|
let mut root_module = Module::new("Starship Root", "The root module", None);
|
|
|
|
root_module.set_segments(
|
|
|
|
formatter
|
|
|
|
.parse(None)
|
|
|
|
.expect("Unexpected error returned in root format variables"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let module_strings = root_module.ansi_strings_for_shell(context.shell);
|
2020-08-18 16:58:29 +00:00
|
|
|
if config.add_newline {
|
|
|
|
writeln!(buf).unwrap();
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
write!(buf, "{}", ANSIStrings(&module_strings)).unwrap();
|
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);
|
2020-01-26 22:37:18 +00:00
|
|
|
let module = get_module(module_name, context).unwrap_or_default();
|
2019-09-30 12:10:35 +00:00
|
|
|
print!("{}", module);
|
|
|
|
}
|
2020-01-02 04:19:08 +00:00
|
|
|
|
2020-01-26 22:37:18 +00:00
|
|
|
pub fn get_module(module_name: &str, context: Context) -> Option<String> {
|
|
|
|
modules::handle(module_name, &context).map(|m| m.to_string())
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:06:15 +00:00
|
|
|
pub fn timings(args: ArgMatches) {
|
|
|
|
let context = Context::new(args);
|
|
|
|
|
|
|
|
struct ModuleTiming {
|
|
|
|
name: String,
|
|
|
|
name_len: usize,
|
|
|
|
value: String,
|
|
|
|
duration: Duration,
|
|
|
|
duration_len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut modules = compute_modules(&context)
|
|
|
|
.iter()
|
|
|
|
.filter(|module| !module.is_empty() || module.duration.as_millis() > 0)
|
|
|
|
.map(|module| ModuleTiming {
|
|
|
|
name: String::from(module.get_name().as_str()),
|
|
|
|
name_len: better_width(module.get_name().as_str()),
|
|
|
|
value: ansi_term::ANSIStrings(&module.ansi_strings())
|
|
|
|
.to_string()
|
|
|
|
.replace('\n', "\\n"),
|
|
|
|
duration: module.duration,
|
|
|
|
duration_len: better_width(format_duration(&module.duration).as_str()),
|
|
|
|
})
|
|
|
|
.collect::<Vec<ModuleTiming>>();
|
|
|
|
|
|
|
|
modules.sort_by(|a, b| b.duration.cmp(&a.duration));
|
|
|
|
|
|
|
|
let max_name_width = modules.iter().map(|i| i.name_len).max().unwrap_or(0);
|
|
|
|
let max_duration_width = modules.iter().map(|i| i.duration_len).max().unwrap_or(0);
|
|
|
|
|
|
|
|
println!("\n Here are the timings of modules in your prompt (>=1ms or output):");
|
|
|
|
|
|
|
|
// for now we do not expect a wrap around at the end... famous last words
|
|
|
|
// Overall a line looks like this: " {module name} - {duration} - {module value}".
|
|
|
|
for timing in &modules {
|
|
|
|
println!(
|
|
|
|
" {}{} - {}{} - {}",
|
|
|
|
timing.name,
|
|
|
|
" ".repeat(max_name_width - (timing.name_len)),
|
|
|
|
" ".repeat(max_duration_width - (timing.duration_len)),
|
|
|
|
format_duration(&timing.duration),
|
|
|
|
timing.value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 04:19:08 +00:00
|
|
|
pub fn explain(args: ArgMatches) {
|
|
|
|
let context = Context::new(args);
|
|
|
|
|
|
|
|
struct ModuleInfo {
|
|
|
|
value: String,
|
|
|
|
value_len: usize,
|
|
|
|
desc: String,
|
2020-09-21 17:06:15 +00:00
|
|
|
duration: String,
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let dont_print = vec!["line_break"];
|
2020-01-02 04:19:08 +00:00
|
|
|
|
|
|
|
let modules = compute_modules(&context)
|
|
|
|
.into_iter()
|
|
|
|
.filter(|module| !dont_print.contains(&module.get_name().as_str()))
|
2020-09-21 17:06:15 +00:00
|
|
|
// this contains empty modules which should not print
|
|
|
|
.filter(|module| !module.is_empty())
|
2020-01-02 04:19:08 +00:00
|
|
|
.map(|module| {
|
|
|
|
let value = module.get_segments().join("");
|
|
|
|
ModuleInfo {
|
2020-07-07 22:45:32 +00:00
|
|
|
value: ansi_term::ANSIStrings(&module.ansi_strings()).to_string(),
|
2020-09-21 17:06:15 +00:00
|
|
|
value_len: better_width(value.as_str())
|
|
|
|
+ better_width(format_duration(&module.duration).as_str()),
|
2020-01-02 04:19:08 +00:00
|
|
|
desc: module.get_description().to_owned(),
|
2020-09-21 17:06:15 +00:00
|
|
|
duration: format_duration(&module.duration),
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<ModuleInfo>>();
|
|
|
|
|
2020-08-05 17:16:59 +00:00
|
|
|
let max_module_width = modules.iter().map(|i| i.value_len).max().unwrap_or(0);
|
2020-01-02 04:19:08 +00:00
|
|
|
|
2020-09-21 17:06:15 +00:00
|
|
|
// In addition to the module width itself there are also 9 padding characters in each line.
|
|
|
|
// Overall a line looks like this: " {module value} ({xxxms}) - {description}".
|
|
|
|
const PADDING_WIDTH: usize = 9;
|
2020-01-02 04:19:08 +00:00
|
|
|
|
|
|
|
let desc_width = term_size::dimensions()
|
|
|
|
.map(|(w, _)| w)
|
2020-08-05 17:16:59 +00:00
|
|
|
// Add padding length to module length to avoid text overflow. This line also assures desc_width >= 0.
|
|
|
|
.map(|width| width - std::cmp::min(width, max_module_width + PADDING_WIDTH));
|
2020-01-02 04:19:08 +00:00
|
|
|
|
|
|
|
println!("\n Here's a breakdown of your prompt:");
|
|
|
|
for info in modules {
|
|
|
|
if let Some(desc_width) = desc_width {
|
2020-08-05 17:16:59 +00:00
|
|
|
// Custom Textwrapping!
|
|
|
|
let mut current_pos = 0;
|
|
|
|
let mut escaping = false;
|
|
|
|
// Print info
|
|
|
|
print!(
|
2020-09-21 17:06:15 +00:00
|
|
|
" {} ({}){} - ",
|
2020-01-02 04:19:08 +00:00
|
|
|
info.value,
|
2020-09-21 17:06:15 +00:00
|
|
|
info.duration,
|
|
|
|
" ".repeat(max_module_width - (info.value_len))
|
2020-01-02 04:19:08 +00:00
|
|
|
);
|
2020-08-05 17:16:59 +00:00
|
|
|
for g in info.desc.graphemes(true) {
|
|
|
|
// Handle ANSI escape sequnces
|
|
|
|
if g == "\x1B" {
|
|
|
|
escaping = true;
|
|
|
|
}
|
|
|
|
if escaping {
|
|
|
|
print!("{}", g);
|
|
|
|
escaping = !("a" <= g && "z" >= g || "A" <= g && "Z" >= g);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle normal wrapping
|
|
|
|
current_pos += grapheme_width(g);
|
|
|
|
// Wrap when hitting max width or newline
|
|
|
|
if g == "\n" || current_pos > desc_width {
|
|
|
|
// trim spaces on linebreak
|
|
|
|
if g == " " && desc_width > 1 {
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-02 04:19:08 +00:00
|
|
|
|
2020-08-05 17:16:59 +00:00
|
|
|
print!("\n{}", " ".repeat(max_module_width + PADDING_WIDTH));
|
|
|
|
if g == "\n" {
|
|
|
|
current_pos = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_pos = 1;
|
|
|
|
}
|
|
|
|
print!("{}", g);
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
2020-08-05 17:16:59 +00:00
|
|
|
println!();
|
2020-01-02 04:19:08 +00:00
|
|
|
} else {
|
|
|
|
println!(
|
2020-08-05 17:16:59 +00:00
|
|
|
" {}{} - {}",
|
2020-01-02 04:19:08 +00:00
|
|
|
info.value,
|
2020-08-05 17:16:59 +00:00
|
|
|
" ".repeat(max_module_width - info.value_len),
|
2020-01-02 04:19:08 +00:00
|
|
|
info.desc,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_modules<'a>(context: &'a Context) -> Vec<Module<'a>> {
|
2020-07-07 22:45:32 +00:00
|
|
|
let mut prompt_order: Vec<Module<'a>> = Vec::new();
|
|
|
|
|
|
|
|
let config = context.config.get_root_config();
|
|
|
|
let formatter = if let Ok(formatter) = StringFormatter::new(config.format) {
|
|
|
|
formatter
|
|
|
|
} else {
|
|
|
|
log::error!("Error parsing `format`");
|
|
|
|
return Vec::new();
|
|
|
|
};
|
|
|
|
let modules = formatter.get_variables();
|
|
|
|
|
|
|
|
for module in &modules {
|
|
|
|
// Manually add all modules if `$all` is encountered
|
|
|
|
if module == "all" {
|
|
|
|
for module in PROMPT_ORDER.iter() {
|
|
|
|
let modules = handle_module(module, &context, &modules);
|
|
|
|
prompt_order.extend(modules.into_iter());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let modules = handle_module(module, &context, &modules);
|
|
|
|
prompt_order.extend(modules.into_iter());
|
|
|
|
}
|
2020-04-11 16:37:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
prompt_order
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_module<'a>(
|
|
|
|
module: &str,
|
|
|
|
context: &'a Context,
|
|
|
|
module_list: &BTreeSet<String>,
|
|
|
|
) -> Vec<Module<'a>> {
|
2020-04-11 16:37:24 +00:00
|
|
|
struct DebugCustomModules<'tmp>(&'tmp toml::value::Table);
|
|
|
|
|
|
|
|
impl Debug for DebugCustomModules<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_list().entries(self.0.keys()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let mut modules: Vec<Option<Module>> = Vec::new();
|
2020-01-02 04:19:08 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
if ALL_MODULES.contains(&module) {
|
|
|
|
// Write out a module if it isn't disabled
|
|
|
|
if !context.is_module_disabled_in_config(module) {
|
|
|
|
modules.push(modules::handle(module, &context));
|
|
|
|
}
|
|
|
|
} else if module == "custom" {
|
|
|
|
// Write out all custom modules, except for those that are explicitly set
|
|
|
|
if let Some(custom_modules) = context.config.get_custom_modules() {
|
|
|
|
let custom_modules = custom_modules
|
|
|
|
.iter()
|
|
|
|
.map(|(custom_module, config)| {
|
|
|
|
if should_add_implicit_custom_module(custom_module, config, &module_list) {
|
|
|
|
modules::custom::module(custom_module, &context)
|
|
|
|
} else {
|
|
|
|
None
|
2020-04-11 16:37:24 +00:00
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<Option<Module<'a>>>>();
|
|
|
|
modules.extend(custom_modules)
|
|
|
|
}
|
2020-10-13 16:03:33 +00:00
|
|
|
} else if let Some(module) = module.strip_prefix("custom.") {
|
2020-07-07 22:45:32 +00:00
|
|
|
// Write out a custom module if it isn't disabled (and it exists...)
|
2020-10-13 16:03:33 +00:00
|
|
|
match context.is_custom_module_disabled_in_config(&module) {
|
2020-07-07 22:45:32 +00:00
|
|
|
Some(true) => (), // Module is disabled, we don't add it to the prompt
|
2020-10-13 16:03:33 +00:00
|
|
|
Some(false) => modules.push(modules::custom::module(&module, &context)),
|
2020-07-07 22:45:32 +00:00
|
|
|
None => match context.config.get_custom_modules() {
|
|
|
|
Some(modules) => log::debug!(
|
2020-09-01 17:07:12 +00:00
|
|
|
"top level format contains custom module \"{}\", but no configuration was provided. Configuration for the following modules were provided: {:?}",
|
2020-07-07 22:45:32 +00:00
|
|
|
module,
|
|
|
|
DebugCustomModules(modules),
|
2020-04-11 16:37:24 +00:00
|
|
|
),
|
2020-07-07 22:45:32 +00:00
|
|
|
None => log::debug!(
|
2020-09-01 17:07:12 +00:00
|
|
|
"top level format contains custom module \"{}\", but no configuration was provided.",
|
2020-07-07 22:45:32 +00:00
|
|
|
module,
|
2020-04-11 16:37:24 +00:00
|
|
|
),
|
2020-07-07 22:45:32 +00:00
|
|
|
},
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
} else {
|
|
|
|
log::debug!(
|
2020-09-01 17:07:12 +00:00
|
|
|
"Expected top level format to contain value from {:?}. Instead received {}",
|
2020-07-07 22:45:32 +00:00
|
|
|
ALL_MODULES,
|
|
|
|
module,
|
|
|
|
);
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
modules.into_iter().flatten().collect()
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 16:37:24 +00:00
|
|
|
fn should_add_implicit_custom_module(
|
|
|
|
custom_module: &str,
|
|
|
|
config: &toml::Value,
|
2020-07-07 22:45:32 +00:00
|
|
|
module_list: &BTreeSet<String>,
|
2020-04-11 16:37:24 +00:00
|
|
|
) -> bool {
|
2020-07-07 22:45:32 +00:00
|
|
|
let explicit_module_name = format!("custom.{}", custom_module);
|
|
|
|
let is_explicitly_specified = module_list.contains(&explicit_module_name);
|
2020-04-11 16:37:24 +00:00
|
|
|
|
|
|
|
if is_explicitly_specified {
|
|
|
|
// The module is already specified explicitly, so we skip it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let false_value = toml::Value::Boolean(false);
|
|
|
|
|
|
|
|
!config
|
|
|
|
.get("disabled")
|
|
|
|
.unwrap_or(&false_value)
|
|
|
|
.as_bool()
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2020-08-05 17:16:59 +00:00
|
|
|
fn better_width(s: &str) -> usize {
|
|
|
|
s.graphemes(true).map(grapheme_width).sum()
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:06:15 +00:00
|
|
|
pub fn format_duration(duration: &Duration) -> String {
|
|
|
|
let milis = duration.as_millis();
|
|
|
|
if milis == 0 {
|
|
|
|
"<1ms".to_string()
|
|
|
|
} else {
|
|
|
|
format!("{:?}ms", &milis)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 17:16:59 +00:00
|
|
|
// Assume that graphemes have width of the first character in the grapheme
|
|
|
|
fn grapheme_width(g: &str) -> usize {
|
|
|
|
g.chars().next().and_then(|i| i.width()).unwrap_or(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_grapheme_aware_better_width() {
|
|
|
|
// UnicodeWidthStr::width would return 8
|
|
|
|
assert_eq!(2, better_width("👩👩👦👦"));
|
|
|
|
assert_eq!(1, better_width("Ü"));
|
|
|
|
assert_eq!(11, better_width("normal text"));
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|