Class: VimGolf::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/vimgolf/ui.rb,
lib/vimgolf/cli.rb

Defined Under Namespace

Classes: UI

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initialize_uiObject



28
29
30
31
32
33
# File 'lib/vimgolf/cli.rb', line 28

def self.initialize_ui
  return if @ui_initialized
  Thor::Base.shell = VimGolf::CLI::UI
  VimGolf.ui = VimGolf::CLI::UI.new
  @ui_initialized = true
end

.reset_uiObject



35
36
37
38
39
40
# File 'lib/vimgolf/cli.rb', line 35

def self.reset_ui
  return unless @ui_initialized
  Thor::Base.shell = Thor::Shell::Color
  VimGolf.ui = nil
  @ui_initialized = false
end

.startObject



42
43
44
45
# File 'lib/vimgolf/cli.rb', line 42

def self.start(*)
  initialize_ui
  super
end

Instance Method Details

#local(infile, outfile) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/vimgolf/cli.rb', line 105

def local(infile, outfile)
  # make sure our files are sane
  if !(File.file?(infile) and File.file?(outfile))
    VimGolf.ui.error "INFILE and OUTFILE must exist and be regular files."
    exit 1
  end

  challenge = Challenge.new(infile) # use the filename as id
  challenge.local(infile, outfile)

  play(challenge)
end

#put(id) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/vimgolf/cli.rb', line 90

def put(id)
  FileUtils.mkdir_p Config.put_path
  VimGolf.ui.warn "Downloading Vimgolf challenge: #{id}"
  VimGolf::Challenge.path(Config.put_path)
  challenge = Challenge.new(id)
  challenge.download

  play(challenge)
end

#setupObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vimgolf/cli.rb', line 65

def setup
  VimGolf.ui.info "\nLet's setup your VimGolf key..."
  VimGolf.ui.warn "1) Open vimgolf.com in your browser."
  VimGolf.ui.warn "2) Click \"Sign in with Twitter\"."
  VimGolf.ui.warn "3) Once signed in, copy your key (black box, top right)."

  key = VimGolf.ui.ask "\nPaste your VimGolf key:"

  if key =~ /[\w\d]{32}/
    FileUtils.mkdir_p Config.path
    FileUtils.mkdir_p Config.put_path
    Config.save({'key' => key})

    VimGolf.ui.info "Saved. Happy golfing!"
  else
    VimGolf.ui.error "Invalid key, please double check your key on vimgolf.com"
  end
end

#versionObject



52
53
54
# File 'lib/vimgolf/cli.rb', line 52

def version
  VimGolf.ui.info "Client #{Vimgolf::VERSION}"
end