Class: Ruco::Application

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options) ⇒ Application

Returns a new instance of Application.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/ruco/application.rb', line 5

def initialize(file, options)
  @file, go_to_line = parse_file_and_line(file)
  @options = OptionAccessor.new(options)

  setup_actions
  setup_keys
  load_user_config
  create_components

  @editor.move(:to, go_to_line.to_i-1,0) if go_to_line
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/ruco/application.rb', line 3

def command
  @command
end

#editorObject (readonly)

Returns the value of attribute editor.



3
4
5
# File 'lib/ruco/application.rb', line 3

def editor
  @editor
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/ruco/application.rb', line 3

def options
  @options
end

#statusObject (readonly)

Returns the value of attribute status.



3
4
5
# File 'lib/ruco/application.rb', line 3

def status
  @status
end

Instance Method Details

#action(name, &block) ⇒ Object



92
93
94
# File 'lib/ruco/application.rb', line 92

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

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



96
97
98
99
100
101
102
# File 'lib/ruco/application.rb', line 96

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



86
87
88
89
90
# File 'lib/ruco/application.rb', line 86

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



111
112
113
# File 'lib/ruco/application.rb', line 111

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

#cursorObject



26
27
28
# File 'lib/ruco/application.rb', line 26

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

#key(key) ⇒ Object



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruco/application.rb', line 30

def key(key)
  # deactivate select_mode if its not re-enabled in this action
  @select_mode_was_on = @select_mode
  @select_mode = false

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

  case key

  # move
  when :down then move_with_select_mode :relative, 1,0
  when :right then move_with_select_mode :relative, 0,1
  when :up then move_with_select_mode :relative, -1,0
  when :left then move_with_select_mode :relative, 0,-1
  when :page_up then move_with_select_mode :page_up
  when :page_down then move_with_select_mode :page_down
  when :"Ctrl+right" then move_with_select_mode :jump, :right
  when :"Ctrl+left" then move_with_select_mode :jump, :left

  # select
  when :"Shift+down" then @focused.selecting { move(:relative, 1, 0) }
  when :"Shift+right" then @focused.selecting { move(:relative, 0, 1) }
  when :"Shift+up" then @focused.selecting { move(:relative, -1, 0) }
  when :"Shift+left" then @focused.selecting { move(:relative, 0, -1) }
  when :"Ctrl+Shift+left" then @focused.selecting{ move(:jump, :left) }
  when :"Ctrl+Shift+right" then @focused.selecting{ move(:jump, :right) }


  # modify
  when :tab then
    if @editor.selection
      @editor.indent
    else
      @focused.insert("\t")
    end
  when :"Shift+tab" then @editor.unindent
  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

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



104
105
106
107
108
109
# File 'lib/ruco/application.rb', line 104

def loop_ask(question, options={}, &block)
  ask(question, options) do |result|
    finished = (block.call(result) == :finished)
    loop_ask(question, options, &block) unless finished
  end
end

#resize(lines, columns) ⇒ Object



115
116
117
118
119
120
# File 'lib/ruco/application.rb', line 115

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

#style_mapObject



21
22
23
24
# File 'lib/ruco/application.rb', line 21

def style_map
  reverse = StyleMap.single_line_reversed(@options[:columns])
  reverse + editor.style_map + @command.style_map
end

#viewObject



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

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