make prompt colors configurable via GOSH_COLORS
- GOSH_COLORS='user=..:root=..:path=..' with ANSI SGR codes, empty value disables the color for that part - only digits and ';' are accepted, invalid entries are ignored - read before every prompt, so 'set GOSH_COLORS=...' takes effect immediately - document in help and README, including example color schemes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7afcecd053
commit
2b8f06ea6d
3 files changed
+155
-20
No files matched your search
@@ -309,31 +309,82 @@ func getPrompt() string {
|
||||
wd = "~" + strings.TrimPrefix(wd, home)
|
||||
}
|
||||
|
||||
return buildPrompt(username, hostname, wd, os.Geteuid() == 0, useColor())
|
||||
var colors *promptColors
|
||||
if useColor() {
|
||||
c := parseColors(os.Getenv("GOSH_COLORS"))
|
||||
colors = &c
|
||||
}
|
||||
return buildPrompt(username, hostname, wd, os.Geteuid() == 0, colors)
|
||||
}
|
||||
|
||||
// ANSI escape sequences used for the prompt
|
||||
const (
|
||||
colorReset = "\x1b[0m"
|
||||
colorUser = "\x1b[1;32m" // bold green
|
||||
colorRoot = "\x1b[1;31m" // bold red
|
||||
colorPath = "\x1b[1;34m" // bold blue
|
||||
)
|
||||
// promptColors holds the SGR parameters (e.g. "1;32") for the parts of the
|
||||
// prompt. An empty value means no color for that part.
|
||||
type promptColors struct {
|
||||
user string // user@host
|
||||
root string // user@host when running as root
|
||||
path string // working directory
|
||||
}
|
||||
|
||||
// defaultColors: bold green user, bold red root, bold blue directory
|
||||
var defaultColors = promptColors{user: "1;32", root: "1;31", path: "1;34"}
|
||||
|
||||
// parseColors reads a GOSH_COLORS value like "user=1;33:root=1;31:path=36"
|
||||
// and overrides the corresponding defaults. Unknown keys and values that are
|
||||
// not plain SGR parameters (digits and ';') are ignored, so the variable
|
||||
// cannot be used to write arbitrary escape sequences to the terminal.
|
||||
func parseColors(spec string) promptColors {
|
||||
colors := defaultColors
|
||||
for _, entry := range strings.Split(spec, ":") {
|
||||
key, value, ok := strings.Cut(entry, "=")
|
||||
if !ok || !isSGR(value) {
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "user":
|
||||
colors.user = value
|
||||
case "root":
|
||||
colors.root = value
|
||||
case "path":
|
||||
colors.path = value
|
||||
}
|
||||
}
|
||||
return colors
|
||||
}
|
||||
|
||||
// isSGR reports whether s consists only of digits and ';'. The empty string
|
||||
// is allowed and disables the color.
|
||||
func isSGR(s string) bool {
|
||||
for _, r := range s {
|
||||
if (r < '0' || r > '9') && r != ';' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// colorize wraps text in the SGR sequence for sgr, or returns it unchanged if
|
||||
// sgr is empty.
|
||||
func colorize(text, sgr string) string {
|
||||
if sgr == "" {
|
||||
return text
|
||||
}
|
||||
return "\x1b[" + sgr + "m" + text + "\x1b[0m"
|
||||
}
|
||||
|
||||
// buildPrompt formats the prompt as user@host:path $. The parts are sanitized
|
||||
// before the color codes are added, so only our own escape sequences end up
|
||||
// in the prompt.
|
||||
func buildPrompt(username, hostname, path string, root, color bool) string {
|
||||
// in the prompt. colors == nil disables colors.
|
||||
func buildPrompt(username, hostname, path string, root bool, colors *promptColors) string {
|
||||
userHost := sanitize(username + "@" + hostname)
|
||||
path = sanitize(path)
|
||||
if !color {
|
||||
if colors == nil {
|
||||
return userHost + ":" + path + " $ "
|
||||
}
|
||||
userColor := colorUser
|
||||
userColor := colors.user
|
||||
if root {
|
||||
userColor = colorRoot
|
||||
userColor = colors.root
|
||||
}
|
||||
return userColor + userHost + colorReset + ":" + colorPath + path + colorReset + " $ "
|
||||
return colorize(userHost, userColor) + ":" + colorize(path, colors.path) + " $ "
|
||||
}
|
||||
|
||||
// useColor reports whether the prompt should be colored: stdout must be a
|
||||
@@ -698,6 +749,8 @@ Features:
|
||||
- Tab completion for commands and file paths
|
||||
- Tilde expansion (~, ~/path) at the start of unquoted words
|
||||
- Colored prompt (user@host green, red for root; directory blue),
|
||||
configurable with $GOSH_COLORS, e.g. GOSH_COLORS='user=1;33:path=36'
|
||||
(keys user, root, path; values are ANSI SGR codes, empty = no color);
|
||||
disabled with $NO_COLOR, TERM=dumb or when output is not a terminal
|
||||
- Environment variable expansion ($VAR, ${VAR}, ${VAR:-default});
|
||||
using an unset variable is an error, use ${VAR:-} for an empty default`)
|
||||
|
||||
Reference in new issue
Block a user