mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 18:32:28 -05:00
Shell: Build prompt based on the PROMPT environment variable if present
This commit is contained in:
parent
1828d9eadd
commit
8262c1e662
1 changed files with 42 additions and 4 deletions
|
@ -55,12 +55,50 @@ static int run_command(const String&);
|
|||
|
||||
static String prompt()
|
||||
{
|
||||
if (g.uid == 0)
|
||||
return "# ";
|
||||
auto* ps1 = getenv("PROMPT");
|
||||
if (!ps1) {
|
||||
if (g.uid == 0)
|
||||
return "# ";
|
||||
|
||||
StringBuilder builder;
|
||||
builder.appendf("\033]0;%s@%s:%s\007", g.username.characters(), g.hostname, g.cwd.characters());
|
||||
builder.appendf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", g.username.characters(), g.hostname, g.cwd.characters());
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
StringBuilder builder;
|
||||
builder.appendf("\033]0;%s@%s:%s\007", g.username.characters(), g.hostname, g.cwd.characters());
|
||||
builder.appendf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", g.username.characters(), g.hostname, g.cwd.characters());
|
||||
for (char* ptr = ps1; *ptr; ++ptr) {
|
||||
if (*ptr == '\\') {
|
||||
++ptr;
|
||||
if (!*ptr)
|
||||
break;
|
||||
switch (*ptr) {
|
||||
case 'X':
|
||||
builder.append("\033]0;");
|
||||
break;
|
||||
case 'a':
|
||||
builder.append(0x07);
|
||||
break;
|
||||
case 'e':
|
||||
builder.append(0x1b);
|
||||
break;
|
||||
case 'u':
|
||||
builder.append(g.username);
|
||||
break;
|
||||
case 'h':
|
||||
builder.append(g.hostname);
|
||||
break;
|
||||
case 'w':
|
||||
builder.append(g.cwd);
|
||||
break;
|
||||
case 'p':
|
||||
builder.append(g.uid == 0 ? '#' : '$');
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
builder.append(*ptr);
|
||||
}
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue