git.sophuwu.com > bashprompt
added fmt so the prompt is now customisable
updated documentation
sophuwu sophie@sophuwu.com
Sat, 20 Sep 2025 00:45:19 +0200
commit

23dd0775272b58ffbdbce728231bb405b120771b

parent

caae1beb7ce691eedd9dfa91d6b16b063974b041

3 files changed, 138 insertions(+), 20 deletions(-)

jump to
M README.mdREADME.md

@@ -41,8 +41,31 @@

## Options: Environment variables can be set to customize the prompt. -* `IPCOLOR=alt` IP address will be colored with an alternate color palette. -* `IPCOLOR=none` IP address and username will not be printed. +### `IPCOLOR` +* Controls how the IP address is printed. +* `IPCOLOR=""` IP address will be colored with the default color palette. +* `IPCOLOR="alt"` IP address will be colored with an alternate color palette. +* `IPCOLOR="none"` IP address will not be printed. + +### `PWDLEN` +* Controls the length of the printed working directory. +* `PWDLEN=0` Full path. +* `PWDLEN=1` Only the current directory. +* `PWDLEN=2` Current directory and its parent. + +### `RBPSFMT` +* Controls the format of the prompt. +* Only valid format specifiers will be printed. Strings or invalid specifiers will be ignored. +* Default: `"%i %? %u %l %e %d"` +* Certain specifiers allow custom colours, to use them, put a number 0-255 after the `%` in the form `fg.bg` (`%32.240u`, `%.42l`, `%128d`). +* Format specifiers: + * `%i` - IP address (4 blocks of color) + * `%?` - Exit code of last command + * `%u` - Username + * `%h` - Hostname + * `%l` - Line number + * `%e` - Random emote (:D, :P, etc.) + * `%d` - Current working directory (shortened according to `PWDLEN`) ## Preview
M releaser/bash-rb-ps1.1releaser/bash-rb-ps1.1

@@ -29,7 +29,7 @@ .TP

.B IPCOLOR changes the color palette used for the IP address. If set to `alt`, it uses an alternative color palette. - If set to `none`, the IP address block and username will be removed from the prompt. + If set to `none`, the IP address block will be removed from the prompt. If unset or set to any other value, the default color palette will be used. .TP .B IPCmd

@@ -42,7 +42,19 @@ .TP

.B IPStr manually sets the IPv4 address to a specific value. If set, the IPCmd will not be executed, and the value of IPStr will be used instead. - +.TP +.B RBPSFMT +specifies a custom format for the rainbow prompt. + The format can include the following placeholders: + \fI%u\fR - the current user + \fI%h\fR - the machine hostname + \fI%i\fR - the current IP address (if available and not disabled) + \fI%d\fR - the current working directory, formatted according to PWDLEN + \fI%e\fR - a random emote (:D, :), :P, etc.) + \fI%?\fR - the exit status of the last command + \fI%l\fR - bash line number + If unset, it defaults to `%i %? %u %l %e %d`. + XTerm 256 colour values can be put after the `%` to set colors: \fI%fg.bg\fR .SH EXAMPLES .TP To enable the rainbow prompt, run:
M src/prompt.cppsrc/prompt.cpp

@@ -97,6 +97,7 @@ else n = siz - n;

for (esc.rain(" "+pwd); n < siz; n++) { esc.rain(" "+parts[n]); } + esc.output += " "; } void checkBash() {

@@ -110,6 +111,93 @@ exit(1);

} } +int validPart(char &part) { + const str parts = "i?uhlde"; + for (int i = 0; i < parts.length(); i++) { + if (part == parts[i]) return 1; + } + return 0; +} + +// return 1 on success, 0 on failure +int parseFmtPart(str &fmt, int &i, char &part, str &fg, str &bg) { + if (i >= fmt.length() || fmt[i] != '%') return 0; + fg = ""; + bg = ""; + int v = 0; + str *col = &fg; + for (i++; i < fmt.length(); i++) { + part = fmt[i]; + if (part == '.') { + if (col == &bg) return 0; // only one dot allowed + v = 0; + col = &bg; + continue; + } + if (part >= '0' && part <= '9') { + *col += part; + v = v * 10 + (int)(part - '0'); + if (v > 255) return 0; // color out of range + continue; + } + return validPart(part); + } + return 0; +} + +int doIP = 0; + +void addIP(escape &esc) { + if (!doIP) return; + IP ip; + if (ip.get()) esc.output+=ip.toColor()+" "; +} + +int lineno ; + +void addPart(escape &esc, char &part, str &fg, str &bg) { + str s; + switch (part) { + case 'i': + addIP(esc); + return; + case 'd': + getpwd(esc); + return; + case '?': + if (fg == "" && bg == "") fg = "202"; + s = "\\${?}"; + break; + case 'u': + s = envorcmd("USER", "whoami"); + break; + case 'h': + s = envorcmd("HOSTNAME", "hostname"); + break; + case 'l': + s = std::to_string(lineno); + break; + case 'e': + s = emote(); + break; + default: + return; + } + if (fg == "" && bg == "") esc.rain(s); + else esc.add(s, fg, bg); + esc.output += " "; +} + +str getFMT() { + const char* ipcol = getenv("IPCOLOR"); + if (ipcol == NULL || !(std::string(ipcol)=="none"||std::string(ipcol)=="NONE")) doIP = 1; + const char* fmtt = getenv("RBPSFMT"); + if (fmtt == NULL) { + if (doIP) return "%i%?%u%l%e%d"; + return "%?%u%h%l%e%d"; + } + return std::string(fmtt); +} int main(int argc,char** argv) { checkBash();

@@ -131,26 +219,21 @@ printf("%s\n%s\n",ip.toString().c_str(),ip.toColor().c_str());

return 0; } } - int lineno = intenv("LINENO"); + lineno = intenv("LINENO"); if (lineno == 0)printf("%s\n","export LINENO"); PS1.r = rain(lineno); - str user=" "; - const char* ipcol = getenv("IPCOLOR"); - if (ipcol == NULL || !(std::string(ipcol)=="none"||std::string(ipcol)=="NONE")) { - user = envorcmd("USER", "whoami"); - user = " " + user + " "; - IP ip; - if (ip.get()) PS1.output+=ip.toColor()+" "; + str fmt = getFMT(); + + char part; + str fg, bg; + for (int i = 0; i < fmt.length();i++) { + part = fmt[i]; + if (parseFmtPart(fmt, i, part, fg, bg)) + addPart(PS1, part, fg, bg); } - - - PS1.add("\\${?}", "202"); - PS1.rain(user); - PS1.rain(std::to_string(lineno)+" "); - PS1.rain(emote()); - getpwd(PS1); - PS1.rain(" $ "); + PS1.rain("$"); + PS1.output += " "; PS1.set(); return 0;