show the exit status of a failed command in the prompt

- user@host:dir [N] $ after a command failed with exit status N,
  the plain prompt again after a successful command
- the status is colored bold red by default, configurable with the new
  GOSH_COLORS key error

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Florian WaltherandClaude Opus 5.5 committed 2026-10-03 23:46:11 +02:00
1 parent b3311cbf94
commit 041406a340
3 files changed
+86 -28

No files matched your search

+30 -15
View File
@@ -612,21 +612,23 @@ func getPrompt() string {
c := parseColors(os.Getenv("GOSH_COLORS"))
colors = &c
}
return buildPrompt(username, hostname, wd, os.Geteuid() == 0, colors)
return buildPrompt(username, hostname, wd, os.Geteuid() == 0, lastStatus, colors)
}
// 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
user string // user@host
root string // user@host when running as root
path string // working directory
error string // exit status of the last command, if it failed
}
// defaultColors: bold green user, bold red root, bold blue directory
var defaultColors = promptColors{user: "1;32", root: "1;31", path: "1;34"}
// defaultColors: bold green user, bold red root, bold blue directory, bold
// red exit status
var defaultColors = promptColors{user: "1;32", root: "1;31", path: "1;34", error: "1;31"}
// parseColors reads a GOSH_COLORS value like "user=1;33:root=1;31:path=36"
// parseColors reads a GOSH_COLORS value like "user=1;33:path=36:error=31"
// 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.
@@ -644,6 +646,8 @@ func parseColors(spec string) promptColors {
colors.root = value
case "path":
colors.path = value
case "error":
colors.error = value
}
}
return colors
@@ -669,20 +673,28 @@ func colorize(text, sgr string) string {
return "\x1b[" + sgr + "m" + text + "\x1b[0m"
}
// buildPrompt formats the prompt as user@host:path $. The parts are sanitized
// buildPrompt formats the prompt as user@host:path $, or user@host:path [N] $
// if the last command failed with exit status N. The parts are sanitized
// before the color codes are added, so only our own escape sequences end up
// in the prompt. colors == nil disables colors.
func buildPrompt(username, hostname, path string, root bool, colors *promptColors) string {
func buildPrompt(username, hostname, path string, root bool, status int, colors *promptColors) string {
userHost := sanitize(username + "@" + hostname)
path = sanitize(path)
var failed string
if status != 0 {
failed = " [" + strconv.Itoa(status) + "]"
}
if colors == nil {
return userHost + ":" + path + " $ "
return userHost + ":" + path + failed + " $ "
}
userColor := colors.user
if root {
userColor = colors.root
}
return colorize(userHost, userColor) + ":" + colorize(path, colors.path) + " $ "
if failed != "" {
failed = " " + colorize(failed[1:], colors.error)
}
return colorize(userHost, userColor) + ":" + colorize(path, colors.path) + failed + " $ "
}
// useColor reports whether the prompt should be colored: stdout must be a
@@ -1793,10 +1805,13 @@ Features:
- Wildcards: * ? [abc] [a-z] [!abc] match file names (not hidden files
unless the pattern starts with a dot); quoted wildcards and values of
variables are not expanded, a pattern without match is kept as is
- 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
- Prompt user@host:dir $, with [N] before the $ if the last command
failed with exit status N
- Colored prompt (user@host green, red for root; directory blue; exit
status red), configurable with $GOSH_COLORS, e.g.
GOSH_COLORS='user=1;33:path=36' (keys user, root, path, error; 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
- Command lists: a ; b (run both), a && b (b only if a succeeded),