简体   繁体   English

如何检测Gnome终端?

[英]How can I detect Gnome Terminal?

I am writing a console application which makes use of the F1 key (for help). 我正在编写一个使用F1键的控制台应用程序(求助)。 Unfortunately, while Konsole (of KDE) doesn't use this key, Gnome Terminal does, so the F1 key becomes inaccessible to my application. 不幸的是,虽然Konsole(KDE)不使用此密钥,Gnome终端会这样做,因此我的应用程序无法访问F1密钥。 I don't think there's a way to detect whether the F1 key is already mapped in the GUI side of things (Gnome Terminal), but if there is, the answer to that will obviate this question. 我不认为有一种方法可以检测F1键是否已经在事物的GUI端映射(Gnome Terminal),但是如果有,那么答案将消除这个问题。 :) :)

Ergo, my next best bet is to try to detect whether I am running inside Gnome Terminal. 因此,我的下一个最好的选择是尝试检测我是否在Gnome终端内运行。 Is there some way to do that? 有办法做到这一点吗? I'm primarily interested in gleaning this from within Ruby, but if it can be done via shell or environment variables, or virtual filesystem (/proc, /dev, etc.) then that will suffice. 我主要感兴趣的是从Ruby中收集它,但如果可以通过shell或环境变量或虚拟文件系统(/ proc,/ dev等)来完成,那么这就足够了。

I'm hoping for a reliable way to do this, but I can settle for "best guess" approaches like grepping the environment variables for clues that can let me reasonably assume that Gnome Terminal is the wrapping terminal. 我希望有一个可靠的方法来做到这一点,但我可以满足于“最好的猜测”方法,比如为环境变量加以获取可以让我合理地假设Gnome Terminal是包装终端的线索。

Extra info: other keys are also "stolen" by Gnome Terminal. 额外信息:其他钥匙也被Gnome Terminal“偷走”。 I intend to display some sort of informative message about alternative keys for Gnome users. 我打算为Gnome用户显示一些关于替代密钥的信息性消息。

谷歌已经透露,我可能可以依靠Gnome Terminal将COLORTERM环境变量设置为'gnome-terminal'。

For zsh: 对于zsh:

[[ "$COLORTERM" == "gnome-terminal" ]] || [[ ${$(ps -p $(ps -p $$ -o ppid=) -o cmd=):t} == gnome-terminal* ]]

gnome-terminal used to set $COLORTERM , but this has been dropped (in 3.12.0-67-g1d5c1b6). gnome-terminal用于设置$COLORTERM ,但这已被删除(在3.12.0-67-g1d5c1b6中)。

Normally you use termcap info aka terminfo. 通常你使用termcap信息即terminfo。 This tells you what kind of terminal you're working with and also what the keys are. 这告诉您正在使用什么类型的终端以及密钥是什么。

It is the user's choice, and since Gnome Terminal reports itself as probably a kind of x-term, as Konsole likely does, there is likely a way for the user to create an F1 keypress. 这是用户的选择,并且由于Gnome Terminal可能会将其自身报告为一种x-term,正如Konsole所做的那样,用户可能有办法创建F1按键。 Therefore, I suspect that the terminal capabilities of the two will be reported as equivalent. 因此,我怀疑两者的终端能力将被报告为等效。

This investigation of Function Key Escape Codes might be interesting to you without actually explaining how, if it is even possible, that the gnome terminal could be made to produce an F1 press. 函数密钥转义代码的这种调查可能对你很有意义,而没有实际解释如果可能的话,如何使gnome终端产生F1印刷机。 Thinking of the Mac OS X terminal I used a long time back, it caught PageUp and PageDown for the scroll bar, while Command-PageUp and Command-PageDown passed them through to the terminal. 考虑到Mac OS X终端我使用了很长时间,它抓住了PageUp和PageDown用于滚动条,而Command-PageUp和Command-PageDown将它们传递给终端。

A rather crude method, if you feel like delegating this to the shell -- otherwise, in C or C++, you will have to wade through your /proc, if you have one: 一个相当粗略的方法,如果你想把它委托给shell - 否则,在C或C ++中,你将不得不涉及你的/ proc,如果你有一个:

ps x | grep `ps o ppid,fname | grep bash | grep -v grep | head -1 | awk '{print $1}'` | grep 'gnome-terminal' | wc -l

If running from your own program, through system() for instance, you may with to 'grep' your program's name rather than 'bash' 如果从您自己的程序运行,例如通过system()运行,您可能会'grep'您的程序名称而不是'bash'

I know, it's definitely "hack like a pirate" ;) 我知道,这肯定是“像海盗一样黑客”;)

fwiw, this is a setting in gnome-terminal; fwiw,这是gnome-terminal的一个设置; users can go to Edit -> Keyboard Shortcuts in the gnome-terminal menus and change or delete the F1 keybinding. 用户可以在gnome-terminal菜单中转到编辑 - >键盘快捷键,然后更改或删除F1键绑定。

I simply check the $TERM for the terminal emulator of my choice and then add an alias for spawning a new terminal in the current directory. 我只需检查我选择的终端仿真器的$ TERM,然后添加一个别名,用于在当前目录中生成一个新终端。

if [[ $TERM == 'xterm' ]] ; then
  alias nw='gnome-terminal --working-directory=$PWD'
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM