Module: Run

Extended by:
Run
Included in:
Run
Defined in:
lib/run.rb,
lib/run/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#byeObject



138
139
140
141
# File 'lib/run.rb', line 138

def bye
  puts "\nBye"
  exit
end

#carriage_returnObject



125
126
127
128
# File 'lib/run.rb', line 125

def carriage_return
  @pos=0
  @line+=1
end

#helpObject



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 parse_options(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

#posObject



134
135
136
# File 'lib/run.rb', line 134

def pos
  @pos
end


105
106
107
# File 'lib/run.rb', line 105

def print_text
  print "\r"+text.sub(/(.{#{pos}})/,"#{@color}\\1#{@no_color}")+"\r"
end

#quotesObject



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_fileObject



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?

  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

  puts "\nWPM: #{wpm}"
end

#statsObject



81
82
83
# File 'lib/run.rb', line 81

def stats
  bye
end

#term_widthObject

width of the terminal



110
111
112
# File 'lib/run.rb', line 110

def term_width
  IO.console.winsize.last
end

#textObject



130
131
132
# File 'lib/run.rb', line 130

def text
  @lines[@line]
end

#timeObject



85
86
87
# File 'lib/run.rb', line 85

def time
  Time.now.to_i
end

#update_textObject



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

#wpmObject



89
90
91
# File 'lib/run.rb', line 89

def wpm
  @lines.join(' ').size*60/((time-@start_time)*@word_length)
end