Class: Ruco::Application

Inherits:
Object show all
Defined in:
lib/ruco/application.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, options) ⇒ Application

Returns a new instance of Application.



3
4
5
6
7
8
9
10
11
# File 'lib/ruco/application.rb', line 3

def initialize(file, options)
  @file = file
  @options = options

  setup_actions
  setup_keys
  load_user_config
  create_components
end

Instance Method Details

#action(name, &block) ⇒ Object



64
65
66
# File 'lib/ruco/application.rb', line 64

def action(name, &block)
  @actions[name] = block
end

#ask(question, options = {}, &block) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/ruco/application.rb', line 68

def ask(question, options={}, &block)
  @focused = command
  command.ask(question, options) do |response|
    @focused = editor
    block.call(response)
  end
end

#bind(key, action = nil, &block) ⇒ Object



58
59
60
61
62
# File 'lib/ruco/application.rb', line 58

def bind(key, action=nil, &block)
  raise "Ctrl+m cannot be bound" if key == :"Ctrl+m" # would shadow enter -> bad
  raise if action and block
  @bindings[key] = action || block
end

#configure(&block) ⇒ Object



76
77
78
# File 'lib/ruco/application.rb', line 76

def configure(&block)
  instance_exec(&block)
end

#cursorObject



17
18
19
# File 'lib/ruco/application.rb', line 17

def cursor
  Cursor.new(@focused.cursor.line + @status_lines, @focused.cursor.column)
end

#key(key) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruco/application.rb', line 21

def key(key)
  if bound = @bindings[key]
    result = if bound.is_a?(Symbol)
      @actions[bound].call
    else
      bound.call
    end
    return result
  end

  case key

  # move
  when :up then @focused.move(:relative, -1,0)
  when :down then @focused.move(:relative, 1,0)
  when :right then @focused.move(:relative, 0,1)
  when :left then @focused.move(:relative, 0,-1)
  when :end then @focused.move :to_eol
  when :home then @focused.move :to_bol
  when :page_up then @focused.move :page_up
  when :page_down then @focused.move :page_down

  # modify
  when :tab then @focused.insert("\t")
  when :enter then
    @focused.insert("\n")
  when :backspace then @focused.delete(-1)
  when :delete then @focused.delete(1)

  when :escape then # escape from focused
    @focused.reset
    @focused = editor
  else
    @focused.insert(key) if key.is_a?(String)
  end
end

#resize(lines, columns) ⇒ Object



80
81
82
83
84
85
# File 'lib/ruco/application.rb', line 80

def resize(lines, columns)
  @options[:lines] = lines
  @options[:columns] = columns
  create_components
  @editor.resize(editor_lines, columns)
end

#viewObject



13
14
15
# File 'lib/ruco/application.rb', line 13

def view
  status.view + "\n" + editor.view + command.view
end