Tag Archives: terminal

Guake Terminal – Dual Monitor Edits

For a long time my favorite Terminal program has been Guake. It is “a drop-down terminal for GNOME Desktop Environment.” It is always available and hidden when not in use. Simply press a button and it pops out from top of the screen, press again and it goes away.

To install Guake simply  sudo apt-get install guake

The only problem I’ve had with it is on my desktop machine when two monitors are plugged in. Guake always defaults to the left monitor. It does a great job of determining the size of ‘monitor 1’, which in my case is a much smaller extra one to the side. To get Guake on my right-side monitor I had to tweak the source code. Here’s how you can do the same:

  1. Make a copy of theGuake program and put it in yourbin folder. I renamed mine toguake-dualmon but you can call it whatever you want.
  2. Edit the copy in your preferred editor.
  3. Find the method definition  def get_final_window_rect(self):
  4. First we will correctly position the terminal on the right monitor. Add one line at the end, between window_rect.y = 0  and  return window_rect . The window_rect.x and window_rect.y variables tell theGuake window where to be located. Set window_rect.x to be the width of your left monitor and window_rect.y will depend on how offset the monitors are. I had to play with the ‘y’ setting to get it just right or the text starts off the top of the screen.
  5.  Now Guake will be positioned on your right monitor, but it will still be the size of the left one. In my case it was sized at 1280, and I needed it to be 1920. Divide your right monitor’s width by the left monitor’s width (ie. 1920/1280 = 150). Still within  get_final_window_rect(self): you will find the line width = 100 . This setting is a percentage of your left monitor, so set it to the answer you got by dividing one width into the other. In my case it was:
  6. That’s it! Just make sure to always run your copy of the program, or better yet add it to your autostart so it runs automatically!

Here’s the new get_final_window_rect() method looks like with edited lines highlighted:

Have any questions? Feel free to ask in the comments below. Thanks for reading!

Python Snippet: Get Terminal Width

There is a wonderful command on UNIX systems to return the width of the current terminal. Enter  tput cols and see for yourself! Tput can actually provide a lot of different info by querying the terminfo database. Really all I’ve needed though is ‘cols’ for columns.

I was writing a Python CLI app, and wanted to format and print output in a nice table. To do that (without downloading a table printing module) I had to know how much space was available for printing. Can’t make a nice-looking set of data if every other line is being wrapped!

Here is a simple function that returns the width of whatever terminal is running the script. Subprocess is the most current built-in Python module for running system commands. It contains a selection of different functions each with many optional parameters, depending on the complexity of your needs. Using subprocess.check_output() is the way to go if you are looking to get back the output of a command. The  check_output() function has a whole slew of accepted arguments, but in this simple instance we need only one: a list containing command to run and the arguments to pass to it.

The first exception will be raised if the system returns an error when trying to execute the tput command. The second exception will be raised if the command (in our case tput) has a problem with the given argument ( cols). If nothing goes wrong, the output will be a nice whole number that you can use to determine exactly how many characters can be printed per line!

Have you ever needed to know the width of your terminal? If you have any questions please feel free to ask in the comments below!

Here’s a link to this code snippet on Github Gists: terminal_width.py

Git: Merging Conflicts Visually

When using git, I much prefer using the command line as opposed to any GUI applications. However, a problem arises when merging conflicts between the local branch and the remote. Looking at the file and doing it manually can be quite a chore, and the syntax to show which edit is from where is confusing for me. The only other option is Git’s default tool for merging, called vimdiff. It’s basically just running the vim editor with a +diff feature. That might be perfect for vim users, but I have no desire to learn the slew of shortcuts I’d need just for this one type of situation.

Welcome Meld to the picture. Here’s the description from the program’s site:

“Meld is a visual diff and merge tool targeted at developers. Meld helps you compare files, directories, and version controlled projects. It provides two- and three-way comparison of both files and directories, and has support for many popular version control systems.”

Here’s the rundown:

Pop open your trusty terminal and run

Now, to replace vimdiff with this lovely GUI, enter

After realizing there’s a conflict and using git pull  to grab the remote’s changes, it’s time to merge the conflicts, commit, and then push your altered file.

It’s as easy as entering  git mergetool . Follow that by hitting enter to confirm, and Meld will launch.

meld-gitmerge-example
Meld in action

The middle field will be your new file after merging the changes. Simply click the arrows to move the desired lines into your new document. You can also manually type or copy/paste into the middle if manual tweaking is needed. Click save, close Merge, and you’re done. Meld will make a backup copy of your local branch’s version, so just delete it when you’re done if you don’t need it anymore. Commit your changes and push to your remote. Conflicts merged and ready for more coding.