class SprichWoerter
Main application class.
Public Class Methods
new(lang='de', args)
click to toggle source
An object of this class acts as a functor. Calling new() is thus the only available access to all its functionality.
def initialize(lang='de', action = :create)
# File lib/sprichwörter.rb, line 39 def initialize(lang='de', args) @lang = lang @log = @@log @options = ArgParser.parse(ARGV) @log.debug("options are: " << @options.inspect) listfile = 'liste' << '_' << @lang # ------------- language is set -------------- @confdir = ENV['HOME'].dup << File::Separator << ".proverbs" if(@options.list) show_favorites exit 0 end user_list = @confdir.dup << File::Separator << listfile # cosmetics listrb = user_list << '.rb' if File::exist?(listrb) && File.readable?(listrb) require user_list else require_relative listfile end # ------------- proverbs are identified --------- if @options.add Adder.new(@lang, @options.add, *ARGV) exit true end # borders up_left = '┌' up_right = '┐' bottom_left = '└' bottom_right = '┘' hor = '─' vert = '│' srand(Time.now.nsec) # group 0 or group 1 ? sfx = [0, 1][rand(2)] proverbs = eval("$proverbs" + sfx.to_s) # This appears to improve the randomness : # allow for the list of proverbs to be 'rearranged' ... proverbs.extend(Rearranging) # ... and do it, each time. # This seeds the pseudo-random generator anew proverbs.randomize num_pvs = proverbs.size first = '' # Find 1 original proverb in the group. until !first.empty? # determine the index of an original proverb. fi = rand(num_pvs) # read the beginning of the proverb second = first = proverbs[fi][0] si = fi end srand(Time.now.nsec) # Find a second proverb, different from the first. until (si != fi && proverbs[si][0] != first && !second.empty?) si = rand(num_pvs) # read the end of the proverb second = proverbs[si][1] end # Construct the new proverb. phr = first << ' ' << second << '.' #draw len = phr.size # TODO: Wrap text if needed, determine number of line-breaks, then draw 1 # border around all lines of text. col = @options.color puts "" << maybe_in_color(up_left << hor * len << up_right, col) puts "" << maybe_in_color(vert, col) << phr << maybe_in_color(vert, col) puts "" << maybe_in_color(bottom_left << hor * len << bottom_right, col) # Allow the new proverb to be rated qualify( phr) if @options.qualify end
Public Instance Methods
maybe_in_color(txt, color)
click to toggle source
This function wraps a call to eval(). It also verifies that a color is set, although a default should apply, if not.
# File lib/sprichwörter.rb, line 118 def maybe_in_color(txt, color) if @options.color cmd = @options.color + ' "' << txt << '"' eval (cmd) else txt end end
qualify(phr)
click to toggle source
Rate a proverb. For the time, only a simple, positive rating is possible.
# File lib/sprichwörter.rb, line 161 def qualify(phr) # Create a cheap busy-indicator; Asynchronously count down from 5 to 1. thr = Thread.new do 5.downto(1) do |n| print n sleep 1 print "\b" if(n == 1) print("\n") exit true end end end msg = trl("Push '+' for a positive rating") << ".\n" puts msg # Wait for user-input while the count-down is running. # The loop serves to re while thr.alive? do user_input = wait_for_user if ('+' == user_input.chr.downcase ) # Terminate the count-down, if the user pushed '+' thr.terminate qf = nil lines, qf = read_qualifile if (qf && !lines.include?(phr)) # add the latest generated proverb File.write(qf, phr + "\n", :mode=>'a+') puts "\b" << trl('Done') exit true end # Handle Ctrl+c, Ctrl+d and Esc in the same way. elsif [3,4,27].include? user_input thr.terminate puts "\n" exit true end end end
read_qualifile()
click to toggle source
Find the file with the list of rated, generated proverbs. Returns an Array of those proverbs and the path to the file itself.
# File lib/sprichwörter.rb, line 133 def read_qualifile qualifile = @confdir.dup << File::Separator << 'qualifile_' << @lang lines = Array.new if(File.exist?(qualifile) ) msg = nil if(!File.readable?(qualifile) ) msg = trl("ERROR") << '!' << trl("The file %s cannot be opened for reading!") %qualifile elsif (!File.writable?(qualifile) ) msg = trl("ERROR") << '!' << trl("The file %s cannot be opened for writing!") %qualifile end if msg puts msg exit false else qf = File.open(qualifile, 'a+') lines = qf.readlines(chomp:true) end else if !File.exist?(@confdir) Dir.mkdir(@confdir) end qf = File.new(qualifile, 'w') end qf.close return lines, qualifile end
show_favorites()
click to toggle source
# File lib/sprichwörter.rb, line 127 def show_favorites puts read_qualifile[0].join("\n") end