#! /usr/bin/perl ############################################################################# # Program Description/Comments/Updates ############################################################################# # Quip.pl by Leslie Houk # # SYNTAX: quip.pl [] [/?|-?|/h|/help|-h|-help|--help] # # DESCRIPTION: Reads a text file of one-line quips and randomly displays # one of them in an xmessage box. # # PARAMETERS: : Specifies the input file of quips. # # /?, etc.: Displays this help file. # ############################################################################# ############################################################################# # Defaults ############################################################################# $quip_file = $ENV{ 'HOME' } . '/data/quip.dat'; $font = '-misc-fixed-medium-r-normal--20-140-100-100-c-100-iso8859-1'; $def_x = 200; $def_y = 300; ############################################################################# # Main body ############################################################################# foreach $i ( 0 .. $#ARGV ) { $ARGV[$i] =~ tr/A-Z/a-z/; if ( $ARGV[$i] eq '-?' || $ARGV[$i] eq '/?' || $ARGV[$i] eq '/help' || $ARGV[$i] eq '/h' || $ARGV[$i] eq '-help' || $ARGV[$i] eq '-h' || $ARGV[$i] eq '--help' ) { print( "Quip.pl Version 1.0 by Leslie Houk.\n\n" ); print( "SYNTAX: quip.pl [] [/?|-?|/h|/help|-h|-help|--help]\n\n" ); print( "DESCRIPTION: Reads a text file of one-line quips and randomly displays\n" ); print( " one of them in an xmessage box.\n\n" ); print( "PARAMETERS: : Specifies the input file of quips. The\n" ); print( " default is \"$quip_file\".\n\n" ); print( " /?, -?, /h, etc.: Displays this help file.\n\n" ); exit; } else { $quip_file = $ARGV[$i]; } } open( QUIPS, "< $quip_file" ) || die "Can not find file \"$quip_file\" - aborting"; ############################################################################# # This algorithm is taken from the O'Reilly book "Programming perl" by # Larry Wall and Randal L. Schwartz, Chapter 5: Common Tasks With Perl, # Section: Selecting A Random Line From A File. Quoting from the book: # # "This procedure selects a line at random from a file, using # just one pass over the file and without knowing in advance # the number of lines. It works by calculating the probability # that the current line (indicated by the $. variable) would # be selected if this line were the last line in the file. The # first line is selected with a probability of 100%, but the # second line has a 50% change of replacing the first one, the # third line a 33% chance of replacing one of the first two, # and so on." ############################################################################# srand; while ( ) { if ( rand( $. ) < 1.0 ) { $line = $.; $quip = $_; } } $quip =~ tr/\n\r//d; close( QUIPS ); ############################################################################# # All this is to get the xmessage box centered. "xdpyinfo" is used to get # the size of the display. The formulas used to get $x and $y are based # on the default font; if you use another one, you may have to tweak them. ############################################################################# open( XDPY, "xdpyinfo | " ); while ( ) { next if ! /dimensions:/; s/^.*dimensions: *//; s/ *pixels.*$//; tr/\n\r//d; @dimen = split( /x/ ); last; } close( XDPY ); # print( "dimen[0]: $dimen[0] dimen[1]: $dimen[1]\n" ); $dimen[0] = ( $dimen[0] / 2 ) - ((( length( $quip ) * 10 ) + 14 ) / 2 ); $dimen[1] = ( $dimen[1] / 2 ) - 36; $x = ( $dimen[0] > 0 ) ? $dimen[0] : $def_x; $y = ( $dimen[1] > 0 ) ? $dimen[1] : $def_y; exec( "xmessage -title 'Quip.pl (click to dismiss)' " . "-name 'Quip.pl' -buttons '' " . "-xrm \"Xmessage.geometry: +$x+$y\" " . "-xrm \"Xmessage*Font: $font\" " . "-xrm '*message.borderWidth: 0' " . "-xrm '*Translations: #override : exit(0)' " . "\"Quip #$line:\n\n$quip\"" ); exit;