Shell: Refactor Shell::prompt to use GenericLexer to read the PROMPT var

This commit is contained in:
Adam Harald Jørgensen 2023-10-23 22:34:31 +02:00 committed by Ali Mohammad Pur
parent 1d26550370
commit 78b3703586
Notes: sideshowbarker 2024-07-17 03:14:39 +09:00

View file

@ -82,8 +82,7 @@ DeprecatedString Shell::prompt() const
if (m_next_scheduled_prompt_text.has_value())
return m_next_scheduled_prompt_text.release_value();
auto build_prompt = [&]() -> DeprecatedString {
auto* ps1 = getenv("PROMPT");
auto const* ps1 = getenv("PROMPT");
if (!ps1) {
if (uid == 0)
return "# ";
@ -95,49 +94,46 @@ DeprecatedString Shell::prompt() const
}
StringBuilder builder;
for (char* ptr = ps1; *ptr; ++ptr) {
if (*ptr == '\\') {
++ptr;
if (!*ptr)
GenericLexer lexer { { ps1, strlen(ps1) } };
while (!lexer.is_eof()) {
builder.append(lexer.consume_until('\\'));
if (!lexer.consume_specific('\\') || lexer.is_eof())
break;
switch (*ptr) {
case 'X':
if (lexer.consume_specific('X')) {
builder.append("\033]0;"sv);
break;
case 'a':
} else if (lexer.consume_specific('a')) {
builder.append(0x07);
break;
case 'e':
} else if (lexer.consume_specific('e')) {
builder.append(0x1b);
break;
case 'u':
} else if (lexer.consume_specific('u')) {
builder.append(username);
break;
case 'h':
} else if (lexer.consume_specific('h')) {
builder.append({ hostname, strlen(hostname) });
break;
case 'w': {
DeprecatedString home_path = getenv("HOME");
} else if (lexer.consume_specific('w')) {
DeprecatedString const home_path = getenv("HOME");
if (cwd.starts_with(home_path)) {
builder.append('~');
builder.append(cwd.substring_view(home_path.length(), cwd.length() - home_path.length()));
} else {
builder.append(cwd);
}
break;
}
case 'p':
} else if (lexer.consume_specific('p')) {
builder.append(uid == 0 ? '#' : '$');
break;
} else {
lexer.consume();
}
continue;
}
builder.append(*ptr);
}
return builder.to_deprecated_string();
};
return build_prompt();
}
DeprecatedString Shell::expand_tilde(StringView expression)