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_goal(goal) ⇒ Object
-
#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
79
|
# 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', goal: nil }
@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
989
990
991
992
|
# File 'lib/clacky/ui2/components/input_area.rb', line 989
def add_to_history(entry)
@history << entry
@history = @history.last(100) if @history.size > 100
end
|
#backspace ⇒ Object
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
# File 'lib/clacky/ui2/components/input_area.rb', line 556
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
756
757
758
|
# File 'lib/clacky/ui2/components/input_area.rb', line 756
def char_display_width(char)
super(char)
end
|
#clear ⇒ Object
595
596
597
598
599
600
601
602
603
604
605
|
# File 'lib/clacky/ui2/components/input_area.rb', line 595
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
1350
1351
1352
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1350
def clear_line
print "\e[2K"
end
|
#clear_tips ⇒ Object
429
430
431
432
433
434
435
|
# File 'lib/clacky/ui2/components/input_area.rb', line 429
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
476
477
478
479
480
|
# File 'lib/clacky/ui2/components/input_area.rb', line 476
def clear_user_tip
stop_user_tip_timer
@user_tip = nil
@user_tip_count = 0
end
|
#current_content ⇒ Object
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
# File 'lib/clacky/ui2/components/input_area.rb', line 504
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
1050
1051
1052
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1050
def current_line
@lines[@line_index] || ""
end
|
#current_value ⇒ Object
527
528
529
|
# File 'lib/clacky/ui2/components/input_area.rb', line 527
def current_value
expand_placeholders(@lines.join("\n"))
end
|
#cursor_down ⇒ Object
945
946
947
948
949
950
|
# File 'lib/clacky/ui2/components/input_area.rb', line 945
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
591
592
593
|
# File 'lib/clacky/ui2/components/input_area.rb', line 591
def cursor_end
@cursor_position = current_line.chars.length
end
|
#cursor_home ⇒ Object
587
588
589
|
# File 'lib/clacky/ui2/components/input_area.rb', line 587
def cursor_home
@cursor_position = 0
end
|
#cursor_left ⇒ Object
579
580
581
|
# File 'lib/clacky/ui2/components/input_area.rb', line 579
def cursor_left
@cursor_position = [@cursor_position - 1, 0].max
end
|
#cursor_right ⇒ Object
583
584
585
|
# File 'lib/clacky/ui2/components/input_area.rb', line 583
def cursor_right
@cursor_position = [@cursor_position + 1, current_line.chars.length].min
end
|
#cursor_up ⇒ Object
938
939
940
941
942
943
|
# File 'lib/clacky/ui2/components/input_area.rb', line 938
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
572
573
574
575
576
577
|
# File 'lib/clacky/ui2/components/input_area.rb', line 572
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
531
532
533
|
# File 'lib/clacky/ui2/components/input_area.rb', line 531
def empty?
@lines.all?(&:empty?) && @files.empty?
end
|
#expand_placeholders(text) ⇒ Object
1054
1055
1056
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1054
def expand_placeholders(text)
super(text, @paste_placeholders)
end
|
#flush ⇒ Object
1354
1355
1356
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1354
def flush
$stdout.flush
end
|
1322
1323
1324
1325
1326
1327
1328
1329
1330
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1322
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
|
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1304
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
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1335
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
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1289
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
871
872
873
|
# File 'lib/clacky/ui2/components/input_area.rb', line 871
def handle_ctrl_c
{ action: :interrupt }
end
|
#handle_ctrl_d ⇒ Object
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
|
# File 'lib/clacky/ui2/components/input_area.rb', line 875
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
859
860
861
862
863
864
865
866
867
868
869
|
# File 'lib/clacky/ui2/components/input_area.rb', line 859
def handle_down_arrow
if multiline?
unless cursor_down
history_next
end
else
history_next
end
{ action: nil }
end
|
#handle_enter ⇒ Object
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
841
842
843
844
845
|
# File 'lib/clacky/ui2/components/input_area.rb', line 799
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
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
279
280
281
282
283
|
# File 'lib/clacky/ui2/components/input_area.rb', line 170
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
|
# File 'lib/clacky/ui2/components/input_area.rb', line 891
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
847
848
849
850
851
852
853
854
855
856
857
|
# File 'lib/clacky/ui2/components/input_area.rb', line 847
def handle_up_arrow
if multiline?
unless cursor_up
history_prev
end
else
history_prev
end
{ action: nil }
end
|
#has_images? ⇒ Boolean
539
540
541
|
# File 'lib/clacky/ui2/components/input_area.rb', line 539
def has_images?
@files.any?
end
|
#history_next ⇒ Object
625
626
627
628
629
630
631
632
633
634
635
636
|
# File 'lib/clacky/ui2/components/input_area.rb', line 625
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
615
616
617
618
619
620
621
622
623
|
# File 'lib/clacky/ui2/components/input_area.rb', line 615
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
|
166
167
168
|
# File 'lib/clacky/ui2/components/input_area.rb', line 166
def input_buffer
@lines.join("\n")
end
|
#insert_char(char) ⇒ Object
--- Public editing methods ---
549
550
551
552
553
554
|
# File 'lib/clacky/ui2/components/input_area.rb', line 549
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
# File 'lib/clacky/ui2/components/input_area.rb', line 906
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
952
953
954
955
|
# File 'lib/clacky/ui2/components/input_area.rb', line 952
def kill_to_end
chars = current_line.chars
@lines[@line_index] = chars[0...@cursor_position].join
end
|
#kill_to_start ⇒ Object
957
958
959
960
961
|
# File 'lib/clacky/ui2/components/input_area.rb', line 957
def kill_to_start
chars = current_line.chars
@lines[@line_index] = chars[@cursor_position..-1]&.join || ""
@cursor_position = 0
end
|
#kill_word ⇒ Object
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
|
# File 'lib/clacky/ui2/components/input_area.rb', line 963
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
980
981
982
983
984
985
986
987
|
# File 'lib/clacky/ui2/components/input_area.rb', line 980
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
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1267
def mode_color_for(mode)
case mode.to_s
when /auto_approve/
:green
when /confirm_safes/
:cyan
else
:white
end
end
|
#move_cursor(row, col) ⇒ Object
1346
1347
1348
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1346
def move_cursor(row, col)
print "\e[#{row + 1};#{col + 1}H"
end
|
#multiline? ⇒ Boolean
535
536
537
|
# File 'lib/clacky/ui2/components/input_area.rb', line 535
def multiline?
@lines.size > 1
end
|
#newline ⇒ Object
930
931
932
933
934
935
936
|
# File 'lib/clacky/ui2/components/input_area.rb', line 930
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
994
995
996
997
998
999
1000
1001
1002
1003
|
# File 'lib/clacky/ui2/components/input_area.rb', line 994
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
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1034
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
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1005
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)
490
491
492
|
# File 'lib/clacky/ui2/components/input_area.rb', line 490
def pause
@paused = true
end
|
#paused? ⇒ Boolean
500
501
502
|
# File 'lib/clacky/ui2/components/input_area.rb', line 500
def paused?
@paused
end
|
#position_cursor(start_row) ⇒ Object
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
401
402
403
404
405
|
# File 'lib/clacky/ui2/components/input_area.rb', line 348
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
|
# File 'lib/clacky/ui2/components/input_area.rb', line 769
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
87
88
89
|
# File 'lib/clacky/ui2/components/input_area.rb', line 87
def prompt
"#{theme.symbol(:user)} "
end
|
#render(start_row:, width: nil) ⇒ Object
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
342
343
344
345
346
|
# File 'lib/clacky/ui2/components/input_area.rb', line 285
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
# File 'lib/clacky/ui2/components/input_area.rb', line 678
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
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1073
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
1058
1059
1060
1061
1062
1063
1064
1065
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1058
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)
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1099
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
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1113
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
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
132
|
# File 'lib/clacky/ui2/components/input_area.rb', line 91
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)
495
496
497
|
# File 'lib/clacky/ui2/components/input_area.rb', line 495
def resume
@paused = false
end
|
#set_goal(goal) ⇒ Object
162
163
164
|
# File 'lib/clacky/ui2/components/input_area.rb', line 162
def set_goal(goal)
@sessionbar_info[:goal] = goal
end
|
#set_prompt(prompt) ⇒ Object
543
544
545
|
# File 'lib/clacky/ui2/components/input_area.rb', line 543
def set_prompt(prompt)
prompt = prompt
end
|
#set_skill_loader(skill_loader, agent_profile = nil) ⇒ Object
Set skill loader for command suggestions
137
138
139
140
|
# File 'lib/clacky/ui2/components/input_area.rb', line 137
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
# File 'lib/clacky/ui2/components/input_area.rb', line 407
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
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1245
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)
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
469
470
471
472
473
|
# File 'lib/clacky/ui2/components/input_area.rb', line 441
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
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
|
# File 'lib/clacky/ui2/components/input_area.rb', line 1278
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
763
764
765
|
# File 'lib/clacky/ui2/components/input_area.rb', line 763
def strip_ansi_codes(text)
text.gsub(/\e\[[0-9;]*m/, '')
end
|
#submit ⇒ Object
607
608
609
610
611
612
613
|
# File 'lib/clacky/ui2/components/input_area.rb', line 607
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
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/clacky/ui2/components/input_area.rb', line 151
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)
749
750
751
|
# File 'lib/clacky/ui2/components/input_area.rb', line 749
def wrap_line(line, max_width)
super(line, max_width)
end
|