Class: VimGolfFinder::CLI

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

Defined Under Namespace

Classes: UI

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.startObject



18
19
20
21
# File 'lib/vimgolf_finder/cli.rb', line 18

def self.start(*)
  Thor::Base.shell = VimGolfFinder::CLI::UI
  super
end

Instance Method Details

#listObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vimgolf_finder/cli.rb', line 31

def list
  order = options[:order]
  desc = options[:desc]
  limit = options[:limit].to_i
  page = options[:page].to_i

  challenges = VimGolfFinder.parser.fetch_challenges

  case order
    when 'title'
      challenges.sort! { |a, b| b.title <=> a.title } if desc
      challenges.sort! { |a, b| a.title <=> b.title } if !desc
    when 'entries'
      challenges.sort! { |a, b| b.entries <=> a.entries } if desc
      challenges.sort! { |a, b| a.entries <=> b.entries } if !desc
    when 'id'
      challenges.sort! { |a, b| b.id <=> a.id } if desc
      challenges.sort! { |a, b| a.id <=> b.id } if !desc
  end
  challenges = challenges[(limit * (page-1))...(limit * page)]
  challenges.each_with_index do |challenge, index|
    challenge.print(index+1)
  end

  number = VimGolfFinder.ui.ask 'Choose challenge number.'
  number = number.to_i
  if number.instance_of? Fixnum
    if number < 1 or number > limit
      VimGolfFinder.ui.error 'Invalid number'
      return
    end

    id = challenges[number-1].id
    print_challenge(id)
    play(id)
  end
end

#randomObject



70
71
72
73
74
75
76
77
78
# File 'lib/vimgolf_finder/cli.rb', line 70

def random
  challenges = VimGolfFinder.parser.fetch_challenges
  challenge = challenges.shuffle.first
  challenge.print

  id = challenge.id
  print_challenge(id)
  play(id)
end