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
- #display_info ⇒ Object
-
#initialize(file, options) ⇒ Application
constructor
A new instance of Application.
-
#key(key) ⇒ Object
user typed a key.
- #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 16 |
# File 'lib/ruco/application.rb', line 5 def initialize(file, ) @file, go_to_line = parse_file_and_line(file) @options = OptionAccessor.new() 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
#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
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, ={}, &block) @focused = command command.ask(question, ) 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 |
#cursor ⇒ Object
30 31 32 |
# File 'lib/ruco/application.rb', line 30 def cursor Position.new(@focused.cursor.line + @status_lines, @focused.cursor.column) end |
#display_info ⇒ Object
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, ={}, &block) ask(question, ) do |result| finished = (block.call(result) == :finished) loop_ask(question, , &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_map ⇒ Object
26 27 28 |
# File 'lib/ruco/application.rb', line 26 def style_map status.style_map + editor.style_map + command.style_map end |
#view ⇒ Object
22 23 24 |
# File 'lib/ruco/application.rb', line 22 def view [status.view, editor.view, command.view].join("\n") end |