Class: Liberator::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/liberator/controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ Controller

Returns a new instance of Controller.



3
4
5
6
7
8
# File 'lib/liberator/controller.rb', line 3

def initialize(view)
  @view = view
  @view.update_status_bar "Analyzing #{Dir.pwd}..."
  @directory = Directory.new Dir.pwd
  render
end

Instance Method Details

#handle_key(key) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/liberator/controller.rb', line 16

def handle_key(key)
  case key
  when 'q'
    @view.close
    exit
  when 'j'
    @directory.select_next_entry
    render
  when 'k'
    @directory.select_previous_entry
    render
  when 10
    if File.directory? @directory.selected_entry[:path]
      @view.update_status_bar "Analyzing #{@directory.selected_entry[:path]}..."
      begin
        @directory = Directory.new @directory.selected_entry[:path]
      rescue IOError
        @view.update_status_bar "Cannot change directories due to permissions"
      end
      render
    end
  when 'h'
    @view.update_status_bar "Analyzing #{File.expand_path(@directory.path + '/..')}..."
    @directory = @directory.parent
    render
  when 'x'
    @directory.delete_selected_entry if @view.confirm_delete
    @directory.refresh
    render
  end
end

#listenObject



10
11
12
13
14
# File 'lib/liberator/controller.rb', line 10

def listen
  loop do
    handle_key @view.capture_keystroke
  end
end

#renderObject



48
49
50
# File 'lib/liberator/controller.rb', line 48

def render
  @view.refresh(@directory.path, @directory.entries, @directory.selected_index)
end