Class: PWKeep::EditorApplication

Inherits:
Object
  • Object
show all
Defined in:
lib/pwkeep/editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options) ⇒ EditorApplication

Returns a new instance of EditorApplication.



36
37
38
39
40
41
42
43
# File 'lib/pwkeep/editor.rb', line 36

def initialize(data, options)
  @options = Ruco::OptionAccessor.new(options)
  @data = data

  setup_actions
  setup_keys
  create_components
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



34
35
36
# File 'lib/pwkeep/editor.rb', line 34

def command
  @command
end

#editorObject (readonly)

Returns the value of attribute editor.



34
35
36
# File 'lib/pwkeep/editor.rb', line 34

def editor
  @editor
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/pwkeep/editor.rb', line 34

def options
  @options
end

#statusObject (readonly)

Returns the value of attribute status.



34
35
36
# File 'lib/pwkeep/editor.rb', line 34

def status
  @status
end

Instance Method Details

#action(name, &block) ⇒ Object



120
121
122
# File 'lib/pwkeep/editor.rb', line 120

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

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



124
125
126
127
128
129
130
# File 'lib/pwkeep/editor.rb', line 124

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



114
115
116
117
118
# File 'lib/pwkeep/editor.rb', line 114

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



139
140
141
# File 'lib/pwkeep/editor.rb', line 139

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

#cursorObject



57
58
59
# File 'lib/pwkeep/editor.rb', line 57

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

#display_infoObject



45
46
47
# File 'lib/pwkeep/editor.rb', line 45

def display_info
  [view, style_map, cursor]
end

#key(key) ⇒ Object

user typed a key



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pwkeep/editor.rb', line 62

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



132
133
134
135
136
137
# File 'lib/pwkeep/editor.rb', line 132

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



143
144
145
146
147
148
# File 'lib/pwkeep/editor.rb', line 143

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

#style_mapObject



53
54
55
# File 'lib/pwkeep/editor.rb', line 53

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

#viewObject



49
50
51
# File 'lib/pwkeep/editor.rb', line 49

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