Class: RubyRich::Composer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/composer.rb

Constant Summary collapse

DEFAULT_MENU_LIMIT =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = 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

#attachmentsObject (readonly)

Returns the value of attribute attachments.



6
7
8
# File 'lib/ruby_rich/composer.rb', line 6

def attachments
  @attachments
end

#editorObject (readonly)

Returns the value of attribute editor.



6
7
8
# File 'lib/ruby_rich/composer.rb', line 6

def editor
  @editor
end

#focusedObject (readonly)

Returns the value of attribute focused.



6
7
8
# File 'lib/ruby_rich/composer.rb', line 6

def focused
  @focused
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/ruby_rich/composer.rb', line 5

def height
  @height
end

#historyObject (readonly)

Returns the value of attribute history.



6
7
8
# File 'lib/ruby_rich/composer.rb', line 6

def history
  @history
end

#max_heightObject

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_heightObject

Returns the value of attribute min_height.



5
6
7
# File 'lib/ruby_rich/composer.rb', line 5

def min_height
  @min_height
end

#widthObject

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



106
107
108
109
# File 'lib/ruby_rich/composer.rb', line 106

def add_attachment(attachment)
  @attachments << normalize_attachment(attachment)
  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

#blurObject



62
63
64
65
66
# File 'lib/ruby_rich/composer.rb', line 62

def blur
  @focused = false
  close_menu
  self
end

#clear_attachmentsObject



139
140
141
142
# File 'lib/ruby_rich/composer.rb', line 139

def clear_attachments
  @attachments.clear
  self
end

#desired_heightObject



144
145
146
147
148
149
# File 'lib/ruby_rich/composer.rb', line 144

def desired_height
  input_rows = @editor.render_lines(width: inner_width, placeholder: nil, focused: false).length
  attachment_rows = @attachments.empty? ? 0 : [@attachments.length, 3].min
  menu_rows = menu_open? ? [[filtered_commands.length, @menu_limit].min, 1].max : 0
  [[1 + input_rows + attachment_rows + menu_rows, @min_height].max, @max_height].min
end

#focusObject



57
58
59
60
# File 'lib/ruby_rich/composer.rb', line 57

def focus
  @focused = true
  self
end

#focused?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/ruby_rich/composer.rb', line 68

def focused?
  @focused
end

#handle_event(event_data, live = nil) ⇒ Object



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ruby_rich/composer.rb', line 151

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
    menu_open? ? move_selection(-1) : @editor.move_up
  when :down
    menu_open? ? move_selection(1) : @editor.move_down
  when :backspace
    @editor.backspace
    sync_menu
  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
      menu_open? ? move_selection(1) : open_menu_if_available
    end
  when :shift_tab
    menu_open? ? move_selection(-1) : open_menu_if_available
  end
end

#ignore_next_tabObject



101
102
103
104
# File 'lib/ruby_rich/composer.rb', line 101

def ignore_next_tab
  @ignore_next_tab = true
  self
end

Returns:

  • (Boolean)


72
73
74
# File 'lib/ruby_rich/composer.rb', line 72

def menu_open?
  @menu_open
end

#native_cursor_positionObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby_rich/composer.rb', line 76

def native_cursor_position
  return nil unless focused?

  input_width = [inner_width - 2, 1].max
  editor_row, editor_col = @editor.cursor_visual_position(width: input_width)
  raw_row = [@attachments.length, 3].min + editor_row
  raw_col = 2 + editor_col

  raw_height = [@height.to_i, 1].max
  raw_lines = []
  raw_lines.concat(render_attachments)
  raw_lines.concat(render_input_lines)
  raw_lines.concat(render_menu_lines) if menu_open?

  clipped_rows = [raw_lines.length - raw_height, 0].max
  visible_row = raw_row - clipped_rows
  return nil if visible_row.negative? || visible_row >= raw_height

  [visible_row, raw_col]
end

#refresh_commands_async(&block) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/ruby_rich/composer.rb', line 126

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.message}") if RubyRich.respond_to?(:logger)
  end
end

#register_command(name:, description: "", aliases: [], handler: nil, hidden: false, group: nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ruby_rich/composer.rb', line 111

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



135
136
137
# File 'lib/ruby_rich/composer.rb', line 135

def remove_attachment(index)
  @attachments.delete_at(index.to_i)
end

#renderObject



205
206
207
208
209
210
211
# File 'lib/ruby_rich/composer.rb', line 205

def render
  lines = []
  lines.concat(render_attachments)
  lines.concat(render_input_lines)
  lines.concat(render_menu_lines) if menu_open?
  fit_height(lines)
end

#valueObject



33
34
35
# File 'lib/ruby_rich/composer.rb', line 33

def value
  @editor.value
end

#wants_tab?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/ruby_rich/composer.rb', line 97

def wants_tab?
  focused? && (menu_open? || @editor.value.include?("/"))
end