Version 2 of A little fortune teller

Updated 2003-07-24 09:08:24

if 0 {Richard Suchenwirth 2003-07-24 - Linux systems, as well as Cygwin, contain the fortune(6) command which gives you a random adage every time you call it. It draws it from a set of files found in /usr/share/fortune. The big files are plain text, sections delimited by a % sign. Here is a simple fortune viewer that runs on Windows too - you just have to obtain a fortune file from somewhere, and put its path into the source. The viewer uses a message widget, which I've almost never done before, but it has the advantage that it resizes automatically. Click on it for another fortune. Enjoy!

http://mini.net/files/tkfortune.jpg }

 set filename /usr/share/fortune/fortunes ;# put your own here

 package require Tk
 pack [message .m -font {Courier 10} -textvariable fortune -width 800]
 bind . <1> showFortune

 set size [file size $filename]
 set fp [open $filename]

 proc showFortune {} {
     global fp size fortune
     seek $fp [expr {int(rand()*$size)}]
     while {[gets $fp line]>=0 && $line!="%"} {}
     set fortune ""
     while {[gets $fp line]>=0 && $line!="%"} {
         append fortune $line\n
     }
 }
 showFortune

Arts and crafts of Tcl-Tk programming