lib/vimgolf/cli.rb in vimgolf-0.1.0 vs lib/vimgolf/cli.rb in vimgolf-0.1.1

- old
+ new

@@ -1,8 +1,9 @@ module VimGolf - GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com" + GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com" + GOLFDEBUG = ENV['GOLFDEBUG'].to_sym rescue false class Error end class UI @@ -36,11 +37,11 @@ DESC def setup token = VimGolf.ui.ask "Please specify your VimGolf API token (register on vimgolf.com to get it):" - if token =~ /[\w\d]{1,32}/ + if token =~ /[\w\d]{32}/ FileUtils.mkdir_p Config.path FileUtils.mkdir_p Config.put_path Config.save({:key => token}) VimGolf.ui.info "Saved. Happy golfing!" @@ -56,17 +57,19 @@ DESC def put(id = nil) VimGolf.ui.warn "Launching VimGolf session for challenge: #{id}" - type = download(id) + begin + type = download(id) - if !type.nil? && !type.empty? # - n - no swap file, memory only editing # - --noplugin - don't load any plugins, lets be fair! + # -u NONE and -U none - don't load .vimrc or .gvimrc + # -i NONE - don't load .viminfo (for saved macros and the like) # - +0 - always start on line 0 - system("vim -n --noplugin +0 -W #{log(id)} #{input(id, type)}") + system("vim -n --noplugin -i NONE +0 -W #{log(id)} #{input(id, type)}") if $?.exitstatus.zero? diff = `diff --strip-trailing-cr #{input(id, type)} #{output(id)}` if diff.size > 0 @@ -86,11 +89,11 @@ if upload(id) == :ok VimGolf.ui.info "Uploaded entry, thanks for golfing!" VimGolf.ui.info "View the leaderboard: #{GOLFHOST}/challenges/#{id}" else - VimGolf.ui.error "Uh oh, upload to VimGolf failed, please check your key." + VimGolf.ui.error "Uh oh, upload filed. You're not cheating are you? :-)" end else VimGolf.ui.warn "Skipping upload. Thanks for playing. Give it another shot!" end @@ -101,14 +104,18 @@ report the error on github.com/igrigorik/vimgolf MSG VimGolf.ui.error error end + + rescue Exception => e + VimGolf.ui.error "Uh oh, something wen't wrong! Error: #{e}" + VimGolf.ui.error "If the error persists, please report it to github.com/igrigorik/vimgolf" end end - private + no_tasks do def download(id) begin url = URI.parse("#{GOLFHOST}/challenges/#{id}.json") req = Net::HTTP::Get.new(url.path) res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)} @@ -124,12 +131,12 @@ File.open(Config.put_path + "/#{id}.output", "w") {|f| f.puts data['out']['data']} data['in']['type'] rescue Exception => e - VimGolf.ui.error "Uh oh, couldn't download or parse challenge, please verify your challenge id and client version." - nil + debug(e) + raise "Uh oh, couldn't download or parse challenge, please verify your challenge id & client version." end end def upload(id) begin @@ -144,18 +151,25 @@ end JSON.parse(res.body)['status'].to_sym rescue Exception => e - VimGolf.ui.error "Uh oh, entry upload has failed, please check your key." + debug(e) + raise "Uh oh, entry upload has failed, please check your key." end end + end + private def input(id, type); challenge(id) + ".#{type}"; end def output(id); challenge(id) + ".output"; end def log(id); challenge(id) + ".log"; end def challenge(id) Config.put_path + "/#{id}" + end + + def debug(msg) + p [caller.first, msg] if GOLFDEBUG end end end