Class: Clacky::UI2::Components::InputArea
- Inherits:
-
Object
- Object
- Clacky::UI2::Components::InputArea
show all
- Includes:
- LineEditor
- Defined in:
- lib/clacky/ui2/components/input_area.rb
Overview
InputArea manages the fixed input area at the bottom of the screen Enhanced with multi-line support, image paste, and more
Constant Summary
collapse
- USER_TIPS =
User tips pool - can be extended with more tips over time
[
"Shift+Tab to toggle permission mode (confirm_safes ⇄ auto_approve)",
"Ctrl+C to interrupt AI execution or clear input",
"Shift+Enter to create multi-line input",
"Ctrl+V to paste images (supports up to 3 images)",
"Ctrl+D to delete pasted images",
"Use /clear to restart session, /help for commands"
].freeze
Constants included
from LineEditor
LineEditor::MAX_CONTENT_WIDTH_RATIO
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_to_history(entry) ⇒ Object
-
#backspace ⇒ Object
-
#char_display_width(char) ⇒ Integer
Calculate display width of a single character.
-
#clear ⇒ Object
-
#clear_line ⇒ Object
-
#clear_tips ⇒ Object
-
#clear_user_tip ⇒ Object
Clear user tip and stop rotation.
-
#current_content ⇒ Object
-
#current_line ⇒ Object
-
#current_value ⇒ Object
-
#cursor_down ⇒ Object
-
#cursor_end ⇒ Object
-
#cursor_home ⇒ Object
-
#cursor_left ⇒ Object
-
#cursor_right ⇒ Object
-
#cursor_up ⇒ Object
-
#delete_char ⇒ Object
-
#empty? ⇒ Boolean
-
#expand_placeholders(text) ⇒ Object
-
#flush ⇒ Object
-
#format_filesize(size) ⇒ Object
-
#format_tips(message, type) ⇒ Object
-
#format_user_tip(tip) ⇒ String
Format user tip (usage suggestion) with lightbulb icon.
-
#get_status_indicator(status, color) ⇒ Object
-
#handle_ctrl_c ⇒ Object
-
#handle_ctrl_d ⇒ Object
-
#handle_down_arrow ⇒ Object
-
#handle_enter ⇒ Object
-
#handle_key(key) ⇒ Object
-
#handle_paste ⇒ Object
-
#handle_up_arrow ⇒ Object
-
#has_images? ⇒ Boolean
-
#history_next ⇒ Object
-
#history_prev ⇒ Object
-
#initialize(row: 0) ⇒ InputArea
constructor
A new instance of InputArea.
-
#input_buffer ⇒ Object
-
#insert_char(char) ⇒ Object
— Public editing methods —.
-
#insert_text(text) ⇒ Object
-
#kill_to_end ⇒ Object
-
#kill_to_start ⇒ Object
-
#kill_word ⇒ Object
-
#load_history_entry ⇒ Object
-
#mode_color_for(mode) ⇒ Object
-
#move_cursor(row, col) ⇒ Object
-
#multiline? ⇒ Boolean
-
#newline ⇒ Object
-
#paste_from_clipboard ⇒ Object
-
#paste_from_clipboard_linux ⇒ Object
-
#paste_from_clipboard_macos ⇒ Object
-
#pause ⇒ Object
Pause input area (when InlineInput is active).
-
#paused? ⇒ Boolean
-
#position_cursor(start_row) ⇒ Object
-
#print_with_padding(content) ⇒ Object
Print content and pad with spaces to clear any remaining characters from previous render This avoids flickering from clear_line while ensuring old content is erased.
-
#prompt ⇒ Object
Get prompt symbol from theme.
-
#render(start_row:, width: nil) ⇒ Object
-
#render_input_lines(start_row) ⇒ Integer
Render all input lines with auto-wrap support.
-
#render_line_segment_with_cursor(line, segment_start, segment_end) ⇒ String
Render a segment of a line with cursor if cursor is in this segment Applies theme colors to the text.
-
#render_line_with_cursor(line) ⇒ Object
-
#render_separator(row) ⇒ Object
Render a separator line (ensures it doesn’t exceed screen width).
-
#render_sessionbar(row) ⇒ Integer
Render session bar with wrapping support.
-
#required_height ⇒ Object
-
#resume ⇒ Object
Resume input area (when InlineInput is done).
-
#set_prompt(prompt) ⇒ Object
-
#set_skill_loader(skill_loader, agent_profile = nil) ⇒ Object
Set skill loader for command suggestions.
-
#set_tips(message, type: :info) ⇒ Object
-
#shorten_path(path) ⇒ Object
-
#show_user_tip(probability: 0.4, rotation_interval: 12, max_tips: 3) ⇒ Object
Show a random user tip with probability and auto-rotation (max 3 tips).
-
#status_color_for(status) ⇒ Object
-
#strip_ansi_codes(text) ⇒ String
Strip ANSI escape codes from a string.
-
#submit ⇒ Object
-
#theme ⇒ Object
Get current theme from ThemeManager.
-
#update_sessionbar(session_id: nil, working_dir: nil, mode: nil, model: nil, tasks: nil, cost: nil, cost_source: nil, status: nil) ⇒ Object
-
#wrap_line(line, max_width) ⇒ Array<Hash>
Wrap a line into multiple segments based on available width Considers display width of characters (multi-byte characters like Chinese).
Methods included from LineEditor
#calculate_display_width, #clear_line_content, #cursor_column, #cursor_position_with_wrap, #initialize_line_editor, #set_line
Constructor Details
#initialize(row: 0) ⇒ InputArea
Returns a new instance of InputArea.
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
|
# File 'lib/clacky/ui2/components/input_area.rb', line 31
def initialize(row: 0)
@row = row
@lines = [""]
@line_index = 0
@cursor_position = 0
@history = []
@history_index = -1
@pastel = Pastel.new
@width = TTY::Screen.width
@files = []
@paste_counter = 0
@paste_placeholders = {}
@last_ctrl_c_time = nil
@tips_message = nil
@tips_type = :info
@tips_timer = nil
@last_render_row = nil
@user_tip = nil
@user_tip_timer = nil
@user_tip_count = 0
@paused = false
@sessionbar_info = {
session_id: nil, working_dir: nil,
mode: nil,
model: nil,
tasks: 0,
cost: 0.0,
cost_source: nil, status: 'idle' }
@animation_frame = 0
@last_animation_update = Time.now
@working_frames = ["❄", "❅", "❆"]
@command_suggestions = CommandSuggestions.new
@skill_loader = nil end
|
Instance Attribute Details
#cursor_position ⇒ Object
Returns the value of attribute cursor_position.
29
30
31
|
# File 'lib/clacky/ui2/components/input_area.rb', line 29
def cursor_position
@cursor_position
end
|
#files ⇒ Object
Returns the value of attribute files.
29
30
31
|
# File 'lib/clacky/ui2/components/input_area.rb', line 29
def files
@files
end
|
#line_index ⇒ Object
Returns the value of attribute line_index.
29
30
31
|
# File 'lib/clacky/ui2/components/input_area.rb', line 29
def line_index
@line_index
end
|
#row ⇒ Object
Returns the value of attribute row.
28
29
30
|
# File 'lib/clacky/ui2/components/input_area.rb', line 28
def row
@row
end
|
#tips_message ⇒ Object
Returns the value of attribute tips_message.
29
30
31
|
# File 'lib/clacky/ui2/components/input_area.rb', line 29
def tips_message
@tips_message
end
|
#tips_type ⇒ Object
Returns the value of attribute tips_type.
29
30
31
|
# File 'lib/clacky/ui2/components/input_area.rb', line 29
def tips_type
@tips_type
end
|
Instance Method Details
#add_to_history(entry) ⇒ Object
984
985
986
987
|
# File 'lib/clacky/ui2/components/input_area.rb', line 984
def add_to_history(entry)
@history << entry
@history = @history.last(100) if @history.size > 100
end
|
#backspace ⇒ Object
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
# File 'lib/clacky/ui2/components/input_area.rb', line 551
def backspace
if @cursor_position > 0
chars = current_line.chars
chars.delete_at(@cursor_position - 1)
@lines[@line_index] = chars.join
@cursor_position -= 1
elsif @line_index > 0
prev_line = @lines[@line_index - 1]
current = @lines[@line_index]
@lines.delete_at(@line_index)
@line_index -= 1
@cursor_position = prev_line.chars.length
@lines[@line_index] = prev_line + current
end
end
|
#char_display_width(char) ⇒ Integer
Calculate display width of a single character
751
752
753
|
# File 'lib/clacky/ui2/components/input_area.rb', line 751
def char_display_width(char)
super(char)
end
|
#clear ⇒ Object
590
591
592
593
594
595
596
597
598
599
600
|
# File 'lib/clacky/ui2/components/input_area.rb', line 590
def clear
@lines = [""]
@line_index = 0
@cursor_position = 0
@history_index = -1
@files = []
@paste_counter = 0
@paste_placeholders = {}
clear_tips
@command_suggestions.hide if @command_suggestions
end
|
#clear_line ⇒ Object
1326
1327
1328
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1326
def clear_line
print "\e[2K"
end
|
#clear_tips ⇒ Object
424
425
426
427
428
429
430
|
# File 'lib/clacky/ui2/components/input_area.rb', line 424
def clear_tips
if @tips_timer&.alive?
@tips_timer.kill
end
@tips_message = nil
end
|
#clear_user_tip ⇒ Object
Clear user tip and stop rotation
471
472
473
474
475
|
# File 'lib/clacky/ui2/components/input_area.rb', line 471
def clear_user_tip
stop_user_tip_timer
@user_tip = nil
@user_tip_count = 0
end
|
#current_content ⇒ Object
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
# File 'lib/clacky/ui2/components/input_area.rb', line 499
def current_content
text = expand_placeholders(@lines.join("\n"))
return "" if text.empty? && @files.empty?
symbol = theme.format_symbol(:user)
content = theme.format_text(text, :user)
result = "\n#{symbol} #{content}\n"
if @files.any?
@files.each_with_index do |f, idx|
filename = f[:name] || f["name"] || "file"
result += @pastel.dim(" [File #{idx + 1}] #{filename}") + "\n"
end
end
result
end
|
#current_line ⇒ Object
1045
1046
1047
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1045
def current_line
@lines[@line_index] || ""
end
|
#current_value ⇒ Object
522
523
524
|
# File 'lib/clacky/ui2/components/input_area.rb', line 522
def current_value
expand_placeholders(@lines.join("\n"))
end
|
#cursor_down ⇒ Object
940
941
942
943
944
945
|
# File 'lib/clacky/ui2/components/input_area.rb', line 940
def cursor_down
return false if @line_index >= @lines.size - 1
@line_index += 1
@cursor_position = [@cursor_position, current_line.chars.length].min
true
end
|
#cursor_end ⇒ Object
586
587
588
|
# File 'lib/clacky/ui2/components/input_area.rb', line 586
def cursor_end
@cursor_position = current_line.chars.length
end
|
#cursor_home ⇒ Object
582
583
584
|
# File 'lib/clacky/ui2/components/input_area.rb', line 582
def cursor_home
@cursor_position = 0
end
|
#cursor_left ⇒ Object
574
575
576
|
# File 'lib/clacky/ui2/components/input_area.rb', line 574
def cursor_left
@cursor_position = [@cursor_position - 1, 0].max
end
|
#cursor_right ⇒ Object
578
579
580
|
# File 'lib/clacky/ui2/components/input_area.rb', line 578
def cursor_right
@cursor_position = [@cursor_position + 1, current_line.chars.length].min
end
|
#cursor_up ⇒ Object
933
934
935
936
937
938
|
# File 'lib/clacky/ui2/components/input_area.rb', line 933
def cursor_up
return false if @line_index == 0
@line_index -= 1
@cursor_position = [@cursor_position, current_line.chars.length].min
true
end
|
#delete_char ⇒ Object
567
568
569
570
571
572
|
# File 'lib/clacky/ui2/components/input_area.rb', line 567
def delete_char
chars = current_line.chars
return if @cursor_position >= chars.length
chars.delete_at(@cursor_position)
@lines[@line_index] = chars.join
end
|
#empty? ⇒ Boolean
526
527
528
|
# File 'lib/clacky/ui2/components/input_area.rb', line 526
def empty?
@lines.all?(&:empty?) && @files.empty?
end
|
#expand_placeholders(text) ⇒ Object
1049
1050
1051
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1049
def expand_placeholders(text)
super(text, @paste_placeholders)
end
|
#flush ⇒ Object
1330
1331
1332
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1330
def flush
$stdout.flush
end
|
1298
1299
1300
1301
1302
1303
1304
1305
1306
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1298
def format_filesize(size)
if size < 1024
"#{size}B"
elsif size < 1024 * 1024
"#{(size / 1024.0).round(1)}KB"
else
"#{(size / 1024.0 / 1024.0).round(1)}MB"
end
end
|
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1280
def format_tips(message, type)
max_length = @width - 10
if message.length > max_length
message = message[0...(max_length - 3)] + "..."
end
case type
when :warning
@pastel.dim("[") + @pastel.yellow("Warn") + @pastel.dim("] ") + @pastel.yellow(message)
when :error
@pastel.dim("[") + @pastel.red("Error") + @pastel.dim("] ") + @pastel.red(message)
else
@pastel.dim("[") + @pastel.cyan("Info") + @pastel.dim("] ") + @pastel.white(message)
end
end
|
Format user tip (usage suggestion) with lightbulb icon
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1311
def format_user_tip(tip)
max_length = @width - 5 if tip.length > max_length
tip = tip[0...(max_length - 3)] + "..."
end
@pastel.dim(@pastel.cyan("💡 #{tip}"))
end
|
#get_status_indicator(status, color) ⇒ Object
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1265
def get_status_indicator(status, color)
case status.to_s.downcase
when 'working'
now = Time.now
if now - @last_animation_update >= 0.3
@animation_frame = (@animation_frame + 1) % @working_frames.length
@last_animation_update = now
end
@pastel.public_send(color, @working_frames[@animation_frame])
else
@pastel.public_send(color, "●") end
end
|
#handle_ctrl_c ⇒ Object
866
867
868
|
# File 'lib/clacky/ui2/components/input_area.rb', line 866
def handle_ctrl_c
{ action: :interrupt }
end
|
#handle_ctrl_d ⇒ Object
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
|
# File 'lib/clacky/ui2/components/input_area.rb', line 870
def handle_ctrl_d
if has_images?
if @files.size == 1
@files.clear
else
@files.shift
end
clear_tips
{ action: nil }
elsif empty?
{ action: :exit }
else
{ action: nil }
end
end
|
#handle_down_arrow ⇒ Object
854
855
856
857
858
859
860
861
862
863
864
|
# File 'lib/clacky/ui2/components/input_area.rb', line 854
def handle_down_arrow
if multiline?
unless cursor_down
history_next
end
else
history_next
end
{ action: nil }
end
|
#handle_enter ⇒ Object
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
|
# File 'lib/clacky/ui2/components/input_area.rb', line 794
def handle_enter
text = current_value.strip
content_to_display = current_content
result_text = current_value
result_files = @files.dup
if text.start_with?('/')
if text =~ /^\/([a-zA-Z-]+)$/
case text
when '/clear'
add_to_history(result_text) unless result_text.empty?
clear
return { action: :clear_output, data: { text: result_text, files: result_files, display: content_to_display } }
when '/help'
add_to_history(result_text) unless result_text.empty?
clear
return { action: :help, data: { text: result_text, files: result_files, display: content_to_display } }
when '/exit', '/quit'
return { action: :exit }
else
end
end
elsif text == '?'
add_to_history(result_text) unless result_text.empty?
clear
return { action: :help, data: { text: result_text, files: result_files, display: content_to_display } }
elsif text == 'exit' || text == 'quit'
return { action: :exit }
end
if text.empty? && @files.empty?
return { action: nil }
end
add_to_history(result_text) unless result_text.empty?
clear
{ action: :submit, data: { text: result_text, files: result_files, display: content_to_display } }
end
|
#handle_key(key) ⇒ Object
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/clacky/ui2/components/input_area.rb', line 165
def handle_key(key)
return { action: nil } if @paused
old_height = required_height
if @command_suggestions.visible
case key
when :up_arrow
@command_suggestions.select_previous
return { action: nil }
when :down_arrow
@command_suggestions.select_next
return { action: nil }
when :enter
if @command_suggestions.has_suggestions?
selected = @command_suggestions.selected_command_text
if selected
@lines = [selected]
@line_index = 0
@cursor_position = selected.length
@command_suggestions.hide
return handle_enter
end
end
when :escape
@command_suggestions.hide
return { action: nil }
when :tab
if @command_suggestions.has_suggestions?
selected = @command_suggestions.selected_command_text
if selected
hint = @command_suggestions.selected_argument_hint
completed = "#{selected} "
@lines = [completed]
@line_index = 0
@cursor_position = completed.length
@command_suggestions.hide
set_tips("Usage: #{selected} #{hint}", type: :info) if hint && !hint.empty?
return { action: nil }
end
end
end
end
if key == :tab
trigger_tab_completion
return { action: nil }
end
result = case key
when Hash
if key[:type] == :rapid_input
insert_text(key[:text])
clear_tips
update_command_suggestions
end
{ action: nil }
when :enter then handle_enter
when :newline then newline; { action: nil }
when :backspace
backspace
update_command_suggestions
{ action: nil }
when :delete
delete_char
update_command_suggestions
{ action: nil }
when :left_arrow, :ctrl_b then cursor_left; { action: nil }
when :right_arrow, :ctrl_f then cursor_right; { action: nil }
when :up_arrow then handle_up_arrow
when :down_arrow then handle_down_arrow
when :home, :ctrl_a then cursor_home; { action: nil }
when :end, :ctrl_e then cursor_end; { action: nil }
when :ctrl_k then kill_to_end; { action: nil }
when :ctrl_u then kill_to_start; { action: nil }
when :ctrl_w then kill_word; { action: nil }
when :ctrl_c then handle_ctrl_c
when :ctrl_d then handle_ctrl_d
when :ctrl_v then handle_paste
when :ctrl_o then { action: :toggle_expand }
when :shift_tab then { action: :toggle_mode }
when :escape
if @command_suggestions.visible
@command_suggestions.hide
{ action: nil }
else
{ action: :time_machine }
end
else
if key.is_a?(String) && key.length >= 1 && key.ord >= 32
insert_char(key)
update_command_suggestions
end
{ action: nil }
end
new_height = required_height
if new_height != old_height
result[:height_changed] = true
result[:new_height] = new_height
end
result
end
|
#handle_paste ⇒ Object
886
887
888
889
890
891
892
893
894
895
896
897
898
899
|
# File 'lib/clacky/ui2/components/input_area.rb', line 886
def handle_paste
pasted = paste_from_clipboard
if pasted[:type] == :image
path = pasted[:path]
mime_type = pasted[:mime_type] || "image/png"
size = File.exist?(path) ? File.size(path) : 0
@files << { name: File.basename(path), mime_type: mime_type, path: path, size: size }
clear_tips
else
insert_text(pasted[:text])
clear_tips
end
{ action: nil }
end
|
#handle_up_arrow ⇒ Object
842
843
844
845
846
847
848
849
850
851
852
|
# File 'lib/clacky/ui2/components/input_area.rb', line 842
def handle_up_arrow
if multiline?
unless cursor_up
history_prev
end
else
history_prev
end
{ action: nil }
end
|
#has_images? ⇒ Boolean
534
535
536
|
# File 'lib/clacky/ui2/components/input_area.rb', line 534
def has_images?
@files.any?
end
|
#history_next ⇒ Object
620
621
622
623
624
625
626
627
628
629
630
631
|
# File 'lib/clacky/ui2/components/input_area.rb', line 620
def history_next
return if @history_index == -1
@history_index += 1
if @history_index >= @history.size
@history_index = -1
@lines = [""]
@line_index = 0
@cursor_position = 0
else
load_history_entry
end
end
|
#history_prev ⇒ Object
610
611
612
613
614
615
616
617
618
|
# File 'lib/clacky/ui2/components/input_area.rb', line 610
def history_prev
return if @history.empty?
if @history_index == -1
@history_index = @history.size - 1
else
@history_index = [@history_index - 1, 0].max
end
load_history_entry
end
|
161
162
163
|
# File 'lib/clacky/ui2/components/input_area.rb', line 161
def input_buffer
@lines.join("\n")
end
|
#insert_char(char) ⇒ Object
— Public editing methods —
544
545
546
547
548
549
|
# File 'lib/clacky/ui2/components/input_area.rb', line 544
def insert_char(char)
chars = current_line.chars
chars.insert(@cursor_position, char)
@lines[@line_index] = chars.join
@cursor_position += 1
end
|
#insert_text(text) ⇒ Object
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
|
# File 'lib/clacky/ui2/components/input_area.rb', line 901
def insert_text(text)
return if text.nil? || text.empty?
text_lines = text.split(/\r\n|\r|\n/)
if text_lines.size > 1
@paste_counter += 1
placeholder = "[##{@paste_counter} Paste Text]"
@paste_placeholders[placeholder] = text
chars = current_line.chars
chars.insert(@cursor_position, *placeholder.chars)
@lines[@line_index] = chars.join
@cursor_position += placeholder.length
else
chars = current_line.chars
text.chars.each_with_index do |c, i|
chars.insert(@cursor_position + i, c)
end
@lines[@line_index] = chars.join
@cursor_position += text.length
end
end
|
#kill_to_end ⇒ Object
947
948
949
950
|
# File 'lib/clacky/ui2/components/input_area.rb', line 947
def kill_to_end
chars = current_line.chars
@lines[@line_index] = chars[0...@cursor_position].join
end
|
#kill_to_start ⇒ Object
952
953
954
955
956
|
# File 'lib/clacky/ui2/components/input_area.rb', line 952
def kill_to_start
chars = current_line.chars
@lines[@line_index] = chars[@cursor_position..-1]&.join || ""
@cursor_position = 0
end
|
#kill_word ⇒ Object
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
|
# File 'lib/clacky/ui2/components/input_area.rb', line 958
def kill_word
chars = current_line.chars
pos = @cursor_position - 1
while pos >= 0 && chars[pos] =~ /\s/
pos -= 1
end
while pos >= 0 && chars[pos] =~ /\S/
pos -= 1
end
delete_start = pos + 1
chars.slice!(delete_start...@cursor_position)
@lines[@line_index] = chars.join
@cursor_position = delete_start
end
|
#load_history_entry ⇒ Object
975
976
977
978
979
980
981
982
|
# File 'lib/clacky/ui2/components/input_area.rb', line 975
def load_history_entry
return unless @history_index >= 0 && @history_index < @history.size
entry = @history[@history_index]
@lines = entry.split("\n")
@lines = [""] if @lines.empty?
@line_index = @lines.size - 1
@cursor_position = current_line.chars.length
end
|
#mode_color_for(mode) ⇒ Object
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1243
def mode_color_for(mode)
case mode.to_s
when /auto_approve/
:magenta
when /confirm_safes/
:cyan
else
:white
end
end
|
#move_cursor(row, col) ⇒ Object
1322
1323
1324
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1322
def move_cursor(row, col)
print "\e[#{row + 1};#{col + 1}H"
end
|
#multiline? ⇒ Boolean
530
531
532
|
# File 'lib/clacky/ui2/components/input_area.rb', line 530
def multiline?
@lines.size > 1
end
|
#newline ⇒ Object
925
926
927
928
929
930
931
|
# File 'lib/clacky/ui2/components/input_area.rb', line 925
def newline
chars = current_line.chars
@lines[@line_index] = chars[0...@cursor_position].join
@lines.insert(@line_index + 1, chars[@cursor_position..-1]&.join || "")
@line_index += 1
@cursor_position = 0
end
|
#paste_from_clipboard ⇒ Object
989
990
991
992
993
994
995
996
997
998
|
# File 'lib/clacky/ui2/components/input_area.rb', line 989
def paste_from_clipboard
case RbConfig::CONFIG["host_os"]
when /darwin/i
paste_from_clipboard_macos
when /linux/i
paste_from_clipboard_linux
else
{ type: :text, text: "" }
end
end
|
#paste_from_clipboard_linux ⇒ Object
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1029
def paste_from_clipboard_linux
if system("which xclip >/dev/null 2>&1")
text = `xclip -selection clipboard -o 2>/dev/null`.to_s
text = Clacky::Utils::Encoding.to_utf8(text)
{ type: :text, text: text }
elsif system("which xsel >/dev/null 2>&1")
text = `xsel --clipboard --output 2>/dev/null`.to_s
text = Clacky::Utils::Encoding.to_utf8(text)
{ type: :text, text: text }
else
{ type: :text, text: "" }
end
rescue => e
{ type: :text, text: "" }
end
|
#paste_from_clipboard_macos ⇒ Object
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1000
def paste_from_clipboard_macos
has_image = system("osascript -e 'try' -e 'the clipboard as «class PNGf»' -e 'on error' -e 'return false' -e 'end try' >/dev/null 2>&1")
if has_image
temp_dir = Dir.tmpdir
temp_filename = "clipboard-#{Time.now.to_i}-#{rand(10000)}.png"
temp_path = File.join(temp_dir, temp_filename)
script = <<~APPLESCRIPT
set png_data to the clipboard as «class PNGf»
set the_file to open for access POSIX file "#{temp_path}" with write permission
write png_data to the_file
close access the_file
APPLESCRIPT
success = system("osascript", "-e", script, out: File::NULL, err: File::NULL)
if success && File.exist?(temp_path) && File.size(temp_path) > 0
return { type: :image, path: temp_path }
end
end
text = `pbpaste 2>/dev/null`.to_s
text = Clacky::Utils::Encoding.to_utf8(text)
{ type: :text, text: text }
rescue => e
{ type: :text, text: "" }
end
|
#pause ⇒ Object
Pause input area (when InlineInput is active)
485
486
487
|
# File 'lib/clacky/ui2/components/input_area.rb', line 485
def pause
@paused = true
end
|
#paused? ⇒ Boolean
495
496
497
|
# File 'lib/clacky/ui2/components/input_area.rb', line 495
def paused?
@paused
end
|
#position_cursor(start_row) ⇒ Object
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
# File 'lib/clacky/ui2/components/input_area.rb', line 343
def position_cursor(start_row)
cursor_row = start_row + 2 + @files.size content_width = effective_content_width(@width)
@lines[0...@line_index].each_with_index do |line, idx|
prefix = if idx == 0
prompt
else
" " * prompt.length
end
prefix_width = calculate_display_width(strip_ansi_codes(prefix))
available_width = [content_width - prefix_width, 20].max
wrapped_segments = wrap_line(line, available_width)
cursor_row += wrapped_segments.size
end
current = current_line
prefix = if @line_index == 0
prompt
else
" " * prompt.length
end
prefix_width = calculate_display_width(strip_ansi_codes(prefix))
available_width = [content_width - prefix_width, 20].max
wrapped_segments = wrap_line(current, available_width)
cursor_segment_idx = 0
cursor_pos_in_segment = @cursor_position
wrapped_segments.each_with_index do |segment, idx|
if @cursor_position >= segment[:start] && @cursor_position < segment[:end]
cursor_segment_idx = idx
cursor_pos_in_segment = @cursor_position - segment[:start]
break
elsif @cursor_position >= segment[:end] && idx == wrapped_segments.size - 1
cursor_segment_idx = idx
cursor_pos_in_segment = segment[:end] - segment[:start]
break
end
end
cursor_row += cursor_segment_idx
chars = current.chars
segment_start = wrapped_segments[cursor_segment_idx][:start]
text_in_segment_before_cursor = chars[segment_start...(segment_start + cursor_pos_in_segment)].join
display_width = calculate_display_width(text_in_segment_before_cursor)
cursor_col = prefix_width + display_width
move_cursor(cursor_row, cursor_col)
end
|
#print_with_padding(content) ⇒ Object
Print content and pad with spaces to clear any remaining characters from previous render This avoids flickering from clear_line while ensuring old content is erased
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
# File 'lib/clacky/ui2/components/input_area.rb', line 764
def print_with_padding(content)
visible_content = content.gsub(/\e\[[0-9;]*m/, '')
visible_width = calculate_display_width(visible_content)
if visible_width > @width
truncate_at = 0
current_width = 0
visible_content.each_char.with_index do |char, idx|
char_width = char_display_width(char)
break if current_width + char_width + 3 > @width current_width += char_width
truncate_at = idx + 1
end
print visible_content[0...truncate_at]
print "..."
remaining = @width - current_width - 3
print " " * remaining if remaining > 0
else
print content
remaining = @width - visible_width
print " " * remaining if remaining > 0
end
end
|
#prompt ⇒ Object
Get prompt symbol from theme
86
87
88
|
# File 'lib/clacky/ui2/components/input_area.rb', line 86
def prompt
"#{theme.symbol(:user)} "
end
|
#render(start_row:, width: nil) ⇒ Object
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# File 'lib/clacky/ui2/components/input_area.rb', line 280
def render(start_row:, width: nil)
@width = width || TTY::Screen.width
@last_render_row = start_row
return if @paused
current_row = start_row
render_sessionbar(current_row)
current_row += 1
render_separator(current_row)
current_row += 1
@files.each_with_index do |f, idx|
move_cursor(current_row, 0)
filename = f[:name] || f["name"] || "file"
size = f[:size] || f["size"]
size_str = size ? " #{format_filesize(size)}" : ""
content = @pastel.dim("[File #{idx + 1}] #{filename}#{size_str} (Ctrl+D to delete)")
print_with_padding(content)
current_row += 1
end
current_row = render_input_lines(current_row)
render_separator(current_row)
current_row += 1
if @command_suggestions && @command_suggestions.visible
print @command_suggestions.render(row: current_row, col: 0, width: [@width - 4, 60].min)
current_row += @command_suggestions.required_height
end
if @tips_message
move_cursor(current_row, 0)
content = format_tips(@tips_message, @tips_type)
print_with_padding(content)
current_row += 1
end
if @user_tip
move_cursor(current_row, 0)
content = format_user_tip(@user_tip)
print_with_padding(content)
current_row += 1
end
position_cursor(start_row)
flush
end
|
Render all input lines with auto-wrap support
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
# File 'lib/clacky/ui2/components/input_area.rb', line 673
def render_input_lines(start_row)
current_row = start_row
content_width = effective_content_width(@width)
@lines.each_with_index do |line, line_idx|
prefix = calculate_line_prefix(line_idx)
prefix_width = calculate_display_width(strip_ansi_codes(prefix))
available_width = content_width - prefix_width
wrapped_segments = wrap_line(line, available_width)
wrapped_segments.each_with_index do |segment_info, wrap_idx|
content = render_line_segment(line, line_idx, segment_info, wrap_idx, prefix, prefix_width)
move_cursor(current_row, 0)
print_with_padding(content)
current_row += 1
end
end
current_row
end
|
#render_line_segment_with_cursor(line, segment_start, segment_end) ⇒ String
Render a segment of a line with cursor if cursor is in this segment Applies theme colors to the text
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1068
def render_line_segment_with_cursor(line, segment_start, segment_end)
chars = line.chars
segment_chars = chars[segment_start...segment_end]
if @cursor_position >= segment_start && @cursor_position < segment_end
cursor_pos_in_segment = @cursor_position - segment_start
before_cursor = segment_chars[0...cursor_pos_in_segment].join
cursor_char = segment_chars[cursor_pos_in_segment] || " "
after_cursor = segment_chars[(cursor_pos_in_segment + 1)..-1]&.join || ""
"#{theme.format_text(before_cursor, :user)}#{@pastel.on_white(@pastel.black(cursor_char))}#{theme.format_text(after_cursor, :user)}"
elsif @cursor_position == segment_end && segment_end == line.length
segment_text = segment_chars.join
"#{theme.format_text(segment_text, :user)}#{@pastel.on_white(@pastel.black(' '))}"
else
theme.format_text(segment_chars.join, :user)
end
end
|
#render_line_with_cursor(line) ⇒ Object
1053
1054
1055
1056
1057
1058
1059
1060
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1053
def render_line_with_cursor(line)
chars = line.chars
before_cursor = chars[0...@cursor_position].join
cursor_char = chars[@cursor_position] || " "
after_cursor = chars[(@cursor_position + 1)..-1]&.join || ""
"#{@pastel.white(before_cursor)}#{@pastel.on_white(@pastel.black(cursor_char))}#{@pastel.white(after_cursor)}"
end
|
#render_separator(row) ⇒ Object
Render a separator line (ensures it doesn’t exceed screen width)
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1094
def render_separator(row)
move_cursor(row, 0)
separator_width = [@width, 1].max
content = @pastel.dim("─" * separator_width)
print content
remaining = @width - separator_width
print " " * remaining if remaining > 0
end
|
#render_sessionbar(row) ⇒ Integer
Render session bar with wrapping support
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1108
def render_sessionbar(row)
move_cursor(row, 0)
unless @sessionbar_info[:working_dir]
separator_width = [@width, 1].max
content = @pastel.dim("─" * separator_width)
print content
remaining = @width - separator_width
print " " * remaining if remaining > 0
return 1
end
session_line = build_sessionbar_content
print_with_padding(session_line)
1
end
|
#required_height ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/clacky/ui2/components/input_area.rb', line 90
def required_height
return 0 if @paused
height = 0
height += calculate_sessionbar_height
height += 1
height += @files.size
content_width = effective_content_width(@width)
@lines.each_with_index do |line, idx|
prefix = if idx == 0
prompt
else
" " * prompt.length
end
prefix_width = calculate_display_width(strip_ansi_codes(prefix))
available_width = [content_width - prefix_width, 20].max wrapped_segments = wrap_line(line, available_width)
height += wrapped_segments.size
end
height += 1
height += @command_suggestions.required_height if @command_suggestions
height += 1 if @tips_message
height += 1 if @user_tip
height
end
|
#resume ⇒ Object
Resume input area (when InlineInput is done)
490
491
492
|
# File 'lib/clacky/ui2/components/input_area.rb', line 490
def resume
@paused = false
end
|
#set_prompt(prompt) ⇒ Object
538
539
540
|
# File 'lib/clacky/ui2/components/input_area.rb', line 538
def set_prompt(prompt)
prompt = prompt
end
|
#set_skill_loader(skill_loader, agent_profile = nil) ⇒ Object
Set skill loader for command suggestions
136
137
138
139
|
# File 'lib/clacky/ui2/components/input_area.rb', line 136
def set_skill_loader(skill_loader, agent_profile = nil)
@skill_loader = skill_loader
@command_suggestions.load_skill_commands(skill_loader, agent_profile) if skill_loader
end
|
#set_tips(message, type: :info) ⇒ Object
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
# File 'lib/clacky/ui2/components/input_area.rb', line 402
def set_tips(message, type: :info)
if @tips_timer&.alive?
@tips_timer.kill
end
@tips_message = message
@tips_type = type
@tips_timer = Thread.new do
sleep 2
@tips_message = nil
tips_row = @last_render_row + 2 + @files.size + @lines.size + 1
move_cursor(tips_row, 0)
clear_line
flush
end
end
|
#shorten_path(path) ⇒ Object
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1221
def shorten_path(path)
return path if path.length <= 40
home = ENV["HOME"]
if home && path.start_with?(home)
path = path.sub(home, "~")
end
if path.length > 40
parts = path.split("/")
if parts.length > 3
".../" + parts[-3..-1].join("/")
else
path[0..40] + "..."
end
else
path
end
end
|
#show_user_tip(probability: 0.4, rotation_interval: 12, max_tips: 3) ⇒ Object
Show a random user tip with probability and auto-rotation (max 3 tips)
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
# File 'lib/clacky/ui2/components/input_area.rb', line 436
def show_user_tip(probability: 0.4, rotation_interval: 12, max_tips: 3)
return unless rand < probability
stop_user_tip_timer
@user_tip_count = 1
@user_tip = USER_TIPS.sample
@user_tip_timer = Thread.new do
while @user_tip_count < max_tips
sleep rotation_interval
@user_tip_count += 1
old_tip = @user_tip
loop do
@user_tip = USER_TIPS.sample
break if @user_tip != old_tip || USER_TIPS.size == 1
end
end
sleep rotation_interval
@user_tip = nil
@user_tip_count = 0
rescue => e
end
end
|
#status_color_for(status) ⇒ Object
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1254
def status_color_for(status)
case status.to_s.downcase
when 'idle'
:cyan when 'working'
:yellow else
:cyan
end
end
|
#strip_ansi_codes(text) ⇒ String
Strip ANSI escape codes from a string
758
759
760
|
# File 'lib/clacky/ui2/components/input_area.rb', line 758
def strip_ansi_codes(text)
text.gsub(/\e\[[0-9;]*m/, '')
end
|
#submit ⇒ Object
602
603
604
605
606
607
608
|
# File 'lib/clacky/ui2/components/input_area.rb', line 602
def submit
text = current_value
files = @files.dup
add_to_history(text) unless text.empty?
clear
{ text: text, files: files }
end
|
#theme ⇒ Object
Get current theme from ThemeManager
#update_sessionbar(session_id: nil, working_dir: nil, mode: nil, model: nil, tasks: nil, cost: nil, cost_source: nil, status: nil) ⇒ Object
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/clacky/ui2/components/input_area.rb', line 150
def update_sessionbar(session_id: nil, working_dir: nil, mode: nil, model: nil, tasks: nil, cost: nil, cost_source: nil, status: nil)
@sessionbar_info[:session_id] = session_id if session_id
@sessionbar_info[:working_dir] = working_dir if working_dir
@sessionbar_info[:mode] = mode if mode
@sessionbar_info[:model] = model if model
@sessionbar_info[:tasks] = tasks if tasks
@sessionbar_info[:cost] = cost if cost
@sessionbar_info[:cost_source] = cost_source if cost_source
@sessionbar_info[:status] = status if status
end
|
#wrap_line(line, max_width) ⇒ Array<Hash>
Wrap a line into multiple segments based on available width Considers display width of characters (multi-byte characters like Chinese)
744
745
746
|
# File 'lib/clacky/ui2/components/input_area.rb', line 744
def wrap_line(line, max_width)
super(line, max_width)
end
|