module WPM

Constants

VERSION

Public Instance Methods

bye() click to toggle source
# File lib/wpm.rb, line 159
def bye
  puts "\nBye"
  exit
end
carriage_return() click to toggle source
# File lib/wpm.rb, line 146
def carriage_return
  @pos=0
  @line+=1
end
help() click to toggle source
# 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) click to toggle source
# File lib/wpm.rb, line 66
def parse_options(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() click to toggle source
# File lib/wpm.rb, line 155
def pos
  @pos
end
print_text() click to toggle source
quotes() click to toggle source
# 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() click to toggle source
# File lib/wpm.rb, line 122
def quotes_file
  "#{__dir__}/wpm/quotes.yml"
end
race(args) click to toggle source
# File lib/wpm.rb, line 16
def race(args)
  help if !(args & %w[-h --help]).empty?

  parse_options(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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source

width of the terminal

# File lib/wpm.rb, line 131
def term_width
  IO.console.winsize.last
end
text() click to toggle source
# File lib/wpm.rb, line 151
def text
  @lines[@line]
end
time() click to toggle source
# File lib/wpm.rb, line 96
def time
  Time.now.to_i
end
update_text() click to toggle source
# 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() click to toggle source
# File lib/wpm.rb, line 100
def wpm
  @lines.join(' ').size*60/((time-@start_time)*@word_length)
end
wpm_file() click to toggle source
# File lib/wpm.rb, line 104
def wpm_file
  "#{ENV['HOME']}/.wpm_history"
end