2022-09-29 21:51:56 +00:00
|
|
|
use clap::{builder::PossibleValue, ValueEnum};
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::AnsiStrings;
|
2019-09-30 12:10:35 +00:00
|
|
|
use rayon::prelude::*;
|
2020-07-07 22:45:32 +00:00
|
|
|
use std::collections::BTreeSet;
|
2022-12-28 20:00:49 +00:00
|
|
|
use std::fmt::{Debug, Write as FmtWrite};
|
2019-09-30 12:10:35 +00:00
|
|
|
use std::io::{self, Write};
|
2023-03-03 10:46:16 +00:00
|
|
|
use std::path::PathBuf;
|
2020-09-21 17:06:15 +00:00
|
|
|
use std::time::Duration;
|
2021-09-23 15:52:51 +00:00
|
|
|
use terminal_size::terminal_size;
|
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;
|
2022-01-04 09:49:42 +00:00
|
|
|
use crate::context::{Context, Properties, Shell, Target};
|
2023-01-12 03:25:36 +00:00
|
|
|
use crate::formatter::string_formatter::StringFormatterError;
|
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;
|
2022-08-02 09:51:15 +00:00
|
|
|
use crate::shadow;
|
2019-09-30 12:10:35 +00:00
|
|
|
|
2021-09-12 23:59:15 +00:00
|
|
|
pub struct Grapheme<'a>(pub &'a str);
|
2021-05-02 07:55:26 +00:00
|
|
|
|
|
|
|
impl<'a> Grapheme<'a> {
|
|
|
|
pub fn width(&self) -> usize {
|
|
|
|
self.0
|
|
|
|
.chars()
|
|
|
|
.filter_map(UnicodeWidthChar::width)
|
|
|
|
.max()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait UnicodeWidthGraphemes {
|
|
|
|
fn width_graphemes(&self) -> usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> UnicodeWidthGraphemes for T
|
|
|
|
where
|
|
|
|
T: AsRef<str>,
|
|
|
|
{
|
|
|
|
fn width_graphemes(&self) -> usize {
|
|
|
|
self.as_ref()
|
|
|
|
.graphemes(true)
|
|
|
|
.map(Grapheme)
|
|
|
|
.map(|g| g.width())
|
|
|
|
.sum()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_grapheme_aware_width() {
|
|
|
|
// UnicodeWidthStr::width would return 8
|
|
|
|
assert_eq!(2, "👩👩👦👦".width_graphemes());
|
|
|
|
assert_eq!(1, "Ü".width_graphemes());
|
|
|
|
assert_eq!(11, "normal text".width_graphemes());
|
|
|
|
}
|
|
|
|
|
2022-01-04 09:49:42 +00:00
|
|
|
pub fn prompt(args: Properties, target: Target) {
|
|
|
|
let context = Context::new(args, target);
|
2019-09-30 12:10:35 +00:00
|
|
|
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 {
|
2021-10-27 13:13:17 +00:00
|
|
|
let config = &context.root_config;
|
2019-10-09 01:43:28 +00:00
|
|
|
let mut buf = String::new();
|
2019-09-30 12:10:35 +00:00
|
|
|
|
2021-02-17 17:53:42 +00:00
|
|
|
match std::env::var_os("TERM") {
|
|
|
|
Some(term) if term == "dumb" => {
|
|
|
|
log::error!("Under a 'dumb' terminal (TERM=dumb).");
|
|
|
|
buf.push_str("Starship disabled due to TERM=dumb > ");
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
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.
|
2022-02-14 12:11:55 +00:00
|
|
|
if Shell::Fish == context.shell && context.target == Target::Main {
|
2020-01-27 23:23:08 +00:00
|
|
|
buf.push_str("\x1b[J"); // An ASCII control code to clear screen
|
|
|
|
}
|
2019-12-19 22:56:30 +00:00
|
|
|
|
2021-09-08 19:45:27 +00:00
|
|
|
let (formatter, modules) = load_formatter_and_modules(&context);
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let formatter = formatter.map_variables_to_segments(|module| {
|
2021-09-08 19:45:27 +00:00
|
|
|
// Make $all display all modules not explicitly referenced
|
2020-07-07 22:45:32 +00:00
|
|
|
if module == "all" {
|
2021-09-08 19:45:27 +00:00
|
|
|
Some(Ok(all_modules_uniq(&modules)
|
2020-07-07 22:45:32 +00:00
|
|
|
.par_iter()
|
|
|
|
.flat_map(|module| {
|
|
|
|
handle_module(module, &context, &modules)
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|module| module.segments)
|
|
|
|
.collect::<Vec<Segment>>()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()))
|
2021-07-29 18:27:46 +00:00
|
|
|
} else if context.is_module_disabled_in_config(module) {
|
2020-07-07 22:45:32 +00:00
|
|
|
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
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(&context))
|
2020-07-07 22:45:32 +00:00
|
|
|
.expect("Unexpected error returned in root format variables"),
|
|
|
|
);
|
|
|
|
|
2021-09-12 23:59:15 +00:00
|
|
|
let module_strings = root_module.ansi_strings_for_shell(context.shell, Some(context.width));
|
2022-01-04 09:49:42 +00:00
|
|
|
if config.add_newline && context.target != Target::Continuation {
|
2022-01-01 13:12:11 +00:00
|
|
|
// continuation prompts normally do not include newlines, but they can
|
2020-08-18 16:58:29 +00:00
|
|
|
writeln!(buf).unwrap();
|
|
|
|
}
|
2022-09-04 16:44:54 +00:00
|
|
|
write!(buf, "{}", AnsiStrings(&module_strings)).unwrap();
|
2019-10-09 01:43:28 +00:00
|
|
|
|
2022-01-04 09:49:42 +00:00
|
|
|
if context.target == Target::Right {
|
2021-09-08 19:45:27 +00:00
|
|
|
// right prompts generally do not allow newlines
|
|
|
|
buf = buf.replace('\n', "");
|
|
|
|
}
|
|
|
|
|
2021-02-27 18:55:27 +00:00
|
|
|
// escape \n and ! characters for tcsh
|
2022-05-23 10:58:27 +00:00
|
|
|
if context.shell == Shell::Tcsh {
|
2021-02-27 18:55:27 +00:00
|
|
|
buf = buf.replace('!', "\\!");
|
|
|
|
// space is required before newline
|
|
|
|
buf = buf.replace('\n', " \\n");
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:43:28 +00:00
|
|
|
buf
|
2019-09-30 12:10:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 09:49:42 +00:00
|
|
|
pub fn module(module_name: &str, args: Properties) {
|
|
|
|
let context = Context::new(args, Target::Main);
|
2020-01-26 22:37:18 +00:00
|
|
|
let module = get_module(module_name, context).unwrap_or_default();
|
2022-11-05 11:40:46 +00:00
|
|
|
print!("{module}");
|
2019-09-30 12:10:35 +00:00
|
|
|
}
|
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())
|
|
|
|
}
|
|
|
|
|
2022-01-04 09:49:42 +00:00
|
|
|
pub fn timings(args: Properties) {
|
|
|
|
let context = Context::new(args, Target::Main);
|
2020-09-21 17:06:15 +00:00
|
|
|
|
|
|
|
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()),
|
2021-05-02 07:55:26 +00:00
|
|
|
name_len: module.get_name().width_graphemes(),
|
2022-09-04 16:44:54 +00:00
|
|
|
value: nu_ansi_term::AnsiStrings(&module.ansi_strings())
|
2020-09-21 17:06:15 +00:00
|
|
|
.to_string()
|
|
|
|
.replace('\n', "\\n"),
|
|
|
|
duration: module.duration,
|
2021-05-02 07:55:26 +00:00
|
|
|
duration_len: format_duration(&module.duration).width_graphemes(),
|
2020-09-21 17:06:15 +00:00
|
|
|
})
|
|
|
|
.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
|
2021-07-30 02:45:03 +00:00
|
|
|
// Overall a line looks like this: " {module name} - {duration} - "{module value}"".
|
2020-09-21 17:06:15 +00:00
|
|
|
for timing in &modules {
|
|
|
|
println!(
|
2021-07-30 02:45:03 +00:00
|
|
|
" {}{} - {}{} - \"{}\"",
|
2020-09-21 17:06:15 +00:00
|
|
|
timing.name,
|
|
|
|
" ".repeat(max_name_width - (timing.name_len)),
|
|
|
|
" ".repeat(max_duration_width - (timing.duration_len)),
|
|
|
|
format_duration(&timing.duration),
|
|
|
|
timing.value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 09:49:42 +00:00
|
|
|
pub fn explain(args: Properties) {
|
|
|
|
let context = Context::new(args, Target::Main);
|
2020-01-02 04:19:08 +00:00
|
|
|
|
|
|
|
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-12-16 16:54:21 +00:00
|
|
|
static DONT_PRINT: &[&str] = &["line_break"];
|
2020-01-02 04:19:08 +00:00
|
|
|
|
|
|
|
let modules = compute_modules(&context)
|
|
|
|
.into_iter()
|
2020-12-16 16:54:21 +00:00
|
|
|
.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 {
|
2022-09-04 16:44:54 +00:00
|
|
|
value: nu_ansi_term::AnsiStrings(&module.ansi_strings()).to_string(),
|
2021-05-02 07:55:26 +00:00
|
|
|
value_len: value.width_graphemes()
|
|
|
|
+ format_duration(&module.duration).width_graphemes(),
|
2021-07-29 18:27:46 +00:00
|
|
|
desc: module.get_description().clone(),
|
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
|
|
|
|
2021-07-30 02:45:03 +00:00
|
|
|
// In addition to the module width itself there are also 11 padding characters in each line.
|
|
|
|
// Overall a line looks like this: " "{module value}" ({xxxms}) - {description}".
|
|
|
|
const PADDING_WIDTH: usize = 11;
|
2020-01-02 04:19:08 +00:00
|
|
|
|
2021-09-23 15:52:51 +00:00
|
|
|
let desc_width = terminal_size()
|
|
|
|
.map(|(w, _)| w.0 as usize)
|
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!(
|
2021-07-30 02:45:03 +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) {
|
2022-06-28 18:19:17 +00:00
|
|
|
// Handle ANSI escape sequences
|
2020-08-05 17:16:59 +00:00
|
|
|
if g == "\x1B" {
|
|
|
|
escaping = true;
|
|
|
|
}
|
|
|
|
if escaping {
|
2022-11-05 11:40:46 +00:00
|
|
|
print!("{g}");
|
2020-11-23 19:38:11 +00:00
|
|
|
escaping = !(("a"..="z").contains(&g) || ("A"..="Z").contains(&g));
|
2020-08-05 17:16:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle normal wrapping
|
2021-05-02 07:55:26 +00:00
|
|
|
current_pos += Grapheme(g).width();
|
2020-08-05 17:16:59 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2022-11-05 11:40:46 +00:00
|
|
|
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();
|
|
|
|
|
2021-09-08 19:45:27 +00:00
|
|
|
let (_formatter, modules) = load_formatter_and_modules(context);
|
2020-07-07 22:45:32 +00:00
|
|
|
|
|
|
|
for module in &modules {
|
|
|
|
// Manually add all modules if `$all` is encountered
|
|
|
|
if module == "all" {
|
2021-09-08 19:45:27 +00:00
|
|
|
for module in all_modules_uniq(&modules) {
|
|
|
|
let modules = handle_module(&module, context, &modules);
|
2021-09-02 15:48:29 +00:00
|
|
|
prompt_order.extend(modules);
|
2020-07-07 22:45:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-29 18:27:46 +00:00
|
|
|
let modules = handle_module(module, context, &modules);
|
2021-09-02 15:48:29 +00:00
|
|
|
prompt_order.extend(modules);
|
2020-07-07 22:45:32 +00:00
|
|
|
}
|
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>> {
|
2021-01-20 14:08:55 +00:00
|
|
|
let mut modules: Vec<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) {
|
2021-07-29 18:27:46 +00:00
|
|
|
modules.extend(modules::handle(module, context));
|
2020-07-07 22:45:32 +00:00
|
|
|
}
|
2022-12-28 20:00:49 +00:00
|
|
|
} else if module.starts_with("custom.") || module.starts_with("env_var.") {
|
|
|
|
// custom.<name> and env_var.<name> are special cases and handle disabled modules themselves
|
|
|
|
modules.extend(modules::handle(module, context));
|
|
|
|
} else if matches!(module, "custom" | "env_var") {
|
|
|
|
// env var is a spacial case and may contain a top-level module definition
|
|
|
|
if module == "env_var" {
|
|
|
|
modules.extend(modules::handle(module, context));
|
2020-07-07 22:45:32 +00:00
|
|
|
}
|
2022-12-28 20:00:49 +00:00
|
|
|
|
|
|
|
// Write out all custom modules, except for those that are explicitly set
|
|
|
|
for (child, config) in context
|
|
|
|
.config
|
|
|
|
.get_config(&[module])
|
2023-01-27 09:33:24 +00:00
|
|
|
.and_then(|config| config.as_table().map(toml::map::Map::iter))
|
2022-12-28 20:00:49 +00:00
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
{
|
|
|
|
// Some env var keys may be part of a top-level module definition
|
|
|
|
if module == "env_var" && !config.is_table() {
|
|
|
|
continue;
|
|
|
|
} else if should_add_implicit_module(module, child, config, module_list) {
|
|
|
|
modules.extend(modules::handle(&format!("{module}.{child}"), context));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-01-20 14:08:55 +00:00
|
|
|
modules
|
2020-01-02 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 20:00:49 +00:00
|
|
|
fn should_add_implicit_module(
|
|
|
|
parent_module: &str,
|
|
|
|
child_module: &str,
|
2020-04-11 16:37:24 +00:00
|
|
|
config: &toml::Value,
|
2020-07-07 22:45:32 +00:00
|
|
|
module_list: &BTreeSet<String>,
|
2020-04-11 16:37:24 +00:00
|
|
|
) -> bool {
|
2022-12-28 20:00:49 +00:00
|
|
|
let explicit_module_name = format!("{parent_module}.{child_module}");
|
2020-07-07 22:45:32 +00:00
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 19:45:27 +00:00
|
|
|
|
|
|
|
/// Return the modules from $all that are not already in the list
|
|
|
|
fn all_modules_uniq(module_list: &BTreeSet<String>) -> Vec<String> {
|
|
|
|
let mut prompt_order: Vec<String> = Vec::new();
|
|
|
|
for module in PROMPT_ORDER.iter() {
|
|
|
|
if !module_list.contains(*module) {
|
|
|
|
prompt_order.push(String::from(*module))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt_order
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load the correct formatter for the context (ie left prompt or right prompt)
|
|
|
|
/// and the list of all modules used in a format string
|
|
|
|
fn load_formatter_and_modules<'a>(context: &'a Context) -> (StringFormatter<'a>, BTreeSet<String>) {
|
2021-10-27 13:13:17 +00:00
|
|
|
let config = &context.root_config;
|
2023-01-12 03:25:36 +00:00
|
|
|
let (formatter, config_param) = match &context.target {
|
|
|
|
Target::Main => (StringFormatter::new(&config.format), "format".to_string()),
|
|
|
|
Target::Right => (
|
|
|
|
StringFormatter::new(&config.right_format),
|
|
|
|
"right_format".to_string(),
|
|
|
|
),
|
|
|
|
Target::Continuation => (
|
|
|
|
StringFormatter::new(&config.continuation_prompt),
|
|
|
|
"continuation_prompt".to_string(),
|
|
|
|
),
|
|
|
|
Target::Profile(name) => (
|
|
|
|
match config.profiles.get(name) {
|
|
|
|
Some(format) => StringFormatter::new(format),
|
|
|
|
_ => Err(StringFormatterError::Custom("Invalid Profile".to_string())),
|
|
|
|
},
|
|
|
|
format!("profile: {}", &name),
|
|
|
|
),
|
|
|
|
};
|
2021-09-08 19:45:27 +00:00
|
|
|
|
2021-10-27 13:13:17 +00:00
|
|
|
let rformatter = StringFormatter::new(&config.right_format);
|
2023-01-12 03:25:36 +00:00
|
|
|
|
|
|
|
if formatter.is_err() {
|
|
|
|
log::error!("Error parsing `{}`", config_param);
|
2021-09-08 19:45:27 +00:00
|
|
|
}
|
|
|
|
if rformatter.is_err() {
|
|
|
|
log::error!("Error parsing `right_format`")
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:25:36 +00:00
|
|
|
match (formatter, rformatter) {
|
|
|
|
(Ok(lf), Ok(rf)) => {
|
2021-09-08 19:45:27 +00:00
|
|
|
let mut modules: BTreeSet<String> = BTreeSet::new();
|
2022-01-04 09:49:42 +00:00
|
|
|
if context.target != Target::Continuation {
|
2022-01-01 13:12:11 +00:00
|
|
|
modules.extend(lf.get_variables());
|
|
|
|
modules.extend(rf.get_variables());
|
|
|
|
}
|
2023-01-12 03:25:36 +00:00
|
|
|
(lf, modules)
|
2021-09-08 19:45:27 +00:00
|
|
|
}
|
|
|
|
_ => (StringFormatter::raw(">"), BTreeSet::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 15:14:05 +00:00
|
|
|
#[cfg(feature = "config-schema")]
|
|
|
|
pub fn print_schema() {
|
|
|
|
let schema = schemars::schema_for!(crate::configs::FullConfig);
|
|
|
|
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
|
|
|
}
|
|
|
|
|
2022-08-02 09:51:15 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Preset(pub &'static str);
|
|
|
|
|
|
|
|
impl ValueEnum for Preset {
|
|
|
|
fn value_variants<'a>() -> &'a [Self] {
|
|
|
|
shadow::get_preset_list()
|
|
|
|
}
|
|
|
|
|
2022-09-29 21:51:56 +00:00
|
|
|
fn to_possible_value(&self) -> Option<PossibleValue> {
|
2022-08-02 09:51:15 +00:00
|
|
|
Some(PossibleValue::new(self.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 10:46:16 +00:00
|
|
|
pub fn preset_command(name: Option<Preset>, output: Option<PathBuf>, list: bool) {
|
2022-08-02 09:51:15 +00:00
|
|
|
if list {
|
|
|
|
println!("{}", preset_list());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let variant = name.expect("name argument must be specified");
|
2023-03-03 10:46:16 +00:00
|
|
|
let content = shadow::get_preset_content(variant.0);
|
|
|
|
if let Some(output) = output {
|
|
|
|
if let Err(err) = std::fs::write(output, content) {
|
|
|
|
eprintln!("Error writing preset to file: {err}");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
} else if let Err(err) = std::io::stdout().write_all(content) {
|
|
|
|
eprintln!("Error writing preset to stdout: {err}");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2022-08-02 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn preset_list() -> String {
|
|
|
|
Preset::value_variants()
|
|
|
|
.iter()
|
|
|
|
.map(|v| format!("{}\n", v.0))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:45:27 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use crate::test::default_context;
|
2023-03-03 10:46:16 +00:00
|
|
|
use crate::utils;
|
2021-09-08 19:45:27 +00:00
|
|
|
|
2023-01-12 03:25:36 +00:00
|
|
|
#[test]
|
|
|
|
fn main_prompt() {
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2023-01-12 03:25:36 +00:00
|
|
|
add_newline=false
|
|
|
|
format="$character"
|
|
|
|
[character]
|
|
|
|
format=">\n>"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
2023-01-12 03:25:36 +00:00
|
|
|
context.target = Target::Main;
|
|
|
|
|
|
|
|
let expected = String::from(">\n>");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:45:27 +00:00
|
|
|
#[test]
|
|
|
|
fn right_prompt() {
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2021-09-08 19:45:27 +00:00
|
|
|
right_format="$character"
|
|
|
|
[character]
|
|
|
|
format=">\n>"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
2022-01-04 09:49:42 +00:00
|
|
|
context.target = Target::Right;
|
2021-09-08 19:45:27 +00:00
|
|
|
|
|
|
|
let expected = String::from(">>"); // should strip new lines
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2022-01-01 13:12:11 +00:00
|
|
|
|
2023-01-12 03:25:36 +00:00
|
|
|
#[test]
|
|
|
|
fn custom_prompt() {
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2023-01-12 03:25:36 +00:00
|
|
|
add_newline = false
|
|
|
|
[profiles]
|
|
|
|
test="0_0$character"
|
|
|
|
[character]
|
|
|
|
format=">>"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
2023-01-12 03:25:36 +00:00
|
|
|
context.target = Target::Profile("test".to_string());
|
|
|
|
|
|
|
|
let expected = String::from("0_0>>");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_prompt_fallback() {
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2023-01-12 03:25:36 +00:00
|
|
|
add_newline=false
|
|
|
|
[profiles]
|
|
|
|
test="0_0$character"
|
|
|
|
[character]
|
|
|
|
format=">>"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
2023-01-12 03:25:36 +00:00
|
|
|
context.target = Target::Profile("wrong_prompt".to_string());
|
|
|
|
|
|
|
|
let expected = String::from(">");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2022-01-01 13:12:11 +00:00
|
|
|
#[test]
|
|
|
|
fn continuation_prompt() {
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2022-01-01 13:12:11 +00:00
|
|
|
continuation_prompt="><>"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
2022-01-04 09:49:42 +00:00
|
|
|
context.target = Target::Continuation;
|
2022-01-01 13:12:11 +00:00
|
|
|
|
|
|
|
let expected = String::from("><>");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
2022-04-01 15:14:05 +00:00
|
|
|
|
2022-08-02 09:51:15 +00:00
|
|
|
#[test]
|
|
|
|
fn preset_list_returns_one_or_more_items() {
|
|
|
|
assert!(preset_list().trim().split('\n').count() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn preset_command_does_not_panic_on_correct_inputs() {
|
2023-03-03 10:46:16 +00:00
|
|
|
preset_command(None, None, true);
|
2022-08-02 09:51:15 +00:00
|
|
|
Preset::value_variants()
|
|
|
|
.iter()
|
2023-03-03 10:46:16 +00:00
|
|
|
.for_each(|v| preset_command(Some(v.clone()), None, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn preset_command_output_to_file() -> std::io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
let path = dir.path().join("preset.toml");
|
|
|
|
preset_command(Some(Preset("nerd-font-symbols")), Some(path.clone()), false);
|
|
|
|
|
|
|
|
let actual = utils::read_file(&path)?;
|
|
|
|
let expected = include_str!("../docs/.vuepress/public/presets/toml/nerd-font-symbols.toml");
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
|
|
|
|
dir.close()
|
2022-08-02 09:51:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 15:14:05 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "config-schema")]
|
|
|
|
fn print_schema_does_not_panic() {
|
|
|
|
print_schema();
|
|
|
|
}
|
2022-12-28 20:00:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_expands() -> std::io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2022-12-28 20:00:49 +00:00
|
|
|
format="$custom"
|
|
|
|
[custom.a]
|
|
|
|
when=true
|
|
|
|
format="a"
|
|
|
|
[custom.b]
|
|
|
|
when=true
|
|
|
|
format="b"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
|
|
|
context.current_dir = dir.path().to_path_buf();
|
2022-12-28 20:00:49 +00:00
|
|
|
|
|
|
|
let expected = String::from("\nab");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_expands() {
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2022-12-28 20:00:49 +00:00
|
|
|
format="$env_var"
|
|
|
|
[env_var]
|
|
|
|
format="$env_value"
|
|
|
|
variable = "a"
|
|
|
|
[env_var.b]
|
|
|
|
format="$env_value"
|
|
|
|
[env_var.c]
|
|
|
|
format="$env_value"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
2022-12-28 20:00:49 +00:00
|
|
|
context.env.insert("a", "a".to_string());
|
|
|
|
context.env.insert("b", "b".to_string());
|
|
|
|
context.env.insert("c", "c".to_string());
|
|
|
|
|
|
|
|
let expected = String::from("\nabc");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_mixed() -> std::io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2022-12-28 20:00:49 +00:00
|
|
|
format="${custom.c}$custom${custom.b}"
|
|
|
|
[custom.a]
|
|
|
|
when=true
|
|
|
|
format="a"
|
|
|
|
[custom.b]
|
|
|
|
when=true
|
|
|
|
format="b"
|
|
|
|
[custom.c]
|
|
|
|
when=true
|
|
|
|
format="c"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
|
|
|
context.current_dir = dir.path().to_path_buf();
|
2022-12-28 20:00:49 +00:00
|
|
|
|
|
|
|
let expected = String::from("\ncab");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_mixed() {
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2022-12-28 20:00:49 +00:00
|
|
|
format="${env_var.c}$env_var${env_var.b}"
|
|
|
|
[env_var]
|
|
|
|
format="$env_value"
|
|
|
|
variable = "d"
|
|
|
|
[env_var.a]
|
|
|
|
format="$env_value"
|
|
|
|
[env_var.b]
|
|
|
|
format="$env_value"
|
|
|
|
[env_var.c]
|
|
|
|
format="$env_value"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
2022-12-28 20:00:49 +00:00
|
|
|
context.env.insert("a", "a".to_string());
|
|
|
|
context.env.insert("b", "b".to_string());
|
|
|
|
context.env.insert("c", "c".to_string());
|
|
|
|
context.env.insert("d", "d".to_string());
|
|
|
|
|
|
|
|
let expected = String::from("\ncdab");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_subset() -> std::io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2022-12-28 20:00:49 +00:00
|
|
|
format="${custom.b}"
|
|
|
|
[custom.a]
|
|
|
|
when=true
|
|
|
|
format="a"
|
|
|
|
[custom.b]
|
|
|
|
when=true
|
|
|
|
format="b"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
|
|
|
context.current_dir = dir.path().to_path_buf();
|
2022-12-28 20:00:49 +00:00
|
|
|
|
|
|
|
let expected = String::from("\nb");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_missing() -> std::io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
2023-04-24 14:03:47 +00:00
|
|
|
let mut context = default_context().set_config(toml::toml! {
|
2022-12-28 20:00:49 +00:00
|
|
|
format="${custom.b}"
|
|
|
|
[custom.a]
|
|
|
|
when=true
|
|
|
|
format="a"
|
2023-04-24 14:03:47 +00:00
|
|
|
});
|
|
|
|
context.current_dir = dir.path().to_path_buf();
|
2022-12-28 20:00:49 +00:00
|
|
|
|
|
|
|
let expected = String::from("\n");
|
|
|
|
let actual = get_prompt(context);
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
2021-09-08 19:45:27 +00:00
|
|
|
}
|