1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:47:35 +00:00

Shell: Shorten prompt path if integer is provided with "\w" option

This commit is contained in:
Adam Harald Jørgensen 2023-11-02 19:08:24 +01:00 committed by Ali Mohammad Pur
parent 52a55fdd6d
commit 4febd31dba
2 changed files with 28 additions and 1 deletions

View file

@ -127,6 +127,33 @@ DeprecatedString Shell::prompt() const
builder.append(cwd);
}
} else if (auto const number_string = lexer.consume_while(is_ascii_digit); !number_string.is_empty()) {
if (lexer.is_eof() || lexer.consume() != 'w')
continue;
auto const max_component_count = number_string.to_uint().value();
DeprecatedString const home_path = getenv("HOME");
auto const should_collapse_path = cwd.starts_with(home_path);
auto const path = should_collapse_path ? cwd.substring_view(home_path.length(), cwd.length() - home_path.length())
: cwd.view();
auto const parts = path.split_view('/');
auto const start_index = (max_component_count < parts.size()) ? parts.size() - max_component_count : 0;
if (start_index == 0) {
if (should_collapse_path)
builder.append('~');
builder.append(path);
continue;
}
for (auto i = start_index; i < parts.size(); ++i) {
if (i != start_index)
builder.append('/');
builder.append(parts[i]);
}
} else if (lexer.consume_specific('p')) {
builder.append(uid == 0 ? '#' : '$');