vimgolf 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- vimgolf (0.1.1)
4
+ vimgolf (0.2.0)
5
5
  highline
6
6
  json
7
7
  thor (>= 0.14.6)
data/lib/vimgolf.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'fileutils'
2
2
  require 'net/http'
3
+ require 'strscan'
3
4
  require 'json'
4
5
  require 'thor'
5
6
 
6
7
  require 'vimgolf/version'
7
8
  require 'vimgolf/config'
9
+ require 'vimgolf/keylog'
8
10
  require 'vimgolf/cli'
9
11
  require 'vimgolf/ui'
data/lib/vimgolf/cli.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  module VimGolf
2
2
 
3
- GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com"
3
+ GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com"
4
4
  GOLFDEBUG = ENV['GOLFDEBUG'].to_sym rescue false
5
+ DIFF = ENV['DIFF'] || 'diff'
6
+ PROXY = ENV['http_proxy'] || ''
5
7
 
6
8
  class Error
7
9
  end
@@ -72,7 +74,7 @@ module VimGolf
72
74
  system(vimcmd)
73
75
 
74
76
  if $?.exitstatus.zero?
75
- diff = `diff --strip-trailing-cr #{input(id, type)} #{output(id)}`
77
+ diff = `#{DIFF} #{input(id, type)} #{output(id)}`
76
78
 
77
79
  if diff.size > 0
78
80
  VimGolf.ui.warn "Uh oh, looks like your entry does not match the desired output:"
@@ -83,7 +85,7 @@ module VimGolf
83
85
  return
84
86
  end
85
87
 
86
- score = File.size(log(id))
88
+ score = Keylog.score(IO.read(log(id)))
87
89
  VimGolf.ui.info "Success! Your output matches. Your score: #{score}"
88
90
 
89
91
  if VimGolf.ui.yes? "Upload result to VimGolf? (yes / no)"
@@ -93,7 +95,7 @@ module VimGolf
93
95
  VimGolf.ui.info "Uploaded entry, thanks for golfing!"
94
96
  VimGolf.ui.info "View the leaderboard: #{GOLFHOST}/challenges/#{id}"
95
97
  else
96
- VimGolf.ui.error "Uh oh, upload filed. You're not cheating are you? :-)"
98
+ VimGolf.ui.error "Uh oh, upload failed. You're not cheating are you? :-)"
97
99
  end
98
100
 
99
101
  else
@@ -110,7 +112,7 @@ module VimGolf
110
112
  end
111
113
 
112
114
  rescue Exception => e
113
- VimGolf.ui.error "Uh oh, something wen't wrong! Error: #{e}"
115
+ VimGolf.ui.error "Uh oh, something went wrong! Error: #{e}"
114
116
  VimGolf.ui.error "If the error persists, please report it to github.com/igrigorik/vimgolf"
115
117
  end
116
118
  end
@@ -120,7 +122,11 @@ module VimGolf
120
122
  begin
121
123
  url = URI.parse("#{GOLFHOST}/challenges/#{id}.json")
122
124
  req = Net::HTTP::Get.new(url.path)
123
- res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)}
125
+
126
+ proxy_url, proxy_user, proxy_pass = get_proxy
127
+ proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_user, proxy_pass)
128
+ res = proxy.start(url.host, url.port) { |http| http.request(req) }
129
+
124
130
  data = JSON.parse(res.body)
125
131
 
126
132
  if data['client'] != Vimgolf::VERSION
@@ -144,15 +150,18 @@ module VimGolf
144
150
  def upload(id)
145
151
  begin
146
152
  url = URI.parse("#{GOLFHOST}/entry.json")
147
- http = Net::HTTP.new(url.host, url.port)
148
153
 
149
- request = Net::HTTP::Post.new(url.request_uri)
150
- request.set_form_data({"challenge_id" => id, "apikey" => Config.load['key'], "entry" => IO.read(log(id))})
151
- request["Accept"] = "application/json"
154
+ proxy_url, proxy_user, proxy_pass = get_proxy
155
+ proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_user, proxy_pass)
152
156
 
153
- res = http.request(request)
154
- JSON.parse(res.body)['status'].to_sym
157
+ proxy.start(url.host, url.port) do |http|
158
+ request = Net::HTTP::Post.new(url.request_uri)
159
+ request.set_form_data({"challenge_id" => id, "apikey" => Config.load['key'], "entry" => IO.read(log(id))})
160
+ request["Accept"] = "application/json"
155
161
 
162
+ res = http.request(request)
163
+ JSON.parse(res.body)['status'].to_sym
164
+ end
156
165
  rescue Exception => e
157
166
  debug(e)
158
167
  raise "Uh oh, entry upload has failed, please check your key."
@@ -170,6 +179,20 @@ module VimGolf
170
179
  Config.put_path + "/#{id}"
171
180
  end
172
181
 
