Class: SprichWoerter
- Inherits:
-
Object
- Object
- SprichWoerter
- Extended by:
- Logging
- Includes:
- Translating
- Defined in:
- lib/sprichwörter.rb
Overview
Main application class.
Constant Summary collapse
- @@log =
self.init_logger(STDOUT)
Instance Method Summary collapse
-
#initialize(lang = 'de', args) ⇒ SprichWoerter
constructor
An object of this class acts as a functor.
-
#maybe_in_color(txt, color) ⇒ Object
This function wraps a call to eval().
-
#qualify(phr) ⇒ Object
Rate a proverb.
-
#read_qualifile ⇒ Object
Find the file with the list of rated, generated proverbs.
- #show_favorites ⇒ Object
Methods included from Logging
init_logger, log_level=, log_target=
Methods included from Translating
language, trl, #trl, trl_words, #trl_words
Constructor Details
#initialize(lang = 'de', args) ⇒ SprichWoerter
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)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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 |
Instance Method Details
#maybe_in_color(txt, color) ⇒ Object
This function wraps a call to eval(). It also verifies that a color is set, although a default should apply, if not.
118 119 120 121 122 123 124 125 |
# 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) ⇒ Object
Rate a proverb. For the time, only a simple, positive rating is possible.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# 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 ⇒ Object
Find the file with the list of rated, generated proverbs. Returns an Array of those proverbs and the path to the file itself.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# 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 ⇒ Object
127 128 129 |
# File 'lib/sprichwörter.rb', line 127 def show_favorites puts read_qualifile[0].join("\n") end |