Class: Ruco::Application
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#editor ⇒ Object
readonly
Returns the value of attribute editor.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #action(name, &block) ⇒ Object
- #ask(question, options = {}, &block) ⇒ Object
- #bind(key, action = nil, &block) ⇒ Object
- #configure(&block) ⇒ Object
- #cursor ⇒ Object
-
#initialize(file, options) ⇒ Application
constructor
A new instance of Application.
- #key(key) ⇒ Object
- #loop_ask(question, options = {}, &block) ⇒ Object
- #resize(lines, columns) ⇒ Object
- #style_map ⇒ Object
- #view ⇒ Object
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, ) @file, go_to_line = parse_file_and_line(file) @options = OptionAccessor.new() 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
#command ⇒ Object (readonly)
Returns the value of attribute command.
3 4 5 |
# File 'lib/ruco/application.rb', line 3 def command @command end |
#editor ⇒ Object (readonly)
Returns the value of attribute editor.
3 4 5 |
# File 'lib/ruco/application.rb', line 3 def editor @editor end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/ruco/application.rb', line 3 def @options end |
#status ⇒ Object (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
94 95 96 |
# File 'lib/ruco/application.rb', line 94 def action(name, &block) @actions[name] = block end |
#ask(question, options = {}, &block) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/ruco/application.rb', line 98 def ask(question, ={}, &block) @focused = command command.ask(question, ) do |response| @focused = editor block.call(response) end end |
#bind(key, action = nil, &block) ⇒ Object
88 89 90 91 92 |
# File 'lib/ruco/application.rb', line 88 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
113 114 115 |
# File 'lib/ruco/application.rb', line 113 def configure(&block) instance_exec(&block) end |
#cursor ⇒ Object
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 85 86 |
# 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 :end then move_with_select_mode :to_eol when :home then move_with_select_mode :to_bol 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
106 107 108 109 110 111 |
# File 'lib/ruco/application.rb', line 106 def loop_ask(question, ={}, &block) ask(question, ) do |result| finished = (block.call(result) == :finished) loop_ask(question, , &block) unless finished end end |
#resize(lines, columns) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/ruco/application.rb', line 117 def resize(lines, columns) @options[:lines] = lines @options[:columns] = columns create_components @editor.resize(editor_lines, columns) end |
#style_map ⇒ Object
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 |
#view ⇒ Object
17 18 19 |
# File 'lib/ruco/application.rb', line 17 def view status.view + "\n" + editor.view + "\n" + command.view end |