Expose errorlevel / exit code of last command to custom hlsl shaders #12948

Closed
opened 2026-01-31 03:29:43 +00:00 by claunia · 7 comments
Owner

Originally created by @nincode on GitHub (Mar 10, 2021).

Description of the new feature/enhancement

Custom HLSL shaders are awesome! They would be even better if they responded to what's happening in the shell. For example, I have a nice smoothly swirling Mandlebrot background in my shell, but I would want to it to turn reddish and swirl angrily when a command errors out. So, I would like to see the errorlevel or exit code of last command exposed to HLSL.

Proposed technical implementation details (optional)

On the HLSL side, expose the error level as one more int in the cbuffer. The issue might be how to get the error level; I don't think there's a generic way of getting that in both Windows and over an SSH connection to, say, Linux. However, the Windows bit seems doable.

cbuffer PixelShaderSettings {
  float  Time;
  float  Scale;
  float2 Resolution;
  float4 Background;
  int ErrorLevel; // <---- error level of last command
};
Originally created by @nincode on GitHub (Mar 10, 2021). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> # Description of the new feature/enhancement <!-- A clear and concise description of what the problem is that the new feature would solve. Describe why and how a user would use this new functionality (if applicable). --> Custom HLSL shaders are awesome! They would be even better if they responded to what's happening in the shell. For example, I have a nice smoothly swirling Mandlebrot background in my shell, but I would want to it to turn reddish and swirl angrily when a command errors out. So, I would like to see the errorlevel or exit code of last command exposed to HLSL. # Proposed technical implementation details (optional) On the HLSL side, expose the error level as one more int in the cbuffer. The issue might be how to get the error level; I don't think there's a generic way of getting that in both Windows and over an SSH connection to, say, Linux. However, the Windows bit seems doable. ``` cbuffer PixelShaderSettings { float Time; float Scale; float2 Resolution; float4 Background; int ErrorLevel; // <---- error level of last command }; ``` <!-- A clear and concise description of what you want to happen. -->
Author
Owner

@DHowett commented on GitHub (Mar 10, 2021):

I really like this idea, but Terminal has to operate under one critical technical constraint: it actually doesn't know anything about the app that it's talking to. It only sees one program -- cmd, powershell, wsl, whatever shell you're actually using -- and only knows about that one thing. If CMD starts a process and then sets ERRORLEVEL, there's no channel through which it can communicate that into back to Terminal. It makes it nearly impossible to do these things without coming up with an in-band control sequence to "tell" Terminal what happened (which then requires shell support, which then requires some standardization, etc.)

@DHowett commented on GitHub (Mar 10, 2021): I really like this idea, but Terminal has to operate under one critical technical constraint: _it actually doesn't know anything about the app that it's talking to_. It only sees one program -- cmd, powershell, wsl, whatever shell you're actually using -- and only knows about that one thing. If CMD starts a process and then sets ERRORLEVEL, there's no channel through which it can communicate that into back to Terminal. It makes it nearly impossible to do these things without coming up with an in-band control sequence to "tell" Terminal what happened (which then requires shell support, which then requires some standardization, etc.)
Author
Owner

@KalleOlaviNiemitalo commented on GitHub (Mar 10, 2021):

For local processes, Windows Terminal could perhaps track the exit codes via JOBOBJECT_ASSOCIATE_COMPLETION_PORT. That seems unlikely to work with WSL2 or SSH, though.

@KalleOlaviNiemitalo commented on GitHub (Mar 10, 2021): For local processes, Windows Terminal could perhaps track the exit codes via [JOBOBJECT_ASSOCIATE_COMPLETION_PORT](<https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_associate_completion_port> "JOBOBJECT_ASSOCIATE_COMPLETION_PORT structure (winnt.h)"). That seems unlikely to work with WSL2 or SSH, though.
Author
Owner

@KalleOlaviNiemitalo commented on GitHub (Mar 11, 2021):

The job-object hack would have another flaw, too: if the shell first runs the requested program, and that exits with an error, but the shell then runs more programs in order to format the prompt, it would be difficult for Windows Terminal to know which of those exit codes is significant.

@KalleOlaviNiemitalo commented on GitHub (Mar 11, 2021): The job-object hack would have another flaw, too: if the shell first runs the requested program, and that exits with an error, but the shell then runs more programs in order to format the prompt, it would be difficult for Windows Terminal to know which of those exit codes is significant.
Author
Owner

@zadjii-msft commented on GitHub (Mar 11, 2021):

Yea, as awesome of an idea as this is, I'm just not sure there's any way to accomplish this in a good cross-platform way. There's a lot of edge cases noted here, with nested shells, wsl, and ssh to consider.

Maybe someone someday does come up for a standardized way for a shell to tell the terminal about the return code. If that escape sequence ever does get standardized, we can revisit this idea.

@zadjii-msft commented on GitHub (Mar 11, 2021): Yea, as awesome of an idea as this is, I'm just not sure there's any way to accomplish this in a good cross-platform way. There's a lot of edge cases noted here, with nested shells, wsl, and ssh to consider. Maybe someone someday does come up for a standardized way for a shell to tell the terminal about the return code. If that escape sequence ever does get standardized, we can revisit this idea.
Author
Owner

@KalleOlaviNiemitalo commented on GitHub (Mar 11, 2021):

Perhaps this could instead be tied to the taskbar progress indicator https://github.com/microsoft/terminal/issues/3004. Expose the state and percentage numbers to the shader. Users could then configure their shells to emit the OSC 9;4 sequence as part of the prompt.

@KalleOlaviNiemitalo commented on GitHub (Mar 11, 2021): Perhaps this could instead be tied to the taskbar progress indicator <https://github.com/microsoft/terminal/issues/3004>. Expose the state and percentage numbers to the shader. Users could then configure their shells to emit the OSC 9;4 sequence as part of the prompt.
Author
Owner

@zadjii-msft commented on GitHub (Mar 11, 2021):

Huh. Now that's not a bad idea. I wonder how breaking it would be to pass more data to a shader. Like, if we add additional params in v1.8, but someone's got a shader file from 1.6 without those params, is it just going to crash/fail? That would certainly be annoying. Then again, it is an experimental feature...

@zadjii-msft commented on GitHub (Mar 11, 2021): Huh. Now that's not a bad idea. I wonder how breaking it would be to pass more data to a shader. Like, if we add additional params in v1.8, but someone's got a shader file from 1.6 without those params, is it just going to crash/fail? That would certainly be annoying. Then again, it is an _experimental_ feature...
Author
Owner

@nincode commented on GitHub (Mar 12, 2021):

This all makes sense, thanks for considering this everyone.
As a compromise, could you please take a look at https://github.com/microsoft/terminal/issues/9468 ?
The fight for sweet sweet pixel shader backgrounds continues. :)

@nincode commented on GitHub (Mar 12, 2021): This all makes sense, thanks for considering this everyone. As a compromise, could you please take a look at https://github.com/microsoft/terminal/issues/9468 ? The fight for sweet sweet pixel shader backgrounds continues. :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#12948