Module: Run
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
- #stats ⇒ Object
-
#term_width ⇒ Object
width of the terminal.
- #text ⇒ Object
- #time ⇒ Object
- #update_text ⇒ Object
- #wpm ⇒ Object
Instance Method Details
#bye ⇒ Object
138 139 140 141 |
# File 'lib/run.rb', line 138 def bye puts "\nBye" exit end |
#carriage_return ⇒ Object
125 126 127 128 |
# File 'lib/run.rb', line 125 def carriage_return @pos=0 @line+=1 end |
#help ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/run.rb', line 47 def help puts " Usage: run\n\n Options:\n -h: show this message\n -s: print stats\n -l: message length [1: short, 2: medium, 3: long]\n -q: print quotes.yml path\n eof\n exit\nend\n" |
#parse_options(args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/run.rb', line 60 def (args) if args.first=='-s' stats 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: `run -h`" end |
#pos ⇒ Object
134 135 136 |
# File 'lib/run.rb', line 134 def pos @pos end |
#print_text ⇒ Object
105 106 107 |
# File 'lib/run.rb', line 105 def print_text print "\r"+text.sub(/(.{#{pos}})/,"#{@color}\\1#{@no_color}")+"\r" end |
#quotes ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/run.rb', line 93 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
101 102 103 |
# File 'lib/run.rb', line 101 def quotes_file "#{__dir__}/run/quotes.yml" end |
#race(args) ⇒ Object
11 12 13 14 15 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 |
# File 'lib/run.rb', line 11 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 puts "\nWPM: #{wpm}" end |
#stats ⇒ Object
81 82 83 |
# File 'lib/run.rb', line 81 def stats bye end |
#term_width ⇒ Object
width of the terminal
110 111 112 |
# File 'lib/run.rb', line 110 def term_width IO.console.winsize.last end |
#text ⇒ Object
130 131 132 |
# File 'lib/run.rb', line 130 def text @lines[@line] end |
#time ⇒ Object
85 86 87 |
# File 'lib/run.rb', line 85 def time Time.now.to_i end |
#update_text ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/run.rb', line 114 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
89 90 91 |
# File 'lib/run.rb', line 89 def wpm @lines.join(' ').size*60/((time-@start_time)*@word_length) end |