oh-my-posh: exitcode segment does not work correctly
Code of Conduct
- I agree to follow this project’s Code of Conduct
What happened?
My prompt is using the exit segment with always_enabled = false. OMP fails to recognize that the last executed command failed and does not render the segment correctly.
I am using the following script to simulate failure:
param($code=99)
echo Hello!
$global:TestError=$code
exit $code
When I run the following, I get the expected result:
PS> .\test.ps1; echo $?
Hello!
False
When I run:
PS > .\test.ps1
OMP fails to render the exit segment. I have also tested this with various other failing utilities with the same result. Documentation clear states, that $? should work as assumed, i.e. $? evaluates to False if $LASTEXITCODE is non-zero. Even though I am getting situations where $? in the Prompt function is True and $LASTEXITCODE is nonzero. I have attached my experimental OMP module for reference.
With experimentation, I found out that the problem is in the Prompt function generated by OMP. For some reason, $? always evaluates to $true.
When I add the following statement (manually) at the beginning of the generated config, I get the correct result.
[ScriptBlock]$Prompt = {
#store if the last command was successful
$lastCommandSuccess = $?
#store the last exit code for restore
$realLASTEXITCODE = $global:LASTEXITCODE
$lastCommandSuccess = $lastCommandSuccess -and ($realLASTEXITCODE -eq 0)
My entire OMP init test module here: test-omp-init.ps1.txt
Why the $? does not work eludes me. Even though my GO is a bit rusty, I am happy to submit a pull request.
Theme
{ “$schema”: “https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json”,
“palette”: { “runtimeVersion”: “lightGray”, “runtimeVersionBackground”: “black” },
“console_title”: true, “console_title_style”: “template”, “console_title_template”: “{{.UserName}} {{.Shell}} in {{.PWD}}”, “final_space”: false, “osc99”: true, “blocks”: [ { “type”: “prompt”, “alignment”: “left”, “segments”: [ { “type”: “session”, “style”: “plain”, “properties”: { “prefix”: “╭─[”, “postfix”: “]─”, “template”: “<#ffff55>{{ .UserName }}</>” } }, { “type”: “path”, “style”: “plain”, “foreground”: “#ff5555”, “properties”: { “prefix”: “<#ffffff>[</>”, “postfix”: “<#ffffff>]</>”, “style”: “folder”, “enable_hyperlink”: true } }, { “type”: “root”, “style”: “plain”, “foreground”: “#FF9248”, “properties”: { “prefix”: “<#ffffff>─(</>”, “postfix”: “<#ffffff>)</>”, “root_icon”: “#” } }, { “type”: “executiontime”, “style”: “plain”, “foreground”: “yellow”, “background”: “black”, “properties”: { “style”: “austin”, “threshold”: 500, “prefix”: “<#fefefe>─[\ufbab</>”, “postfix”: “<#fefefe>]</>” } } ] }, { “type”: “prompt”, “alignment”: “right”, “segments”: [ { “type”: “kubectl”, “style”: “plain”, “foreground”: “p:runtimeVersion”, “background”: “p:runtimeVersionBackground”, “properties”: { “prefix”: "(\uFD31 ", “postfix”: “)”, “template”: “{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}” } }, { “type”: “dotnet”, “style”: “plain”, “foreground”: “p:runtimeVersion”, “background”: “p:runtimeVersionBackground”, “properties”: { “prefix”: “(.net v”, “postfix”: “)”, “home_enabled”: true, “display_mode”: “files”, “display_version”: true } } ] }, { “type”: “prompt”, “alignment”: “left”, “newline”: true, “segments”: [ { “type”: “text”, “style”: “plain”, “properties”: { “prefix”: “”, “postfix”: “”, “text”: “╰─” } }, { “type”: “git”, “style”: “plain”, “foreground”: “#e0f8ff”, “properties”: { “prefix”: “<#ffffff>[</>”, “postfix”: “<#ffffff>]</>”, “branch_icon”: "\ue725 ", “fetch_status”: true, “fetch_upstream_icon”: true, “template”: “{{ .HEAD }}{{ if .Staging.Changed }}<#00AA00> ● {{ .Staging.String }}</>{{ end }}{{ if .Working.Changed }}<#D75F00> ● {{ .Working.String }}</>{{ end }}” } }, { “type”: “exit”, “style”: “plain”, “foreground”: “#ecf7fa”, “background”: “black”, “properties”: { “prefix”: “<#fefefe>─[</>”, “postfix”: “<#fefefe>]</>”, “always_enabled”: false, “template”: “<#e91e63>{{ if gt .Code 0 }}!{{ end }}{{ .Code }}</>” } }, { “type”: “text”, “style”: “plain”, “foreground”: “#fefefe”, “background”: “black”, “properties”: { “prefix”: “”, “text”: “\ue285ff” } } ] } ] }
What OS are you seeing the problem on?
Windows
Which shell are you using?
powershell
Log output
[1mVersion:[0m 6.49.2
[1mSegments:[0m
ConsoleTitle(true) - 0 ms -
session(true) - 0 ms - [38;2;195;134;241m[0m[48;2;195;134;241m[38;2;255;255;255m [0m[48;2;195;134;241m[38;2;255;255;255mondra-pragmax@GOTHMOG[0m[48;2;195;134;241m[38;2;255;255;255m [0m[38;2;195;134;241m[0m
path(true) - 0 ms - [38;2;255;71;156;49m[7m[0m[48;2;255;71;156m[38;2;255;255;255m [0m[48;2;255;71;156m[38;2;255;255;255moh-my-posh[0m[48;2;255;71;156m[38;2;255;255;255m [0m[38;2;255;71;156m[0m
git(true) - 3 ms - [48;2;255;251;56m[38;2;255;71;156m[0m[48;2;255;251;56m[38;2;25;53;73m [0m[48;2;255;251;56m[38;2;25;53;73mmaster[0m[48;2;255;251;56m[38;2;25;53;73m [0m[38;2;255;251;56m[0m
battery(false) - 1 ms -
node(false) - 0 ms -
shell(true) - 0 ms - [48;2;0;119;194m[38;2;255;251;56m[0m[48;2;0;119;194m[38;2;255;255;255m ﲵ [0m[48;2;0;119;194m[38;2;255;255;255muni[0m[48;2;0;119;194m[38;2;255;255;255m [0m[38;2;0;119;194m[0m
root(false) - 0 ms -
text(true) - 0 ms - [48;2;255;255;255m[38;2;0;119;194m[0m[48;2;255;255;255m[38;2;17;17;17m [0m[48;2;255;255;255m[38;2;17;17;17mNO CONFIG[0m[48;2;255;255;255m[38;2;17;17;17m [0m[38;2;255;255;255m[0m
exit(true) - 0 ms - [38;2;255;255;255m[0m[38;2;46;149;153;49m[7m[0m[48;2;46;149;153m[38;2;255;255;255m [0m[48;2;46;149;153m[38;2;255;255;255m [0m[38;2;46;149;153m[0m
[1mRun duration:[0m 6.7795ms
[1mLogs:[0m
2022/01/25 17:24:57 debug: getenv
C:\Users\ondra-pragmax\AppData\Local
2022/01/25 17:24:57 getenv duration: 0s, args: LOCALAPPDATA
2022/01/25 17:24:57 getCachePath duration: 517.9µs, args:
2022/01/25 17:24:57 getArgs duration: 0s, args:
2022/01/25 17:24:57 getArgs duration: 0s, args:
2022/01/25 17:24:57 getShellName duration: 0s, args:
2022/01/25 17:24:57 debug: getenv
2022/01/25 17:24:57 getenv duration: 0s, args: OMP_CACHE_DISABLED
2022/01/25 17:24:57 isRunningAsRoot duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getShellName duration: 0s, args:
2022/01/25 17:24:57 getCurrentUser duration: 0s, args:
2022/01/25 17:24:57 getHostName duration: 0s, args:
2022/01/25 17:24:57 lastErrorCode duration: 0s, args:
2022/01/25 17:24:57 environ duration: 0s, args:
2022/01/25 17:24:57 getRuntimeGOOS duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 debug: getenv
2022/01/25 17:24:57 getenv duration: 0s, args: SSH_CONNECTION
2022/01/25 17:24:57 debug: getenv
2022/01/25 17:24:57 getenv duration: 0s, args: SSH_CLIENT
2022/01/25 17:24:57 getCurrentUser duration: 0s, args:
2022/01/25 17:24:57 getRuntimeGOOS duration: 0s, args:
2022/01/25 17:24:57 getHostName duration: 0s, args:
2022/01/25 17:24:57 debug: getenv
2022/01/25 17:24:57 getenv duration: 0s, args: POSH_SESSION_DEFAULT_USER
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getArgs duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getRuntimeGOOS duration: 0s, args:
2022/01/25 17:24:57 getRuntimeGOOS duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getRuntimeGOOS duration: 0s, args:
2022/01/25 17:24:57 stackCount duration: 0s, args:
2022/01/25 17:24:57 isRunningAsRoot duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getShellName duration: 0s, args:
2022/01/25 17:24:57 getCurrentUser duration: 0s, args:
2022/01/25 17:24:57 getHostName duration: 0s, args:
2022/01/25 17:24:57 lastErrorCode duration: 0s, args:
2022/01/25 17:24:57 environ duration: 0s, args:
2022/01/25 17:24:57 getRuntimeGOOS duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getRuntimeGOOS duration: 0s, args:
2022/01/25 17:24:57 hasCommand duration: 3.6557ms, args: git.exe
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 hasParentFilePath duration: 0s, args: .git
2022/01/25 17:24:57 getFileContent duration: 0s, args: C:\Users\ondra-pragmax\Repositories\ondrasek\dotfiles\.git/HEAD
2022/01/25 17:24:57 error: getFileContent
open C:\Users\ondra-pragmax\Repositories\ondrasek\dotfiles\.git/logs/refs/stash: The system cannot find the file specified.
2022/01/25 17:24:57 getFileContent duration: 0s, args: C:\Users\ondra-pragmax\Repositories\ondrasek\dotfiles\.git/logs/refs/stash
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getBatteryInfo duration: 1.5669ms, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 hasFiles duration: 520.3µs, args: *.js
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 hasFiles duration: 0s, args: *.ts
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 hasFiles duration: 0s, args: package.json
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 hasFiles duration: 0s, args: .nvmrc
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 hasFiles duration: 0s, args: pnpm-workspace.yaml
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 hasFiles duration: 0s, args: .pnpmfile.cjs
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 hasFiles duration: 0s, args: .npmrc
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getShellName duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 isRunningAsRoot duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 isRunningAsRoot duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getPathSeperator duration: 0s, args:
2022/01/25 17:24:57 getShellName duration: 0s, args:
2022/01/25 17:24:57 getCurrentUser duration: 0s, args:
2022/01/25 17:24:57 getHostName duration: 0s, args:
2022/01/25 17:24:57 lastErrorCode duration: 0s, args:
2022/01/25 17:24:57 environ duration: 0s, args:
2022/01/25 17:24:57 getRuntimeGOOS duration: 0s, args:
2022/01/25 17:24:57 getcwd duration: 0s, args:
2022/01/25 17:24:57 lastErrorCode duration: 0s, args:
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (6 by maintainers)
Don’t be sorry. The error handling/propagation is not the best part of powershell 😀.