Class: RubyRich::Composer
- Inherits:
-
Object
- Object
- RubyRich::Composer
- Defined in:
- lib/ruby_rich/composer.rb
Constant Summary collapse
- DEFAULT_MENU_LIMIT =
8
Instance Attribute Summary collapse
-
#attachments ⇒ Object
readonly
Returns the value of attribute attachments.
-
#editor ⇒ Object
readonly
Returns the value of attribute editor.
-
#focused ⇒ Object
readonly
Returns the value of attribute focused.
-
#height ⇒ Object
Returns the value of attribute height.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#max_height ⇒ Object
Returns the value of attribute max_height.
-
#min_height ⇒ Object
Returns the value of attribute min_height.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #add_attachment(attachment) ⇒ Object
- #attach(layout, priority: 200) ⇒ Object
- #blur ⇒ Object
- #clear_attachments ⇒ Object
- #desired_height ⇒ Object
- #focus ⇒ Object
- #focused? ⇒ Boolean
- #handle_event(event_data, live = nil) ⇒ Object
- #ignore_next_tab ⇒ Object
-
#initialize(placeholder: "Type a message or /", commands: [], menu_limit: DEFAULT_MENU_LIMIT, multiline: true, history_path: nil, max_history: 200, on_submit: nil, on_select: nil, on_interrupt: nil, on_eof: nil, on_paste: nil) ⇒ Composer
constructor
A new instance of Composer.
- #menu_open? ⇒ Boolean
- #refresh_commands_async(&block) ⇒ Object
- #register_command(name:, description: "", aliases: [], handler: nil, hidden: false, group: nil) ⇒ Object
- #remove_attachment(index) ⇒ Object
- #render ⇒ Object
- #value ⇒ Object
- #wants_tab? ⇒ Boolean
Constructor Details
#initialize(placeholder: "Type a message or /", commands: [], menu_limit: DEFAULT_MENU_LIMIT, multiline: true, history_path: nil, max_history: 200, on_submit: nil, on_select: nil, on_interrupt: nil, on_eof: nil, on_paste: nil) ⇒ Composer
Returns a new instance of Composer.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby_rich/composer.rb', line 10 def initialize(placeholder: "Type a message or /", commands: [], menu_limit: DEFAULT_MENU_LIMIT, multiline: true, history_path: nil, max_history: 200, on_submit: nil, on_select: nil, on_interrupt: nil, on_eof: nil, on_paste: nil) @placeholder = placeholder @commands = normalize_commands(commands) @menu_limit = @on_submit = on_submit @on_select = on_select @on_interrupt = on_interrupt @on_eof = on_eof @on_paste = on_paste @width = 0 @height = 0 @min_height = 3 @max_height = 10 @editor = LineEditor.new(multiline: multiline, history_path: history_path, max_history: max_history) @attachments = [] @focused = false @menu_open = false @selected_index = 0 @ignore_next_tab = false end |
Instance Attribute Details
#attachments ⇒ Object (readonly)
Returns the value of attribute attachments.
6 7 8 |
# File 'lib/ruby_rich/composer.rb', line 6 def @attachments end |
#editor ⇒ Object (readonly)
Returns the value of attribute editor.
6 7 8 |
# File 'lib/ruby_rich/composer.rb', line 6 def editor @editor end |
#focused ⇒ Object (readonly)
Returns the value of attribute focused.
6 7 8 |
# File 'lib/ruby_rich/composer.rb', line 6 def focused @focused end |
#height ⇒ Object
Returns the value of attribute height.
5 6 7 |
# File 'lib/ruby_rich/composer.rb', line 5 def height @height end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
6 7 8 |
# File 'lib/ruby_rich/composer.rb', line 6 def history @history end |
#max_height ⇒ Object
Returns the value of attribute max_height.
5 6 7 |
# File 'lib/ruby_rich/composer.rb', line 5 def max_height @max_height end |
#min_height ⇒ Object
Returns the value of attribute min_height.
5 6 7 |
# File 'lib/ruby_rich/composer.rb', line 5 def min_height @min_height end |
#width ⇒ Object
Returns the value of attribute width.
5 6 7 |
# File 'lib/ruby_rich/composer.rb', line 5 def width @width end |
Instance Method Details
#add_attachment(attachment) ⇒ Object
85 86 87 88 |
# File 'lib/ruby_rich/composer.rb', line 85 def () @attachments << () self end |
#attach(layout, priority: 200) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ruby_rich/composer.rb', line 41 def attach(layout, priority: 200) handled_events.each do |event_name| layout.key(event_name, priority) do |event_data, live| handle_event(event_data, live) false end end layout.key(:mouse_down, priority) do |_event_data, _live| focus false end self end |
#blur ⇒ Object
62 63 64 65 66 |
# File 'lib/ruby_rich/composer.rb', line 62 def blur @focused = false self end |
#clear_attachments ⇒ Object
118 119 120 121 |
# File 'lib/ruby_rich/composer.rb', line 118 def @attachments.clear self end |
#desired_height ⇒ Object
123 124 125 126 127 128 |
# File 'lib/ruby_rich/composer.rb', line 123 def desired_height input_rows = @editor.render_lines(width: inner_width, placeholder: nil, focused: false).length = @attachments.empty? ? 0 : [@attachments.length, 3].min = ? [[filtered_commands.length, @menu_limit].min, 1].max : 0 [[1 + input_rows + + , @min_height].max, @max_height].min end |
#focus ⇒ Object
57 58 59 60 |
# File 'lib/ruby_rich/composer.rb', line 57 def focus @focused = true self end |
#focused? ⇒ Boolean
68 69 70 |
# File 'lib/ruby_rich/composer.rb', line 68 def focused? @focused end |
#handle_event(event_data, live = nil) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ruby_rich/composer.rb', line 130 def handle_event(event_data, live = nil) return false unless focused? case event_data[:name] when :string insert_text(event_data[:value].to_s) when :paste handle_paste(event_data[:value].to_s) when :left @editor.move_left when :right @editor.move_right when :home, :ctrl_a @editor.home when :end, :ctrl_e @editor.end when :up ? move_selection(-1) : @editor.move_up when :down ? move_selection(1) : @editor.move_down when :backspace @editor.backspace when :delete @editor.delete when :ctrl_k @editor.kill_to_end when :ctrl_u @editor.kill_to_start when :ctrl_w @editor.kill_word_back when :enter enter(live) when :shift_enter, :alt_enter, :ctrl_enter @editor.newline when :escape escape when :ctrl_c invoke_callback(@on_interrupt, live, self) when :ctrl_d ctrl_d(live) when :ctrl_v invoke_callback(@on_paste, live, self) when :tab if @ignore_next_tab @ignore_next_tab = false else ? move_selection(1) : end when :shift_tab ? move_selection(-1) : end end |
#ignore_next_tab ⇒ Object
80 81 82 83 |
# File 'lib/ruby_rich/composer.rb', line 80 def ignore_next_tab @ignore_next_tab = true self end |
#menu_open? ⇒ Boolean
72 73 74 |
# File 'lib/ruby_rich/composer.rb', line 72 def @menu_open end |
#refresh_commands_async(&block) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/ruby_rich/composer.rb', line 105 def refresh_commands_async(&block) Thread.new do next_commands = block.call @commands = normalize_commands(next_commands) if next_commands rescue => e RubyRich.logger.error("Command refresh failed: #{e.class}: #{e.}") if RubyRich.respond_to?(:logger) end end |
#register_command(name:, description: "", aliases: [], handler: nil, hidden: false, group: nil) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ruby_rich/composer.rb', line 90 def register_command(name:, description: "", aliases: [], handler: nil, hidden: false, group: nil) command = { label: name.to_s, value: name.to_s, description: description.to_s, aliases: aliases.map(&:to_s), handler: handler, hidden: hidden, group: group } @commands.reject! { |item| item[:value] == command[:value] } @commands << command self end |
#remove_attachment(index) ⇒ Object
114 115 116 |
# File 'lib/ruby_rich/composer.rb', line 114 def (index) @attachments.delete_at(index.to_i) end |
#render ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/ruby_rich/composer.rb', line 184 def render lines = [] lines.concat() lines.concat(render_input_lines) lines.concat() if fit_height(lines) end |
#value ⇒ Object
33 34 35 |
# File 'lib/ruby_rich/composer.rb', line 33 def value @editor.value end |
#wants_tab? ⇒ Boolean
76 77 78 |
# File 'lib/ruby_rich/composer.rb', line 76 def wants_tab? focused? && ( || @editor.value.include?("/")) end |