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
16
# File 'lib/ruco/application.rb', line 5

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

  change_terminal_title
  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



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

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

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



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

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



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

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

#configure(&block) ⇒ Object



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

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

#cursorObject



30
31
32
# File 'lib/ruco/application.rb', line 30

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

#display_infoObject



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

def display_info
  [view, style_map, cursor]
end

#key(key) ⇒ Object

user typed a key



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
85
# File 'lib/ruco/application.rb', line 35

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]
    return execute_action(bound)
  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", :"Alt+f" then move_with_select_mode :jump, :right
  when :"Ctrl+left", :"Alt+b" 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", :"Alt+Shift+left" then @focused.selecting{ move(:jump, :left) }
  when :"Ctrl+Shift+right", :"Alt+Shift+right" then @focused.selecting{ move(:jump, :right) }
  when :"Shift+end" then @focused.selecting{ move(:to_eol) }
  when :"Shift+home" then @focused.selecting{ move(:to_bol) }


  # 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



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

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



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

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

#style_mapObject



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

def style_map
  status.style_map + editor.style_map + command.style_map
end

#viewObject



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

def view
  [status.view, editor.view, command.view].join("\n")
end