-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieve current foreground and background color #120
Comments
Here is another Answer with some more information: It also mentions a |
We already have a similar logic for querying the cursor position; perhaps it could be generalized to cover this case as well. ansi-terminal/src/System/Console/ANSI/Unix.hs Lines 86 to 105 in 5217d08
|
On Windows 10/11 and with Windows Terminal (planned to be the default terminal on Windows 11 during 2022: https://devblogs.microsoft.com/commandline/windows-terminal-as-your-default-command-line-experience/), the implementation of the |
Not supported by native terminals on Windows. Also adds, and applies, utility `osc :: [(String, String)] -> String`, as several functions now use the XTerm OSC control sequences. This means that `setTitleCode` now ends with the recommended STRING TERMINATOR (ST), rather than legacy `\007`. The filter in `setTitleCode` is changed to exclude all non-printable characters.
Not supported by native terminals on Windows. Also adds, and applies, utility `osc :: [(String, String)] -> String`, as several functions now use the XTerm OSC control sequences. This means that `setTitleCode` now ends with the recommended STRING TERMINATOR (ST), rather than legacy `\007`. The filter in `setTitleCode` is changed to exclude all non-printable characters.
Not supported by terminals on Windows. Native terminals do not allow the color to be reported. Other terminals do not allow the reported color to be read from the console input stream.
Not supported by terminals on Windows. Native terminals do not allow the color to be reported. Other terminals do not allow the reported color to be read from the console input stream.
…r` etc Also adds `reportLayerColorCode`, `layerColor` and `getLayerColor`. Not supported by terminals on Windows. Native terminals do not allow the color to be reported. Other terminals do not allow the reported color to be read from the console input stream.
I have been looking into this further, on macOS 10.15.5. I am running into inconsistencies with the STRING TERMINATOR (ST) in the output. I was hoping the output would use the same ST as the command. macOS Terminal v2.10 (433) appears to recognise legacy "\BEL" and modern "\ESC\" but returns only "\BEL". On Windows: |
Not supported by terminals on Windows. Native terminals do not allow the color to be reported. Other terminals do not allow the reported color to be read from the console input stream.
Not supported by terminals on Windows. Native terminals do not allow the color to be reported. Other terminals do not allow the reported color to be read from the console input stream.
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. Not supported by terminals on Windows. Native terminals do not allow the color to be reported. Other terminals do not allow the reported color to be read from the console input stream.
@ad-si, I think my proposed pull request #137 will add this to My proposed API mimics the existing API for |
Emulation works for the Windows Console Host (ConHost) user-interface and not for Windows Terminal.
Emulation works for the Windows Console Host (ConHost) user-interface and not for Windows Terminal.
Emulation works for the Windows Console Host (ConHost) user-interface and not for Windows Terminal.
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. On Windows, emulated using the Windows Console API but the emulation of `reportLayerColor` works only on the native ConHost terminal and not on Windows Terminal. Non-native terminals cannot make use of `getReportedLayerColor`.
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. On Windows, emulated using the Windows Console API but the emulation of `reportLayerColor` works only on the native ConHost terminal and not on Windows Terminal. Non-native terminals cannot make use of `getReportedLayerColor`.
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. Also updates and reorganises some Haddock documentation for developments in Windows. On Windows, emulated using the Windows Console API but the emulation of `reportLayerColor` works only on the native ConHost terminal and not on Windows Terminal. Non-native terminals cannot make use of `getReportedLayerColor`.
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. Also updates and reorganises some Haddock documentation for developments in Windows. On Windows, emulated using the Windows Console API but the emulation of `reportLayerColor` works only on the native ConHost terminal and not on Windows Terminal. Non-native terminals cannot make use of `getReportedLayerColor`.
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. On Windows, emulated using the Windows Console API but the emulation of `reportLayerColor` works only on the native ConHost terminal and not on Windows Terminal. Non-native terminals cannot make use of `getReportedLayerColor`. Also updates and reorganises some Haddock documentation for developments in Windows and to take a consistent approach to the documentation of the 'h...' and '...Code' variants.
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. On Windows, emulated using the Windows Console API but the emulation of `reportLayerColor` works only on the native ConHost terminal and not on Windows Terminal. Non-native terminals cannot make use of `getReportedLayerColor`. Also updates and reorganises some Haddock documentation for developments in Windows and to take a consistent approach to the documentation of the 'h...' and '...Code' variants.
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. On Windows, emulated using the Windows Console API but the emulation of `reportLayerColor` works only on the native ConHost terminal and not on Windows Terminal. Non-native terminals cannot make use of `getReportedLayerColor`.
I have updated my pull request to cover Windows, and I think it could be merged and a new version of On Windows, things are a little complicated. As mentioned above, the native terminals still do not support the OSC code. So, I have emulated that. However, the emulation only works for ConHost and not for Windows Terminal (which no longer uses ConHost for the user-interface). Further, getting the data emitted into the input stream has to use the Windows Console API (GHC's WinIO does not yet provide a solution - see https://gitlab.haskell.org/ghc/ghc/-/issues/21488) and so will not work on mintty (for example). |
…getLayerColor Also adds `hReportLayerColor`, `hGetLayerColor` and `layerColor`. On Windows, emulated using the Windows Console API but the emulation of `reportLayerColor` works only on the native ConHost terminal and not on Windows Terminal. Non-native terminals cannot make use of `getReportedLayerColor`.
Awesome, thanks a lot for implementing this @mpilgrem! The docs say:
But how would it work to use some other Handler? When I use it with stdout, it behaves very weirdly on redirection: $ stack run open | head -c 40
Id []11;rgb:1999/1999/1999^[\⏎
$ ]11;rgb:1999/1999/1999\ How can I fix / circumvent this? |
@ad-si, the way that this works is that the relevant 'ANSI' codes are detected in the (ANSI-capable) terminal's output stream and the information is injected into the terminal's input stream. If the module Main (main) where
import System.Console.ANSI ( ConsoleLayer (..), hGetLayerColor )
import System.IO ( stderr )
main :: IO ()
main = do
mBC <- hGetLayerColor stderr Background
mFC <- hGetLayerColor stderr Foreground
print (mBC, mFC) |
Yeah, |
@ad-si, not as far as I am aware. An application's code would usually detect if its output was to a terminal or not (eg redirected) (https://hackage.haskell.org/package/ansi-terminal-0.11.4/docs/System-Console-ANSI.html#g:32), and then behave accordingly. |
I want to implement a dark and light mode for a CLI tool, but therefore I need to figure out the current fore-/background color.
There seems to be a control sequence for it: https://stackoverflow.com/a/7767891/1850340
However, it's not clear if the response can be captured in a sensible way.
Are there any other ways to achieve this?
The text was updated successfully, but these errors were encountered: