fix(rprompt): remove lprompt modules from `$all` again (#5067)

This commit is contained in:
David Knaack 2023-07-06 11:32:17 +02:00 committed by GitHub
parent 35241bad92
commit b9a4b08ac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 95 additions and 37 deletions

View File

@ -12,7 +12,6 @@ use unicode_width::UnicodeWidthChar;
use crate::configs::PROMPT_ORDER; use crate::configs::PROMPT_ORDER;
use crate::context::{Context, Properties, Shell, Target}; use crate::context::{Context, Properties, Shell, Target};
use crate::formatter::string_formatter::StringFormatterError;
use crate::formatter::{StringFormatter, VariableHolder}; use crate::formatter::{StringFormatter, VariableHolder};
use crate::module::Module; use crate::module::Module;
use crate::module::ALL_MODULES; use crate::module::ALL_MODULES;
@ -405,43 +404,65 @@ fn all_modules_uniq(module_list: &BTreeSet<String>) -> Vec<String> {
/// and the list of all modules used in a format string /// and the list of all modules used in a format string
fn load_formatter_and_modules<'a>(context: &'a Context) -> (StringFormatter<'a>, BTreeSet<String>) { fn load_formatter_and_modules<'a>(context: &'a Context) -> (StringFormatter<'a>, BTreeSet<String>) {
let config = &context.root_config; let config = &context.root_config;
let (formatter, config_param) = match &context.target {
Target::Main => (StringFormatter::new(&config.format), "format".to_string()), if context.target == Target::Continuation {
Target::Right => ( let cf = &config.continuation_prompt;
StringFormatter::new(&config.right_format), let formatter = StringFormatter::new(cf);
"right_format".to_string(), return match formatter {
), Ok(f) => {
Target::Continuation => ( let modules = f.get_variables().into_iter().collect();
StringFormatter::new(&config.continuation_prompt), (f, modules)
"continuation_prompt".to_string(), }
), Err(e) => {
Target::Profile(name) => ( log::error!("Error parsing continuation prompt: {e}");
match config.profiles.get(name) { (StringFormatter::raw(">"), BTreeSet::new())
Some(format) => StringFormatter::new(format), }
_ => Err(StringFormatterError::Custom("Invalid Profile".to_string())), };
}, }
format!("profile: {}", &name),
), let (left_format_str, right_format_str): (&str, &str) = match context.target {
Target::Main | Target::Right => (&config.format, &config.right_format),
Target::Profile(ref name) => {
if let Some(lf) = config.profiles.get(name) {
(lf, "")
} else {
log::error!("Profile {name:?} not found");
return (StringFormatter::raw(">"), BTreeSet::new());
}
}
Target::Continuation => unreachable!("Continuation prompt should have been handled above"),
}; };
let rformatter = StringFormatter::new(&config.right_format); let lf = StringFormatter::new(left_format_str);
let rf = StringFormatter::new(right_format_str);
if formatter.is_err() { if let Err(ref e) = lf {
log::error!("Error parsing `{}`", config_param); let name = if let Target::Profile(ref profile_name) = context.target {
} format!("profile.{profile_name}")
if rformatter.is_err() { } else {
log::error!("Error parsing `right_format`") "format".to_string()
};
log::error!("Error parsing {name:?}: {e}");
};
if let Err(ref e) = rf {
log::error!("Error parsing right_format: {e}");
} }
match (formatter, rformatter) { let modules = [&lf, &rf]
(Ok(lf), Ok(rf)) => { .into_iter()
let mut modules: BTreeSet<String> = BTreeSet::new(); .flatten()
if context.target != Target::Continuation { .flat_map(|f| f.get_variables())
modules.extend(lf.get_variables()); .collect();
modules.extend(rf.get_variables());
} let main_formatter = match context.target {
(lf, modules) Target::Main | Target::Profile(_) => lf,
} Target::Right => rf,
Target::Continuation => unreachable!("Continuation prompt should have been handled above"),
};
match main_formatter {
Ok(f) => (f, modules),
_ => (StringFormatter::raw(">"), BTreeSet::new()), _ => (StringFormatter::raw(">"), BTreeSet::new()),
} }
} }
@ -526,13 +547,50 @@ mod test {
} }
#[test] #[test]
fn custom_prompt() { fn prompt_with_all() -> io::Result<()> {
let mut context = default_context().set_config(toml::toml! { let mut context = default_context().set_config(toml::toml! {
add_newline = false add_newline = false
[profiles] right_format= "$directory$line_break"
test="0_0$character" format="$all"
[character] [character]
format=">>" format=">"
});
let dir = tempfile::tempdir().unwrap();
context.current_dir = dir.path().to_path_buf();
let expected = String::from(">");
let actual = get_prompt(context);
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn rprompt_with_all() -> io::Result<()> {
let mut context = default_context().set_config(toml::toml! {
format= "$directory$line_break"
right_format="$all"
[character]
format=">"
});
let dir = tempfile::tempdir().unwrap();
context.current_dir = dir.path().to_path_buf();
context.target = Target::Right;
let expected = String::from(">");
let actual = get_prompt(context);
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn custom_prompt() {
let mut context = default_context().set_config(toml::toml! {
add_newline = false
[profiles]
test="0_0$character"
[character]
format=">>"
}); });
context.target = Target::Profile("test".to_string()); context.target = Target::Profile("test".to_string());