Sunday, October 24, 2010

Console-ing myself transparently

I spend a lot of time in the command line, so it would be convenient for me to have access to a console at all times.  The only problem is it's so easy to forget not close it, plus it's never big enough, plus I don't want it in the task list/pager, etc, etc.  I've tried solutions like Yakuake and Guake, but they never quite fit my needs.

Besides, I'm playing around with Linux, right?  Might as well try cobbling together a Linuxish solution that works exactly how *I* want it to.  So, first, I put together some requirements:

- Needs to always be available.
- Needs to be unobtrusive.
- Needs to be big enough to see full lines of whatever I was working on (such as looking at logs).
- Needs to not hide the desktop.

After looking over my requirements, I decided that what I was looking for was essentially one of those cool transparent and undecorated consoles that you see in the screenshots of the really cool kids.  First, we need to install a couple of packages: sudo aptitude install wmctrl devilspie. I'll explain their uses as I get to them.

The first one we'll setup is Devil's Pie.  This is a unique utility that runs in the background. At launch, it reads in any Devli's Pie scripts in ~/.devilspie. These scripts are made up of rules that tell Devil's Pie "when you see a window launch that matches these attributes, apply these settings to it". You can set placement (such as making sure an app always launches in a particular location or workspace) or display characteristics. We're going to use the latter to remove the title bar from a very specific console and nail it to the desktop.

To start, open Gnome Terminal (you can use whatever you want, just apply similar settings). Edit -> Profiles -> New.

- On the General tab, give the profile a name. I'm calling mine DesktopConsole. Uncheck "Show menubar by default in new terminals."

- Title and Command tab, set the Initial Title also to DesktopConsole and set "When terminal commands set their own titles" to "Keep initial title".

- Background tab, uncheck "Use background settings from system theme" and choose Transparent background. Set transparency to whatever's most comfortable for you. We'll be modifying this on the fly later, but need an initial setting.

- Scrolling tab, Scrollbar is Disabled.

Apply any other customizations, such as colors, as you like and then save the profile. Now, to make sure it launches at startup, we can just add it to Startup Applications in System -> Preferences, but I have need of a little more control, so we're going to create a script to launch it with. I put all of my scripts in ~/.bin, so

joe ~/.bin/launchconsole

Into which I add:

#!/bin/sh
gnome-terminal --window-with-profile=DesktopConsole --command=screen --geometry=400x200


This launches a new terminal window that uses my custom profile and launches Gnu screen. Screen's a tool that's well worth looking in to, especially for a setup like this. It allows you to essentially have multiple terminal sessions to use as you need. We will be going into more detail about it in later articles. We'll also see a little later why I used a script to launch a single command. Hint: it's not going to be alone for long! And, though I'm spreading this console across the desktop, I still specified a geometry. For some reason, what we do next never works properly without it.

If we launched it now, we'd get a transparent console, yes, but it's free-floating and still has a titlebar. Time for a slice of the Devil's Pie. If you don't already have one, you need to create a directory in your home for the scripts (mkdir ~/.devilspie). Then, we create the script to do all of the magic...or at least most of it (joe ~/.devilspie/DesktopConsole.ds)

(if
(matches (window_name) "DesktopConsole")
(begin
(undecorate)
(skip_pager)
(skip_tasklist)
(wintype "desktop")
)
)


Each Devil's Pie script can contain only one rule. In this case, the rule is "if you see a window with the name DesktopConsole apply these properties to it":

- undecorate: Removes the title bar from the window.

- skip_pager: Prevents the window from appearing in your alt-tab list of windows.

- skip_tasklist: Prevents it from showing up on taskbars.

- wintype "desktop": I played around with quite a few different types before finally finding and settling on "desktop". When you set this property, the window manager thinks of the window as another desktop. That means you can do all of the "show desktop" things (which minimize all windows) and the console remains. It also means the console is always below other windows.

So, to verify it works, we log off and back on and are presented with our lovely desktop console:



A little small, though, huh? Let's fix that by modifying that launchconsole script:

 

#!/bin/sh

# First we need to know the screen's resolution
screenResolution="`xrandr | grep \* | cut -d' ' -f4`"

# The above command returns the geometry of the screen, i.e. 1600x900
# So, we need to split that into two numbers we can plug in elsewhere in the script
# In the next couple of lines, we're using the bash split function and putting each
# number into its own cell in an array.
IFS="x"
arr=( $screenResolution )
IFS=""
# echo ${arr[1]}
screenHeight=${arr[1]}
screenWidth=${arr[0]}

# My top panel is 32 pixels high. The below setting ensures the console fills the
# remaining desktop
consoleFullHeight=$(($screenHeight-33))

# Launch the terminal
gnome-terminal --window-with-profile=DesktopConsole --command=screen

# wmctrl is a utility that allows you to control windows and their placement from the
# command line. This command tells it to place the console starting 33 pixels down
# from the top-left corner (right below the panel), and fill the screen.
wmctrl -r "DesktopConsole" -e 0,0,33,$screenWidth,$consoleFullHeight

# As you'll see in later articles, I've got some plans in mind to make this console
# usable in a Yakuake/Guake kind of way. As part of those plans, I'll need to modify the
# transparency of the console as needed. In order to ensure we start off
# transparent enough, we'll set this in the initial launch script.
gconftool --set "/apps/gnome-terminal/profiles/Profile0/background_darkness" --type float .01

So, now we save this file, log off and log back on again.



Much better this time. We're good with this for the moment, but I've got an addition in the works to share with you soon. Beyond that, I've got my screen config and what I use that for. Hint: gMail and Mutt. gMutt.

No comments:

Post a Comment