Skip to main content

How can I detect Gnome Terminal?

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.

Solved

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


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


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.


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" ;)


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.


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

Comments

Popular posts from this blog

Alias for a shell script

I need wget to be always used with --no-check-certificate option. This can be accomplished by typing the following line in a Unix terminal: alias wget='wget --no-check-certificate' However, when I execute shell scripts .sh that include wget command, the original wget is used not the aliased one. How can I resolve this issue? Solved You have to ensure your alias is defined, where the Shell-script is also sourcing it. If you are using Bash and the Shell-script is using /bin/bash as interpreator, putting into file .bashrc might bei a good idea. However, --no-check-certificate is in about 99.9% of all cases a very bad idea as SSL is there for a reason. Also I recommend not to overwrite common commands with custom aliases as this might change behavior of tools a script is relying on in unexpected way. I really recommend to fix up the environment so you don't need this dirty hack.

Generate PHP Interfaces

Is there a tool the generate php interface from existing classes? It would be nice to have a tool like Netbeans automatic getter/setter creation but for interfaces. Solved For programmatic usage there is InterfaceDistiller that allows you to derive interfaces from existing classes like this: $distiller = new InterfaceDistiller; $distiller ->methodsWithModifiers(\ReflectionMethod::IS_PUBLIC) ->extendInterfaceFrom('Iterator, SeekableIterator') ->excludeImplementedMethods() ->excludeInheritedMethods() ->excludeMagicMethods() ->excludeOldStyleConstructors() ->filterMethodsByPattern('(^get)') ->saveAs(new SplFileObject('MyInterface.php')) ->distill('SomeFoo', 'MyInterface'); It also has a CLI interface: Usage: phpdistill [options] --bootstrap Path to File containing your bootstrap and autoloader --methodsWithModifiers A ReflectionMe...