Linux:software:fortune

From Linux How-To Repository

Jump to: navigation, search

Fortune Output Manipulation

Fortune supplies some default quote text files and database files. The quotes in these appear to have a maximum line length of 80 characters. This is fine if your output medium has a width to cope with it. Mine didn't.

I have a Fortune quote as part of my Conky desktop display. The display is 65 characters wide. But previously, when my Fortune quotes appeared, they would be broken up sometimes because of uneven word wrapping. It's awkward to read, sometimes extends the number of lines below the screen and is just plain annoying. The solution, obviously, was to change all of Fortunes quotes so that they have a maxmum line length of 65 characters.

The first problem is that all lines have a return or "\n" character at the end. They have to be removed. I used gedit's Replace function, replacing all instances of "\n" with a space. This made one long string.

The usual format for its text files is like this:

quote 1
%
quote 2
%

So, the file needs to be reformatted to match, except for the long quotes. First, put the delimiter Fortune uses, a % character, and then each quote's author, on separate lines. In other words (ignorning quotation marks I use here), do this:

   Replace all instances of "%" with "\n%\n" to put it on a line of its own. 
   
   Replace all instances of "--" before the author's name with "\n\t\t--" so that it indents on a line of its own.   

Now you have two options.

1. Just use the "fold -sw" output option (see how in the second .conkyrc example on this page). If you use this method, you can skip down to the "strfile" section below.
2. Apply hard word wraps at a desired length of, say, 50 characters. For this, modify gedit with the code found here: http://ubuntuforums.org/showthread.php?t=566618

The instructions from that site are as follows:

1. Open gedit
2. Edit -> Preferences -> Plugins
3. Enable "External Tools" and press "Configure Plugin"
4. Press "New" and enter "Line Break at Col 50" (or any name you like)
5. Paste the following code in the "command(s)" text area:
#!/usr/bin/env python

import sys

breakat = 50 # change this to desired length

for line in sys.stdin.readlines():
    newline = ''
    for word in line.split() + ['\n']:
        if word == '\n':
            print newline
        elif len(newline + word) > breakat:
            print newline
            newline = word + ' '
        else:
            newline = newline + word + ' '
6. Choose "Current selection" as Input
7. Choose "Replace current selection" as Output
8. Close "External Tools Manager" by pressing the "Close" button
9. Now this script is executable under the "Tools" menu. 

Make sure you first select the text that you want to apply line breaks. Otherwise it will apply to the whole document.

Open up your newly formated Fortune file (but save a back up). Now run your new tool to parse the text. The resulting text will appear at the bottom. Copy and paste this into a new file and delete the top line gedit places about the process and any blank lines. Also delete "Done" at the bottom and any blank lines at the bottom. Save the file.

You will see that the formating is exactly how you want it. However, there is a problem. There are trailing whitespaces that you need to delete or you cannot create a proper Fortune database file. So, open up the new file with the Cream text editor. You will see under Format that it has a Remove Trailing Whitespaces option. Just run that and save the file.

Now you are ready to create the Fortune database file. Simply run strfile on it. You have to do this after any change you make.

   sudo strfile filename

Next do a symlink to the file, if there isn't one already, naming it with a ".u8" extension:

   ln -s filename.dat filename.u8

That's it. When you run Conky now, your fortune quotes will wrap neatly at 50 characters.

Personal tools
KARA Logo