182
+ def get_proxy
183
+ begin
184
+ proxy_url = URI.parse(PROXY)
185
+ rescue Exception => e
186
+ VimGolf.ui.error "Invalid proxy uri in http_proxy environment variable - will try to run with out proxy"
187
+ proxy_url = URI.parse("");
188
+ end
189
+
190
+ proxy_url.port ||= 80
191
+ proxy_user, proxy_pass = proxy_url.userinfo.split(/:/) if proxy_url.userinfo
192
+
193
+ return proxy_url, proxy_user, proxy_pass
194
+ end
195
+
173
196
  def debug(msg)
174
197
  p [caller.first, msg] if GOLFDEBUG
175
198
  end
@@ -0,0 +1,105 @@
1
+ module VimGolf
2
+ class Session < Array
3
+ def to_s(sep = '')
4
+ @log.join(sep)
5
+ end
6
+ end
7
+
8
+ class Keylog
9
+ def self.parse(input)
10
+ session = Session.new
11
+ scan(input) {|s| session << s }
12
+ session
13
+ end
14
+
15
+ def self.convert(input)
16
+ parse(input).join('')
17
+ end
18
+
19
+ def self.score(input)
20
+ keys = 0
21
+ scan(input) {|s| keys += 1 }
22
+ keys
23
+ end
24
+
25
+ def self.scan(input)
26
+ scanner = StringScanner.new(input)
27
+ output = ""
28
+
29
+ until scanner.eos?
30
+ c = scanner.get_byte
31
+ n = c.unpack('C').first
32
+
33
+ out_char = \
34
+ case n
35
+
36
+ # Special platform-independent encoding stuff
37
+ when 0x80
38
+ code = scanner.get_byte + scanner.get_byte
39
+
40
+ # This list has been populated by experimentation so far,
41
+ # because I haven't bothered looking for a more authoritative
42
+ # source.
43
+ case code
44
+ when "k1"; "<F1>"
45
+ when "k2"; "<F2>"
46
+ when "k3"; "<F3>"
47
+ when "k4"; "<F4>"
48
+ when "k5"; "<F5>"
49
+ when "k6"; "<F6>"
50
+ when "k7"; "<F7>"
51
+ when "k8"; "<F8>"
52
+ when "k9"; "<F9>"
53
+ when "k;"; "<F10>"
54
+ when "F1"; "<F11>"
55
+ when "F2"; "<F12>"
56
+
57
+ when "kP"; "<PageUp>"
58
+ when "kN"; "<PageDown>"
59
+ when "kh"; "<Home>"
60
+ when "@7"; "<End>"
61
+ when "kI"; "<Insert>"
62
+ when "kD"; "<Del>"
63
+ when "kb"; "<BS>"
64
+
65
+ when "ku"; "<Up>"
66
+ when "kd"; "<Down>"
67
+ when "kl"; "<Left>"
68
+ when "kr"; "<Right>"
69
+ when "#4"; "<S-Left>"
70
+ when "%i"; "<S-Right>"
71
+
72
+ when "kB"; "<S-Tab>"
73
+ when "\xffX"; "<C-Space>"
74
+
75
+ else
76
+ #puts "Unknown Vim code: #{code.inspect}"
77
+ '<%02x-%02x>' % code.unpack('CC')
78
+ end
79
+
80
+ # Control characters with special names
81
+ when 0; "<Nul>"
82
+ when 9; "<Tab>"
83
+ when 10; "<NL>"
84
+ when 13; "<CR>"
85
+ when 27; "<Esc>"
86
+
87
+ when 127; "<Del>"
88
+
89
+ # Otherwise, use <C-x> format
90
+ when 0..31; "<C-#{(n + 64).chr}>"
91
+
92
+ # The rest of ANSI is printable
93
+ when 32..126; c
94
+
95
+ else
96
+ #puts "Unexpected extended ASCII: #{'%#04x' % n}"
97
+ '<%#04x>' % n
98
+
99
+ end
100
+
101
+ yield out_char
102
+ end
103
+ end
104
+ end
105
+ end
@@ -1,3 +1,3 @@
1
1
  module Vimgolf
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/spec/cli_spec.rb CHANGED
@@ -30,7 +30,7 @@ describe VimGolf do
30
30
  end
31
31
 
32
32
  it "should return type of challenge on success" do
33
- c.download('4d1a21e88ae121365c00000e').should == "rb"
33
+ c.download('4d1a1c36567bac34a9000002').should == "rb"
34
34
  end
35
35
 
36
36
  it "should raise error on invalid upload id" do
