问题
I'm developing a library that needs to identify the running terminal emulator in order to output emulator-specific escape code sequences.
For example, iTerm 2 uses the following proprietary sequence:
ESC ] 50 ; CurrentDir=/path/to/dir BEL
While ConEmu uses the following proprietary sequence:
ESC ] 9 ; 9 ; C:\path\to\dir BEL
Now, I need to query the terminal (either using environment variables or using an escape sequence) in order to output the correct terminal-specific codes.
How can I reliably detect and identify what terminal emulator that I'm running on?
回答1:
The answer depends...
Well, you may try to check environment variable, e.g. ConEmuServerPID. But that may be not reliable, because a) the variable may be somehow inherited from parent process when your process is running actually outside of ConEmu b) the variable may be not set in some cases, for example when you ssh somewhere
You may request terminal "version" with ESC [ > c
reports ESC > 0 ; 136 ; 0 c
. But of course, this 136
is not a guarantee of ConEmu.
Also, you may use ANSI sequence to execute IsConEmu GuiMacro. However, most likely you aren't able to check the result when ssh-ing.
You may call ConEmuC.exe -IsConEmu, but you can't do that in ssh.
来源:https://stackoverflow.com/questions/54747768/how-to-identify-the-terminal-emulator-being-used