wpm 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66c769d53c2a9c286c422b3fc90f15ead2344154d997c248fd8c6745cf30f5f5
4
- data.tar.gz: 2757079c781faeaba584d85fd0d6dabe0da95f40091146cb609c1428bd9b74d9
3
+ metadata.gz: 4cc3c4877fb13c8efb66e4c318d5abb2a9fccc2b131e39da402d387be81e0018
4
+ data.tar.gz: 48cc76b43407e2c7853cea45fa365744bd9a2b36161a9f5de045e8b2145382b4
5
5
  SHA512:
6
- metadata.gz: 00245cb01901c5526c44d805e5abc5f0cd633f7920871dcfc0baa148e2757a013b0d36bd15ca4afa92964f058afa4ca802a0557dcd091e6d22eb688036abf25b
7
- data.tar.gz: 33d3e0645aba43f9a9dd3383478c9e199c6f992e47e8d982dce85128b954f19ecfe1ab22cb222663c43c5a95fc1395cffba4bac6b38213ab0e446120a0b8afd8
6
+ metadata.gz: 5af70b6da9e4caf524ba7fe38120a36a63694314caf7f428f61f7a2665c00ea9b04248ea3418db3e129687d494ca58d254a067782ef186c9d97f198061a3c481
7
+ data.tar.gz: 280817de709846f9b27483b4e0fc95483aee28f4ca8a12c4a950810a0e7639112937a16c54e3d8deb69df5e1e7203e802accea08b718f123053593a14bb07540
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ wpm (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ansi (1.5.0)
10
+ benchmark-ips (2.7.2)
11
+ builder (3.2.4)
12
+ byebug (11.0.1)
13
+ minitest (5.14.0)
14
+ minitest-reporters (1.3.8)
15
+ ansi
16
+ builder
17
+ minitest (>= 5.0)
18
+ ruby-progressbar
19
+ rake (13.0.1)
20
+ ruby-progressbar (1.10.1)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ benchmark-ips
27
+ bundler (~> 2.0)
28
+ byebug (~> 11.0.1)
29
+ minitest (~> 5.0)
30
+ minitest-reporters (~> 1.3.8)
31
+ rake (~> 13.0.1)
32
+ wpm!
33
+
34
+ BUNDLED WITH
35
+ 2.1.4
data/exe/wpm CHANGED
@@ -1,3 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "wpm"
4
+
5
+ WPM.race(ARGV)
@@ -0,0 +1,3 @@
1
+ - "Do nothing out of selfish ambition or vain conceit, but in humility consider others better than yourselves. - philippians 2:3"
2
+ - "It's a funny thing, but people mostly have it backward. They think they live by what they want. But really, what guides them is what they're afraid of. What they don't want. - khaled hosseini typeracer"
3
+ - "roses are red, violets are blue"
data/lib/wpm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module Wpm
2
- VERSION = "0.1.0"
1
+ module WPM
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/wpm.rb CHANGED
@@ -1,6 +1,163 @@
1
- require "wpm/version"
1
+ require 'io/console'
2
+ require 'wpm/version'
3
+ require 'yaml'
2
4
 
3
- module Wpm
4
- class Error < StandardError; end
5
- # Your code goes here...
5
+ class WPMError < StandardError; end
6
+
7
+ class Array
8
+ def average
9
+ (reduce(:+).to_f/size).round(2)
10
+ end
11
+ end
12
+
13
+ module WPM
14
+ extend self
15
+
16
+ def race(args)
17
+ help if !(args & %w[-h --help]).empty?
18
+
19
+ parse_options(args) unless args.empty?
20
+
21
+ @max_length||=4000
22
+
23
+ @eot=0x03
24
+ @eof=0x04
25
+
26
+ if quotes.empty?
27
+ puts "No quotes with less than #{@max_length} characters found"
28
+ exit
29
+ end
30
+
31
+ @color="\e[4;32m"
32
+ @no_color="\e[m"
33
+
34
+ @lines=quotes[rand(quotes.size)].chars.each_slice(term_width).map(&:join)
35
+ @line=0
36
+ @pos=0
37
+
38
+ # average length of English words
39
+ @word_length=5
40
+
41
+ @start_time=time
42
+ print_text
43
+
44
+ loop do
45
+ update_text
46
+ break if @line==@lines.size-1 and pos==@lines.last.size
47
+ end
48
+
49
+ save_wpm
50
+ puts "\nWPM: #{wpm}"
51
+ end
52
+
53
+ def help
54
+ puts <<~eof
55
+ Usage: wpm
56
+
57
+ Options:
58
+ -h: show this message
59
+ -s: print stats
60
+ -l: message length [1: short, 2: medium, 3: long]
61
+ -q: print quotes.yml path
62
+ eof
63
+ exit
64
+ end
65
+
66
+ def parse_options(args)
67
+ if args.first=='-s'
68
+ stats
69
+ exit
70
+ end
71
+
72
+ if args.first=='-q'
73
+ puts quotes_file
74
+ exit
75
+ end
76
+
77
+ if args.shift=='-l' and args.first.to_i > 0
78
+ @max_length=args.first.to_i
79
+ return
80
+ end
81
+
82
+ puts "Unknown option"
83
+ bye
84
+ rescue => e
85
+ "Bad options. For help: `wpm -h`"
86
+ exit
87
+ end
88
+
89
+ def stats
90
+ str=File.open(wpm_file).read
91
+ scores=str.split(" ").map(&:to_i).select.with_index{|e,i| i.odd?}
92
+ puts "Number of races: #{str.count("\n")}"
93
+ puts "Average speed: #{scores.average}WPM"
94
+ end
95
+
96
+ def time
97
+ Time.now.to_i
98
+ end
99
+
100
+ def wpm
101
+ @lines.join(' ').size*60/((time-@start_time)*@word_length)
102
+ end
103
+
104
+ def wpm_file
105
+ "#{ENV['HOME']}/.wpm_history"
106
+ end
107
+
108
+ def save_wpm
109
+ open(wpm_file,'a') do |f|
110
+ f.puts "#{Time.now.strftime("%Y-%M-%d")} #{wpm}"
111
+ end
112
+ end
113
+
114
+ def quotes
115
+ YAML.load(open(quotes_file))
116
+ .sort_by(&:length)
117
+ .select {|e| e.size<@max_length}
118
+ rescue => e
119
+ "Error while reading #{quotes_file}"
120
+ end
121
+
122
+ def quotes_file
123
+ "#{__dir__}/wpm/quotes.yml"
124
+ end
125
+
126
+ def print_text
127
+ print "\r"+text.sub(/(.{#{pos}})/,"#{@color}\\1#{@no_color}")+"\r"
128
+ end
129
+
130
+ # width of the terminal
131
+ def term_width
132
+ IO.console.winsize.last
133
+ end
134
+
135
+ def update_text
136
+ system('stty raw -echo')
137
+ c=STDIN.getc
138
+ bye if c==@eof.chr or c==@eot.chr
139
+ carriage_return if @pos>=term_width-1
140
+ @pos+=1 if c==text[pos]
141
+ ensure
142
+ system('stty -raw echo')
143
+ print_text
144
+ end
145
+
146
+ def carriage_return
147
+ @pos=0
148
+ @line+=1
149
+ end
150
+
151
+ def text
152
+ @lines[@line]
153
+ end
154
+
155
+ def pos
156
+ @pos
157
+ end
158
+
159
+ def bye
160
+ puts "\nBye"
161
+ exit
162
+ end
6
163
  end
data/wpm.gemspec CHANGED
@@ -2,7 +2,7 @@ require_relative 'lib/wpm/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "wpm"
5
- spec.version = Wpm::VERSION
5
+ spec.version = WPM::VERSION
6
6
  spec.authors = ["sergioro"]
7
7
  spec.email = ["yo@sergioro.com"]
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wpm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sergioro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-13 00:00:00.000000000 Z
11
+ date: 2020-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,6 +106,7 @@ files:
106
106
  - ".rubocop.yml"
107
107
  - ".travis.yml"
108
108
  - Gemfile
109
+ - Gemfile.lock
109
110
  - LICENSE.txt
110
111
  - README.md
111
112
  - Rakefile
@@ -113,6 +114,7 @@ files:
113
114
  - bin/setup
114
115
  - exe/wpm
115
116
  - lib/wpm.rb
117
+ - lib/wpm/quotes.yml
116
118
  - lib/wpm/version.rb
117
119
  - wpm.gemspec
118
120
  homepage: