Class: Kat::App

Inherits:
Object
  • Object
show all
Defined in:
lib/kat/app.rb

Constant Summary collapse

MIN_WIDTH =
80
CONFIG =
File.join ENV['HOME'], '.katrc'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV) ⇒ App

Create a new Kat::App object, using command-line switches as options by default



33
34
35
36
37
38
39
40
41
42
# File 'lib/kat/app.rb', line 33

def initialize(args = ARGV)
  @kat = nil
  @page = 0
  @window_width = 0
  @show_info = !hide_info?
  @h = HighLine.new

  init_options args
  init_search
end

Instance Attribute Details

#katObject (readonly)

The Kat::Search search object



25
26
27
# File 'lib/kat/app.rb', line 25

def kat
  @kat
end

#optionsObject (readonly)

The app options hash



28
29
30
# File 'lib/kat/app.rb', line 28

def options
  @options
end

#pageObject

The current page number (0-based)



22
23
24
# File 'lib/kat/app.rb', line 22

def page
  @page
end

Instance Method Details

#init_options(args = nil) ⇒ Object

Initialise the app’s options



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kat/app.rb', line 47

def init_options(args = nil)
  @args = case args
          when nil    then []
          when String then args.split
          else             args
          end

  @options = load_config || {}

  Kat.options(@args).tap do |o|
    @options.merge!(o) { |k, ov, nv| o["#{ k }_given".intern] ? nv : ov }
  end

  Kat::Colour.colour = @options[:colour]
rescue NoMethodError => e
  @options = {}
  warn "Wrong config file format: #{ e }"
end

#init_searchObject

Initialise the Kat::Search object with the query and it’s options



69
70
71
72
73
# File 'lib/kat/app.rb', line 69

def init_search
  @kat ||= Kat.search
  @kat.query = @args.join(' ')
  @kat.options = @options
end

#mainObject

The main method. Prints a list of options for categories, platforms, languages or times if the user has asked for them, otherwise will loop over the running method until the user quits (or there’s no results).



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kat/app.rb', line 80

def main
  puts VERSION_STR

  Kat::Search.selects.select { |k, v| @options[v[:select]] }.tap do |lists|
    if lists.empty?
      while running; end
    else
      puts format_lists lists
    end
  end
end