Steven Jewel Blog RSS Feed

15 Mar 2014

A Bash Run Dialog

One day I realized that I really missed the keybindings from the command-line when I was using the run dialog. In particular, I missed Ctrl+r, Ctrl+p, and Bash's advanced tab completion. I started out to build a run dialog with better bindings, but ended up just using bash itself.

In other words, it's like a terminal that you can launch with a single keystroke, and that disappears as soon as you press Enter.

Screenshot

I have this bound to Ctrl+Alt+r.

Spawning a terminal

The funnest part of the project was figuring out how to control an interactive bash process. For most projects it's sufficient to feed the commands to a shell via system, but in this case I had to pass all keystrokes through.

The ruby standard library has an excellent tool for this, PTY. PTY spawns a pseudo terminal (as opposed to an actual terminal), and gives you an IO object for input, another for output, and a final one for errors:

term_read, term_write, term_err = PTY.spawn "bash --login"

The rest of the project consisted of translating keystrokes from GTK into the sequences that a terminal would expect, and translating the output from the terminal into modifications to a Gtk::TextView.

The source code is available on github.