Module: WPM
Constant Summary collapse
- VERSION =
"0.1.2"
Instance Method Summary collapse
- #bye ⇒ Object
- #carriage_return ⇒ Object
- #help ⇒ Object
- #parse_options(args) ⇒ Object
- #pos ⇒ Object
- #print_text ⇒ Object
- #quotes ⇒ Object
- #quotes_file ⇒ Object
- #race(args) ⇒ Object
- #save_wpm ⇒ Object
- #stats ⇒ Object
-
#term_width ⇒ Object
width of the terminal.
- #text ⇒ Object
- #time ⇒ Object
- #update_text ⇒ Object
- #wpm ⇒ Object
- #wpm_file ⇒ Object
Instance Method Details
#bye ⇒ Object
159 160 161 162 |
# File 'lib/wpm.rb', line 159 def bye puts "\nBye" exit end |
#carriage_return ⇒ Object
146 147 148 149 |
# File 'lib/wpm.rb', line 146 def carriage_return @pos=0 @line+=1 end |
#help ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/wpm.rb', line 53 def help puts <<~eof Usage: wpm [options] Options: -h show this message -s print stats -l [length] maximum string length -q print quotes.yml path eof exit end |
#parse_options(args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/wpm.rb', line 66 def (args) if args.first=='-s' stats exit end if args.first=='-q' puts quotes_file exit end if args.shift=='-l' and args.first.to_i > 0 @max_length=args.first.to_i return end puts "Unknown option" bye rescue => e "Bad options. For help: `wpm -h`" exit end |
#pos ⇒ Object
155 156 157 |
# File 'lib/wpm.rb', line 155 def pos @pos end |
#print_text ⇒ Object
126 127 128 |
# File 'lib/wpm.rb', line 126 def print_text print "\r"+text.sub(/(.{#{pos}})/,"#{@color}\\1#{@no_color}")+"\r" end |
#quotes ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/wpm.rb', line 114 def quotes YAML.load(open(quotes_file)) .sort_by(&:length) .select {|e| e.size<@max_length} rescue => e "Error while reading #{quotes_file}" end |
#quotes_file ⇒ Object
122 123 124 |
# File 'lib/wpm.rb', line 122 def quotes_file "#{__dir__}/wpm/quotes.yml" end |
#race(args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/wpm.rb', line 16 def race(args) help if !(args & %w[-h --help]).empty? (args) unless args.empty? @max_length||=4000 @eot=0x03 @eof=0x04 if quotes.empty? puts "No quotes with less than #{@max_length} characters found" exit end @color="\e[4;32m" @no_color="\e[m" @lines=quotes[rand(quotes.size)].chars.each_slice(term_width).map(&:join) @line=0 @pos=0 # average length of English words @word_length=5 @start_time=time print_text loop do update_text break if @line==@lines.size-1 and pos==@lines.last.size end save_wpm puts "\nWPM: #{wpm}" end |
#save_wpm ⇒ Object
108 109 110 111 112 |
# File 'lib/wpm.rb', line 108 def save_wpm open(wpm_file,'a') do |f| f.puts "#{Time.now.strftime("%Y-%M-%d")} #{wpm}" end end |
#stats ⇒ Object
89 90 91 92 93 94 |
# File 'lib/wpm.rb', line 89 def stats str=File.open(wpm_file).read scores=str.split(" ").map(&:to_i).select.with_index{|e,i| i.odd?} printf "%-20s %s", "Number of races:", "#{str.count("\n")}\n" printf "%-20s %s", "Average speed (WPM):", "#{scores.average}\n" end |
#term_width ⇒ Object
width of the terminal
131 132 133 |
# File 'lib/wpm.rb', line 131 def term_width IO.console.winsize.last end |
#text ⇒ Object
151 152 153 |
# File 'lib/wpm.rb', line 151 def text @lines[@line] end |
#time ⇒ Object
96 97 98 |
# File 'lib/wpm.rb', line 96 def time Time.now.to_i end |
#update_text ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/wpm.rb', line 135 def update_text system('stty raw -echo') c=STDIN.getc bye if c==@eof.chr or c==@eot.chr carriage_return if @pos>=term_width-1 @pos+=1 if c==text[pos] ensure system('stty -raw echo') print_text end |
#wpm ⇒ Object
100 101 102 |
# File 'lib/wpm.rb', line 100 def wpm @lines.join(' ').size*60/((time-@start_time)*@word_length) end |
#wpm_file ⇒ Object
104 105 106 |
# File 'lib/wpm.rb', line 104 def wpm_file "#{ENV['HOME']}/.wpm_history" end |