如何检测 Gnome 终端?

发布于 2024-07-08 23:19:55 字数 466 浏览 7 评论 0原文

我正在编写一个使用 F1 键的控制台应用程序(寻求帮助)。 不幸的是,虽然 Konsole(KDE)不使用此键,但 Gnome Terminal 却使用此键,因此我的应用程序无法访问 F1 键。 我认为没有办法检测 F1 键是否已经映射到 GUI 端(Gnome 终端),但如果存在,答案将消除这个问题。 :)

因此,我的下一个最佳选择是尝试检测我是否在 Gnome 终端内运行。 有什么办法可以做到这一点吗? 我主要感兴趣的是从 Ruby 内部收集这些信息,但如果它可以通过 shell 或环境变量或虚拟文件系统(/proc、/dev 等)来完成,那么这就足够了。

我希望有一种可靠的方法来做到这一点,但我可以满足“最佳猜测”方法,例如 grep 环境变量以获取线索,这些线索可以让我合理地假设 Gnome 终端是包装终端。

额外信息:其他密钥也被 Gnome 终端“窃取”。 我打算向 Gnome 用户显示某种有关替代键的信息性消息。

I am writing a console application which makes use of the F1 key (for help). Unfortunately, while Konsole (of KDE) doesn't use this key, Gnome Terminal does, so the F1 key becomes inaccessible to my application. 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. :)

Ergo, my next best bet is to try to detect whether I am running inside Gnome Terminal. 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.

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.

Extra info: other keys are also "stolen" by Gnome Terminal. I intend to display some sort of informative message about alternative keys for Gnome users.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风轻花落早 2024-07-15 23:19:56

Googleage 透露,我也许可以依靠 Gnome 终端将 COLORTERM 环境变量设置为“gnome-terminal”。

Googleage has revealed that I might be able to rely on Gnome Terminal setting the COLORTERM environment variable to 'gnome-terminal'.

我不在是我 2024-07-15 23:19:56

对于 zsh:

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

gnome-terminal 用于设置 $COLORTERM,但这已被删除(在 3.12.0-67-g1d5c1b6 中)。

For 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).

清晰传感 2024-07-15 23:19:56

通常您使用 termcap info 又名 terminfo。 这会告诉您正在使用的终端类型以及按键是什么。

这是用户的选择,并且由于 Gnome Terminal 将自己报告为可能是一种 x-term,正如 Konsole 可能所做的那样,因此用户可能有一种方法来创建 F1 按键。 因此,我怀疑两者的终端能力会被报告为相当。

您可能会对功能键转义代码的调查感兴趣没有实际解释如何(如果有可能的话)如何使 gnome 终端产生 F1 按下。 想起我很久以前使用的 Mac OS X 终端,它捕获滚动条的 PageUp 和 PageDown,而 Command-PageUp 和 Command-PageDown 将它们传递到终端。

Normally you use termcap info aka 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. 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. 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.

月下伊人醉 2024-07-15 23:19:56

这是一种相当粗略的方法,如果您想将其委托给 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

如果从您自己的程序运行,则通过 system() for例如,您可以“grep”您的程序名称而不是“bash”,

我知道,这绝对是“像海盗一样黑客”;)

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:

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'

I know, it's definitely "hack like a pirate" ;)

香草可樂 2024-07-15 23:19:56

fwiw,这是 gnome-terminal 中的设置; 用户可以进入编辑-> gnome 终端菜单中的键盘快捷键并更改或删除 F1 键绑定。

fwiw, this is a setting in gnome-terminal; users can go to Edit -> Keyboard Shortcuts in the gnome-terminal menus and change or delete the F1 keybinding.

傲娇萝莉攻 2024-07-15 23:19:56

我只需检查我选择的终端模拟器的 $TERM,然后添加一个别名以在当前目录中生成新终端。

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

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.

if [[ $TERM == 'xterm' ]] ; then
  alias nw='gnome-terminal --working-directory=$PWD'
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文