@@ -0,0 +1 @@
1
+ ��5ibbb�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb�kb:wq
@@ -0,0 +1 @@
1
+ �kdd3di�kb�kb�kd�kr�kr�kb�kb �kd�kl�kb�ku�kr�kb�ku�kl�kr�kr�kb�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr*�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kl�kb�kb�kb�kb�kb�kb�kba�kd.join(',')�kr�krd$i�kr":wq
@@ -0,0 +1 @@
1
+ ��5�kd�kd�kd�kdA "Vi IMproved"�kd�kd�kd�kdAa�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kli�kb�kb�kb�kb�kb�kb�kb�kb�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kroriginally �kd�kd�kdyyp�kddd:wq
@@ -0,0 +1 @@
1
+ ��5�kdd3di�kb�kb�kd�kd�ku�ku�kb�ku�kr�kd�kd�kr�kb�kb�kd�kr�kb�ku�ku�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kl�kl*�kr�kr�kr�kr�kr�kr�kr�kb�kb�kb�kb�kb�kb�kd.join(',')�kr�krd$i"�kb�kr":wq
@@ -0,0 +1 @@
1
+ ��5�kd�kd�kd�kdAi �kb�kb "Vi IMproved"�kd�kd�kd�kd�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kl�kr�kr�kr�kr�kr�kr originallyG�ku�ku�ku�kudwdw�kd�kd�kd�kd�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kr�kld$:wq
@@ -0,0 +1,15 @@
1
+ require "helper"
2
+
3
+ describe VimGolf::Keylog do
4
+ include VimGolf
5
+
6
+ Dir['spec/fixtures/*'].each do |f|
7
+ it "should parse #{f} logfile" do
8
+ lambda { Keylog.convert(IO.read(f)) }.should_not raise_error
9
+ end
10
+
11
+ it "should score #{f} logfile" do
12
+ lambda { Keylog.score(IO.read(f)) }.should_not raise_error
13
+ end
14
+ end
15
+ end
data/vimgolf.gemspec CHANGED
@@ -26,8 +26,10 @@ Gem::Specification.new do |s|
26
26
 
27
27
  s.post_install_message = %{
28
28
  ------------------------------------------------------------------------------
29
- Thank you for installing vimgolf-#{Vimgolf::VERSION}. As of 0.1.3, we are shipping a custom
30
- vimgolf .vimrc file for each challenge to help level the playing field.
29
+ Thank you for installing vimgolf-#{Vimgolf::VERSION}.
30
+
31
+ 0.1.3: custom vimgolf .vimrc file to help level the playing field
32
+ 0.2.0: proxy support, custom diffs + proper vimscript parser/scoring
31
33
 
32
34
  For more information, rules & updates: http://vimgolf.com/about
33
35
  ------------------------------------------------------------------------------
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 3
9
- version: 0.1.3
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ilya Grigorik
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-30 00:00:00 -05:00
17
+ date: 2010-12-31 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -91,10 +91,17 @@ files:
91
91
  - lib/vimgolf.rb
92
92
  - lib/vimgolf/cli.rb
93
93
  - lib/vimgolf/config.rb
94
+ - lib/vimgolf/keylog.rb
94
95
  - lib/vimgolf/ui.rb
95
96
  - lib/vimgolf/version.rb
96
97
  - spec/cli_spec.rb
98
+ - spec/fixtures/4d19832d8ae121365c00000b.log
99
+ - spec/fixtures/4d1a1c36567bac34a9000002.log
100
+ - spec/fixtures/4d1a1c69567bac34a9000004.log
101
+ - spec/fixtures/4d1a21e88ae121365c00000e.log
102
+ - spec/fixtures/4d1a34ccfa85f32065000004.log
97
103
  - spec/helper.rb
104
+ - spec/keylog_spec.rb
98
105
  - vimgolf.gemspec
99
106
  has_rdoc: true
100
107
  homepage: http://github.com/igrigorik/vimgolf
@@ -102,8 +109,9 @@ licenses: []
102
109
 
103
110
  post_install_message: "\n\
104
111
  ------------------------------------------------------------------------------\n\
105
- Thank you for installing vimgolf-0.1.3. As of 0.1.3, we are shipping a custom\n\
106
- vimgolf .vimrc file for each challenge to help level the playing field.\n\n\
112
+ Thank you for installing vimgolf-0.2.0. \n\n\
113
+ 0.1.3: custom vimgolf .vimrc file to help level the playing field\n\
114
+ 0.2.0: proxy support, custom diffs + proper vimscript parser/scoring\n\n\
107
115
  For more information, rules & updates: http://vimgolf.com/about\n\
108
116
  ------------------------------------------------------------------------------\n"
109
117
  rdoc_options: []
@@ -135,4 +143,10 @@ specification_version: 3
135
143
  summary: CLI client for vimgolf.com
136
144
  test_files:
137
145
  - spec/cli_spec.rb
146
+ - spec/fixtures/4d19832d8ae121365c00000b.log
147
+ - spec/fixtures/4d1a1c36567bac34a9000002.log
148
+ - spec/fixtures/4d1a1c69567bac34a9000004.log
149
+ - spec/fixtures/4d1a21e88ae121365c00000e.log
150
+ - spec/fixtures/4d1a34ccfa85f32065000004.log
138
151
  - spec/helper.rb
152
+ - spec/keylog_spec.rb