Skip to content
Kward Search API index

Class: Kward::PromptInterface

Inherits:
Object
  • Object
show all
Includes:
ApprovalPrompt, ComposerController, ComposerRenderer, EditorAutoClosePairs, EditorAutoIndent, EditorController, EditorEndwise, EditorExplorer, EditorOutline, EditorRenderer, EditorRunner, EditorSyntaxHighlighter, EditorWordCompletion, EmacsEditorMode, FileOverlay, GeneratedImagePreview, GitPrompt, InteractiveRenderer, InteractiveState, KeyHandler, Layout, ModernEditorMode, OverlayRenderer, PluginUIRequests, ProjectBrowser, PromptRenderer, QuestionPrompt, RuntimeState, Screen, SelectionPrompt, SlashOverlay, TranscriptRenderer, VibeEditorMode, VibeInsertReadline
Defined in:
lib/kward/prompt_interface.rb,
lib/kward/prompt_interface/banner.rb,
lib/kward/prompt_interface/layout.rb,
lib/kward/prompt_interface/screen.rb,
lib/kward/prompt_interface/git_prompt.rb,
lib/kward/prompt_interface/key_handler.rb,
lib/kward/prompt_interface/editor/state.rb,
lib/kward/prompt_interface/file_overlay.rb,
lib/kward/prompt_interface/stream_state.rb,
lib/kward/prompt_interface/editor/buffer.rb,
lib/kward/prompt_interface/editor/runner.rb,
lib/kward/prompt_interface/editor/search.rb,
lib/kward/prompt_interface/runtime_state.rb,
lib/kward/prompt_interface/slash_overlay.rb,
lib/kward/prompt_interface/composer_state.rb,
lib/kward/prompt_interface/editor/endwise.rb,
lib/kward/prompt_interface/editor/outline.rb,
lib/kward/prompt_interface/approval_prompt.rb,
lib/kward/prompt_interface/editor/explorer.rb,
lib/kward/prompt_interface/editor/renderer.rb,
lib/kward/prompt_interface/project_browser.rb,
lib/kward/prompt_interface/prompt_renderer.rb,
lib/kward/prompt_interface/question_prompt.rb,
lib/kward/prompt_interface/editor/file_save.rb,
lib/kward/prompt_interface/editor/kill_ring.rb,
lib/kward/prompt_interface/overlay_renderer.rb,
lib/kward/prompt_interface/selection_prompt.rb,
lib/kward/prompt_interface/composer_renderer.rb,
lib/kward/prompt_interface/editor/controller.rb,
lib/kward/prompt_interface/editor/modes/vibe.rb,
lib/kward/prompt_interface/editor/selections.rb,
lib/kward/prompt_interface/editor/vibe_state.rb,
lib/kward/prompt_interface/interactive/state.rb,
lib/kward/prompt_interface/project_file_tree.rb,
lib/kward/prompt_interface/transcript_buffer.rb,
lib/kward/prompt_interface/editor/auto_indent.rb,
lib/kward/prompt_interface/editor/buffer_diff.rb,
lib/kward/prompt_interface/editor/modes/emacs.rb,
lib/kward/prompt_interface/editor/status_text.rb,
lib/kward/prompt_interface/plugin_ui_requests.rb,
lib/kward/prompt_interface/composer_controller.rb,
lib/kward/prompt_interface/editor/modes/modern.rb,
lib/kward/prompt_interface/editor/runner_state.rb,
lib/kward/prompt_interface/editor/undo_history.rb,
lib/kward/prompt_interface/transcript_renderer.rb,
lib/kward/prompt_interface/interactive/renderer.rb,
lib/kward/prompt_interface/editor/word_completion.rb,
lib/kward/prompt_interface/interactive/controller.rb,
lib/kward/prompt_interface/editor/auto_close_pairs.rb,
lib/kward/prompt_interface/generated_image_preview.rb,
lib/kward/prompt_interface/editor/indent_navigation.rb,
lib/kward/prompt_interface/editor/syntax_highlighter.rb,
lib/kward/prompt_interface/editor/modes/vibe_insert_readline.rb

Overview

Interactive terminal UI used by the CLI frontend.

Defined Under Namespace

Modules: ApprovalPrompt, ComposerController, ComposerRenderer, EditorAutoClosePairs, EditorAutoIndent, EditorBufferDiff, EditorController, EditorEndwise, EditorExplorer, EditorFileSave, EditorOutline, EditorRenderer, EditorRunner, EditorStatusText, EditorSyntaxHighlighter, EditorWordCompletion, EmacsEditorMode, FileOverlay, GeneratedImagePreview, GitPrompt, InteractiveRenderer, InteractiveState, KeyHandler, Layout, ModernEditorMode, OverlayRenderer, PluginUIRequests, ProjectBrowser, PromptRenderer, QuestionPrompt, RuntimeState, Screen, SelectionPrompt, SlashOverlay, TranscriptRenderer, VibeEditorMode, VibeInsertReadline Classes: Banner, ComposerState, EditorBuffer, EditorIndentNavigation, EditorKillRing, EditorRunnerState, EditorSearch, EditorSelections, EditorState, EditorUndoHistory, InteractiveController, ProjectFileTree, StreamState, SubmittedInput, TerminalHandoffOutput, TranscriptBuffer, VibeEditorState

Constant Summary collapse

HELP_TEXT =
"Enter sends • Shift+Enter newline • / commands • @ files • ↑/↓ history • Ctrl+D exits".freeze
BUSY_HELP_TEXT =
"Ctrl+C cancels".freeze
SPINNER_FRAMES =
%w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏].freeze
SPINNER_INTERVAL =
0.1
1.0
COMPOSER_STATUS_REFRESH_INTERVAL =
1.0
COMPLETION_DISPLAY_SECONDS =
0.8
RESPONSE_ARRIVAL_SECONDS =
0.4
BUSY_ELAPSED_DELAY_SECONDS =
2.0
TRANSCRIPT_COMPOSER_GAP_ROWS =
1
COMPOSER_MAX_INPUT_ROWS =
6
IMAGE_VIEWER_MAX_ROWS =
8
TRANSCRIPT_BUFFER_LIMIT =
200_000
Banner::MESSAGE
KEYBOARD_PROTOCOL_ENABLE =
TerminalSequences::KEYBOARD_PROTOCOL_ENABLE
KEYBOARD_PROTOCOL_RESTORE =
TerminalSequences::KEYBOARD_PROTOCOL_RESTORE
BRACKETED_PASTE_ENABLE =
TerminalSequences::BRACKETED_PASTE_ENABLE
BRACKETED_PASTE_RESTORE =
TerminalSequences::BRACKETED_PASTE_RESTORE
BRACKETED_PASTE_START =
TerminalSequences::BRACKETED_PASTE_START
BRACKETED_PASTE_END =
TerminalSequences::BRACKETED_PASTE_END
SYNCHRONIZED_OUTPUT_ENABLE =
TerminalSequences::SYNCHRONIZED_OUTPUT_ENABLE
SYNCHRONIZED_OUTPUT_DISABLE =
TerminalSequences::SYNCHRONIZED_OUTPUT_DISABLE
CURSOR_SHOW =
TerminalSequences::CURSOR_SHOW
CURSOR_HIDE =
TerminalSequences::CURSOR_HIDE
CURSOR_SHAPE_DEFAULT =
TerminalSequences::CURSOR_SHAPE_DEFAULT
CURSOR_SHAPE_BAR =
TerminalSequences::CURSOR_SHAPE_BAR
SHIFT_ENTER_SEQUENCES =
TerminalKeys::SHIFT_ENTER
EXIT_INPUT =
:exit_input
CANCEL_INPUT =
:cancel_input
SELECT_CANCEL =
:select_cancel
SELECT_CONTINUE =
:select_continue
SELECT_ACTION_MINIMUM_BUSY_SECONDS =
1.0

Constants included from KeyHandler

KeyHandler::EDITOR_WHEEL_BATCH_SIZE, KeyHandler::SGR_MOUSE_PATTERN

Constants included from VibeEditorMode

VibeEditorMode::VIBE_PAIR_TEXT_OBJECTS, VibeEditorMode::VIBE_RUBY_BLOCK_OPENERS, VibeEditorMode::VIBE_RUBY_EXTENSIONS, VibeEditorMode::VIBE_RUBY_PATHS, VibeEditorMode::VIBE_SIMPLE_MOTION_KEYS, VibeEditorMode::VIBE_VISUAL_LINE_MOVE_CONTROLS

Constants included from EditorAutoIndent

EditorAutoIndent::C_LIKE_INDENT_LANGUAGES, EditorAutoIndent::EDITOR_SHIFT_TAB_SEQUENCES, EditorAutoIndent::EDITOR_TAB_SEQUENCES, EditorAutoIndent::LUA_INDENT_KEYWORDS, EditorAutoIndent::PUNCTUATION_INDENT_LANGUAGES, EditorAutoIndent::PUNCTUATION_PAIRS, EditorAutoIndent::PYTHON_INDENT_KEYWORDS, EditorAutoIndent::RUBY_INDENT_KEYWORDS, EditorAutoIndent::SHELL_DEDENT_KEYWORDS, EditorAutoIndent::SHELL_INDENT_KEYWORDS

Constants included from EditorWordCompletion

EditorWordCompletion::EDITOR_COMPLETION_PREFIX_PATTERN, EditorWordCompletion::EDITOR_COMPLETION_SUFFIX_PATTERN, EditorWordCompletion::EDITOR_COMPLETION_WORD_PATTERN

Constants included from EditorEndwise

EditorEndwise::ENDWISE_ENDLESS_DEFINITION, EditorEndwise::ENDWISE_LANGUAGES, EditorEndwise::ENDWISE_LINE_PARSE_LIMIT, EditorEndwise::ENDWISE_SINGLE_LINE_DEFINITION

Constants included from EditorAutoClosePairs

EditorAutoClosePairs::AUTO_CLOSE_CLOSERS, EditorAutoClosePairs::AUTO_CLOSE_OPENERS, EditorAutoClosePairs::AUTO_CLOSE_PAIRS, EditorAutoClosePairs::AUTO_CLOSE_QUOTES, EditorAutoClosePairs::WORD_CHARACTER

Constants included from EditorSyntaxHighlighter

EditorSyntaxHighlighter::CSS_EXTENSIONS, EditorSyntaxHighlighter::CSS_PATTERN, EditorSyntaxHighlighter::ERB_CLOSE_PATTERN, EditorSyntaxHighlighter::ERB_EXTENSIONS, EditorSyntaxHighlighter::ERB_OPEN_PATTERN, EditorSyntaxHighlighter::GENERIC_STRING_NUMBER_PATTERN, EditorSyntaxHighlighter::HTML_EXTENSIONS, EditorSyntaxHighlighter::HTML_TAG_START_PATTERN, EditorSyntaxHighlighter::JSON_EXTENSIONS, EditorSyntaxHighlighter::JSON_PATTERN, EditorSyntaxHighlighter::LANGUAGE_DEFINITIONS, EditorSyntaxHighlighter::MARKDOWN_EXTENSIONS, EditorSyntaxHighlighter::MARKDOWN_PATTERN, EditorSyntaxHighlighter::RUBY_EXTENSIONS, EditorSyntaxHighlighter::RUBY_FILENAMES, EditorSyntaxHighlighter::RUBY_KEYWORDS, EditorSyntaxHighlighter::RUBY_PATTERN, EditorSyntaxHighlighter::SCSS_EXTENSIONS, EditorSyntaxHighlighter::SQL_EXTENSIONS, EditorSyntaxHighlighter::SQL_PATTERN, EditorSyntaxHighlighter::YAML_EXTENSIONS, EditorSyntaxHighlighter::YAML_PATTERN

Constants included from EditorOutline

EditorOutline::OUTLINE_DELAY, EditorOutline::OUTLINE_MIN_WIDTH, EditorOutline::OUTLINE_TOGGLE

Constants included from EditorExplorer

EditorExplorer::EXPLORER_MAX_INDENT_DEPTH, EditorExplorer::EXPLORER_SPLIT_MIN_WIDTH, EditorExplorer::EXPLORER_TOGGLE

Constants included from ProjectBrowser

ProjectBrowser::IMAGE_VIEWER_CELL_HEIGHT_TO_WIDTH, ProjectBrowser::IMAGE_VIEWER_MAX_ZOOM, ProjectBrowser::IMAGE_VIEWER_MIN_ZOOM, ProjectBrowser::IMAGE_VIEWER_ZOOM_STEP, ProjectBrowser::PROJECT_BROWSER_DIRECTORY_ICON, ProjectBrowser::PROJECT_BROWSER_EXTENSION_ICONS, ProjectBrowser::PROJECT_BROWSER_FILENAME_ICONS, ProjectBrowser::PROJECT_BROWSER_FILE_ICON, ProjectBrowser::PROJECT_BROWSER_MARKDOWN_ICON, ProjectBrowser::PROJECT_BROWSER_RESULT_LIMIT, ProjectBrowser::PROJECT_BROWSER_ROOT, ProjectBrowser::PROJECT_BROWSER_RUBY_ICON, ProjectBrowser::PROJECT_BROWSER_STATE_VERSION, ProjectBrowser::PROJECT_BROWSER_YAML_ICON

Instance Method Summary collapse

Methods included from GitPrompt

#git_commit_message, #open_modal_diff_viewer

Methods included from ApprovalPrompt

#ask_tool_approval

Methods included from PluginUIRequests

#request_plugin_ui

Methods included from GeneratedImagePreview

#offer_generated_images, #take_generated_image_offers

Methods included from ProjectBrowser

#open_project_browser, #open_project_browser_locked

Constructor Details

#initialize(input: $stdin, output: $stdout, slash_commands: [], overlay_settings: nil, project_browser_icon_theme: "off", footer: nil, composer_status: nil, busy_help: true, attachment_badges: nil, attachment_parser: nil, banner_message: nil, tab_keybindings: nil, prompt_history: nil, workspace_root: Dir.pwd, editor_mode: nil, editor_mode_source: nil, editor_auto_indent: true, editor_auto_indent_source: nil, editor_auto_close_pairs: true, editor_auto_close_pairs_source: nil, editor_soft_wrap: true, editor_soft_wrap_source: nil, editor_bar_cursor: true, editor_bar_cursor_source: nil, editor_line_numbers: "absolute", editor_line_numbers_source: nil, diff_view: "auto", diff_view_source: nil, editor_runners_source: nil, redraw_handler: nil) ⇒ PromptInterface

Returns a new instance of PromptInterface.



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
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/kward/prompt_interface.rb', line 227

def initialize(input: $stdin, output: $stdout, slash_commands: [], overlay_settings: nil, project_browser_icon_theme: "off", footer: nil, composer_status: nil, busy_help: true, attachment_badges: nil, attachment_parser: nil, banner_message: nil, tab_keybindings: nil, prompt_history: nil, workspace_root: Dir.pwd, editor_mode: nil, editor_mode_source: nil, editor_auto_indent: true, editor_auto_indent_source: nil, editor_auto_close_pairs: true, editor_auto_close_pairs_source: nil, editor_soft_wrap: true, editor_soft_wrap_source: nil, editor_bar_cursor: true, editor_bar_cursor_source: nil, editor_line_numbers: "absolute", editor_line_numbers_source: nil, diff_view: "auto", diff_view_source: nil, editor_runners_source: nil, redraw_handler: nil)
  @input_io = input
  @output_io = output
  # Logical state may change during a child handoff, but only the owner may render.
  @terminal_owner = :kward
  @terminal_handoff_mode = nil
  @reader = TTY::Reader.new(input: input, output: output, interrupt: :error)
  @mutex = Mutex.new
  @prompt_history = prompt_history
  @workspace_root = File.expand_path(workspace_root.to_s.empty? ? Dir.pwd : workspace_root)
  @composer = ComposerState.new
  load_history(@prompt_history.values) if @prompt_history
  @started = false
  @asking = false
  @busy = false
  @busy_activity = "thinking"
  @busy_activity_visible = true
  @busy_started_at = nil
  @last_busy_elapsed = nil
  @completion_status = nil
  @response_arrival_until = nil
  @queued_count = 0
  @steered_count = 0
  @spinner_frame_index = 0
  @last_spinner_tick = monotonic_now
  @last_footer_refresh = nil
  @cached_footer_text = nil
  @last_composer_status_refresh = nil
  @cached_composer_status_text = nil
  @prompt_label = "You>"
  @assistant_label = "Assistant"
  @stream_state = StreamState.new
  @rendered_rows = 0
  @last_composer_rows = []
  @rendered_composer_cursor = nil
  @transcript_buffer = TranscriptBuffer.new(limit: TRANSCRIPT_BUFFER_LIMIT)
  @transcript_viewport_rows = 0
  @restoring_transcript = false
  @pending_keys = []
  @handoff_input_buffer = +"".b
  @completion_provider = nil
  @completion_cycle = nil
  @completion_overlay = nil
  @redraw_handler = redraw_handler
  @transcript_redraw_requested = false
  @original_console_mode = nil
  @raw_mode_active = false
  @slash_commands = normalize_slash_commands(slash_commands)
  @slash_selection_index = 0
  @slash_overlay_dismissed_input = nil
  @slash_overlay_disabled = false
  @file_selection_index = 0
  @file_overlay_dismissed_token = nil
  @file_open_dismissed_token = nil
  @file_editor_open_status = nil
  @file_mention_paths = nil
  @file_mention_discovery_thread = nil
  @file_mention_discovery_root = nil
  @file_mention_discovery_token = nil
  @project_browser_file_paths = nil
  @project_browser_state = nil
  @image_viewer_state = nil
  @terminal_image_protocol = nil
  @project_browser_restore_after_editor = false
  @editor_state = nil
  @editor_runner_state = nil
  @editor_runner_completion = nil
  @editor_runner_output_visible = false
  @editor_runner_selection_anchor = nil
  @editor_runner_selection_cursor = nil
  @editor_runner_mouse_dragging = false
  @editor_runner_last_click = nil
  @editor_agent_suspended = false
  @editor_prompt_active = false
  @editor_prompt_input = ""
  @interactive_state = nil
  @last_interactive_tick = monotonic_now
  @select_state = nil
  @question_state = nil
  @question_prompt_active = false
  @git_state = nil
  @last_width = screen_width
  @last_height = screen_height
  @reserved_rows = 0
  @color_enabled = ANSI.enabled?(output)
  @cursor_visible = true
  @editor_bar_cursor_active = false
  @synchronized_output_depth = 0
  @overlay_settings = normalize_overlay_settings(overlay_settings)
  @project_browser_icon_theme = normalize_project_browser_icon_theme(project_browser_icon_theme)
  @footer = footer
  @composer_status = composer_status
  @busy_help = busy_help
  @attachment_badges = attachment_badges
  @attachment_parser = attachment_parser
  @banner = Banner.new(message: banner_message, screen_height: method(:screen_height))
  @tabs = []
  @active_tab_index = 0
  @tab_keybindings = normalize_tab_keybindings(tab_keybindings)
  @editor_mode = normalize_editor_mode(editor_mode)
  @editor_mode_source = editor_mode_source
  @editor_auto_indent = editor_auto_indent != false
  @editor_auto_indent_source = editor_auto_indent_source
  @editor_auto_close_pairs = editor_auto_close_pairs != false
  @editor_auto_close_pairs_source = editor_auto_close_pairs_source
  @editor_soft_wrap = editor_soft_wrap != false
  @editor_soft_wrap_source = editor_soft_wrap_source
  @editor_bar_cursor = editor_bar_cursor != false
  @editor_bar_cursor_source = editor_bar_cursor_source
  @editor_line_numbers = normalize_editor_line_numbers(editor_line_numbers)
  @editor_line_numbers_source = editor_line_numbers_source
  @diff_view = DiffViewMode.normalize(diff_view)
  @diff_view_source = diff_view_source
  @editor_runners_source = editor_runners_source
end

Instance Method Details

#ask(message = "You>") ⇒ Object



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/kward/prompt_interface.rb', line 645

def ask(message = "You>")
  was_composing = @started && @asking
  start
  @mutex.synchronize do
    preserve_input = was_composing && !@busy && !composer_input.empty?
    @prompt_label = message.to_s
    unless preserve_input
      self.composer_input = @composer.prefill_input.to_s
      @composer.prefill_input = nil
      self.composer_cursor = composer_input.length
      @composer.clear_attachments
      reset_history_navigation
    end
    @pending_keys.clear
    @completion_cycle = nil
    @completion_overlay = nil
    @asking = true
    @busy = false
    @queued_count = 0
    render_prompt_locked
  end

  loop do
    key = read_key(nonblock: true)
    result = nil
    @mutex.synchronize do
      if key.nil?
        resized = handle_resize_locked
        chrome_changed = tick_composer_chrome_locked
        completion_changed = tick_completion_locked
        preview_opened = open_pending_generated_image_locked
        render_prompt_locked if resized || chrome_changed || completion_changed || preview_opened
      else
        result = handle_key_before_redraw(key)
        render_prompt_locked unless result.is_a?(String) || result == EXIT_INPUT || prompt_action_result?(result)
      end
    end
    perform_pending_transcript_redraw
    return result if result.is_a?(String) || prompt_action_result?(result)
    return nil if result == EXIT_INPUT

    sleep 0.02 if key.nil?
  end
ensure
  perform_pending_transcript_redraw
end

#ask_user_question(questions) ⇒ Object



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
# File 'lib/kward/prompt_interface.rb', line 764

def ask_user_question(questions)
  return [] if questions.empty?

  start
  saved_state = nil
  answers = []
  @mutex.synchronize do
    @question_prompt_active = true
    saved_state = begin_question_prompt_state
  end

  questions.each_with_index do |question, index|
    answer = ask_single_user_question(question, index + 1, questions.length)
    if answer == SELECT_CANCEL
      finish_question_prompt(saved_state)
      return nil
    end
    answers << answer
  end

  finish_question_prompt(saved_state)
  answers
rescue StandardError
  finish_question_prompt(saved_state) if saved_state
  raise
end

#begin_busy_input(message = "You>", activity: "thinking", show_activity: true) ⇒ Object



1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
# File 'lib/kward/prompt_interface.rb', line 1029

def begin_busy_input(message = "You>", activity: "thinking", show_activity: true)
  start
  @mutex.synchronize do
    @prompt_label = message.to_s
    @busy_activity = normalize_busy_activity(activity)
    @busy_activity_visible = show_activity
    @completion_status = nil
    @response_arrival_until = nil
    self.composer_input = ""
    self.composer_cursor = 0
    @composer.clear_attachments
              @pending_keys.clear
    @asking = true
    @busy = true
    @busy_started_at = monotonic_now
    @last_busy_elapsed = nil
    @queued_count = 0
    @steered_count = 0
    reset_spinner_locked
    reset_history_navigation
    render_prompt_locked
  end
end

#clear_steered_count ⇒ Object



1081
1082
1083
1084
1085
1086
1087
# File 'lib/kward/prompt_interface.rb', line 1081

def clear_steered_count
  @mutex.synchronize do
    @steered_count = 0
    @busy_activity = "thinking"
    render_prompt_locked if @asking
  end
end

#clear_transcript ⇒ Object



1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
# File 'lib/kward/prompt_interface.rb', line 1262

def clear_transcript
  @mutex.synchronize do
    @transcript_buffer.clear
    @transcript_viewport_rows = 0
    @stream_state.finish_block
    @stream_state.reset
    width, height = screen_size
    with_synchronized_output_locked { redraw_screen_locked(width: width, height: height) }
    flush_output_locked
  end
end

#close ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/kward/prompt_interface.rb', line 366

def close
  @mutex.synchronize do
    return unless @started

    persist_editor_explorer
    cancel_editor_outline

    clear_image_viewer_output_locked if image_viewer_active?
    clear_prompt_for_output_locked
    last_transcript_row = transcript_bottom_row
    restore_scroll_region_locked
    # Resetting the scroll region homes the cursor on VT terminals.
    move_to_screen(last_transcript_row, 1)
    disable_editor_mouse_reporting(force: true)
    print_output_locked(BRACKETED_PASTE_RESTORE)
    print_output_locked(KEYBOARD_PROTOCOL_RESTORE)
    restore_editor_cursor_shape_locked
    set_cursor_visible_locked(true, force: true)
    puts_output_locked
    flush_output_locked
    @started = false
    restore_console_mode_locked
  end
end

#commit_editor_prompt(session) ⇒ Object

Presents a completed agent draft for review before changing the buffer.



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/kward/prompt_interface.rb', line 510

def commit_editor_prompt(session)
  @mutex.synchronize do
    return false unless editor_active? && @editor_agent_suspended
    unless @editor_state.equal?(session.context[:buffer_identity]) &&
        @editor_state.buffer.equal?(session.context[:buffer_revision]) && @editor_state.buffer == session.initial_content
      @editor_state.status = "Agent draft discarded · buffer changed"
      return false
    end

    unless session.changed?
      @editor_state.status = "Agent made no buffer changes"
      render_prompt_locked if @started
      return false
    end

    draft = session.content.dup.freeze
    open_editor_comparison(@editor_state.buffer, draft, from: "Buffer", to: "Agent draft",
                           status: "Agent draft · K keep · Esc discard")
    @editor_comparison[:draft] = draft
    @editor_agent_suspended = true
    render_prompt_locked if @started
    true
  end
end

#composer_snapshot ⇒ Object



924
925
926
927
928
929
930
931
# File 'lib/kward/prompt_interface.rb', line 924

def composer_snapshot
  @mutex.synchronize do
    {
      composer: @composer,
      prompt_label: @prompt_label
    }
  end
end

#edit_file(path, base_dir: nil, allow_new: true) ⇒ Object



564
565
566
567
568
# File 'lib/kward/prompt_interface.rb', line 564

def edit_file(path, base_dir: nil, allow_new: true)
  run_editor_session do
    open_editor(path, allow_new: allow_new, base_dir: base_dir || prompt_workspace_root, restrict_to_workspace: false)
  end
end

#edit_workspace(root) ⇒ Object



570
571
572
573
# File 'lib/kward/prompt_interface.rb', line 570

def edit_workspace(root)
  update_workspace_root(root)
  run_editor_session { open_editor_workspace }
end

#editing_file? ⇒ Boolean

Returns:

  • (Boolean)


469
470
471
# File 'lib/kward/prompt_interface.rb', line 469

def editing_file?
  @mutex.synchronize { editor_active? }
end

#editor_prompt_context ⇒ Object

Returns the active Vibe or Modern editor buffer for an agent prompt turn.



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/kward/prompt_interface.rb', line 474

def editor_prompt_context
  @mutex.synchronize do
    next nil unless editor_active? && !@editor_state.readonly? && (@editor_state.vibe? || @editor_state.modern?)

    cursor = @editor_state.cursor_line_and_column
    line = cursor.first
    line_start = @editor_state.line_start_offset(line)
    {
      path: @editor_state.path,
      display_path: @editor_state.display_path,
      workspace_root: @workspace_root,
      language: @editor_state.language,
      content: @editor_state.buffer.dup.freeze,
      buffer_identity: @editor_state,
      buffer_revision: @editor_state.buffer,
      cursor: cursor,
      cursor_offset: @editor_state.cursor,
      cursor_line_range: [line_start, line_start + @editor_state.lines[line].length],
      selections: @editor_state.prompt_selections
    }
  end
end

#filter_terminal_handoff_input(chunk) ⇒ Object



1118
1119
1120
# File 'lib/kward/prompt_interface.rb', line 1118

def filter_terminal_handoff_input(chunk)
  @mutex.synchronize { filter_terminal_handoff_input_locked(chunk) }
end

#finish_busy_input ⇒ Object



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/kward/prompt_interface.rb', line 1089

def finish_busy_input
  @mutex.synchronize do
    @last_busy_elapsed = monotonic_now - @busy_started_at if @busy_started_at
    @busy_started_at = nil
    @busy = false
    @busy_activity = "thinking"
    @busy_activity_visible = true
    @response_arrival_until = nil
    @queued_count = 0
    @steered_count = 0
    @asking = true
    render_prompt_locked
  end
end

#finish_interactive ⇒ Object



807
808
809
810
811
812
813
814
815
816
817
# File 'lib/kward/prompt_interface.rb', line 807

def finish_interactive
  @mutex.synchronize do
    return unless @interactive_state

    snapshot = @interactive_state[:snapshot]
    @interactive_state = nil
    restore_composer_snapshot_locked(snapshot)
    redraw_screen_locked if @started
    flush_output_locked
  end
end

#finish_stream_block ⇒ Object



1204
1205
1206
1207
1208
# File 'lib/kward/prompt_interface.rb', line 1204

def finish_stream_block
  @mutex.synchronize do
    write_stream_block_locked(nil, "", finish: true)
  end
end

#flush_terminal_handoff_escape ⇒ Object



1122
1123
1124
# File 'lib/kward/prompt_interface.rb', line 1122

def flush_terminal_handoff_escape
  @mutex.synchronize { flush_terminal_handoff_escape_locked }
end

#inline_image_protocol ⇒ Object



547
548
549
# File 'lib/kward/prompt_interface.rb', line 547

def inline_image_protocol
  @mutex.synchronize { terminal_image_protocol }
end

#interactive_active? ⇒ Boolean

Returns:

  • (Boolean)


795
796
797
# File 'lib/kward/prompt_interface.rb', line 795

def interactive_active?
  @mutex.synchronize { interactive_active_locked? }
end

#interactive_exited? ⇒ Boolean

Returns:

  • (Boolean)


799
800
801
802
803
804
805
# File 'lib/kward/prompt_interface.rb', line 799

def interactive_exited?
  @mutex.synchronize do
    return false unless @interactive_state

    @interactive_state[:controller].exited?
  end
end

Returns:

  • (Boolean)


791
792
793
# File 'lib/kward/prompt_interface.rb', line 791

def modal_active?
  @mutex.synchronize { modal_active_locked? }
end

#new_composer_state_with_history ⇒ Object



983
984
985
986
987
# File 'lib/kward/prompt_interface.rb', line 983

def new_composer_state_with_history
  composer = ComposerState.new
  composer.load_history(@prompt_history.values) if @prompt_history
  composer
end

#picker_choice_width ⇒ Object



717
718
719
# File 'lib/kward/prompt_interface.rb', line 717

def picker_choice_width
  [overlay_card_width(screen_width) - 6, 1].max
end

#poll_input ⇒ Object



1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/kward/prompt_interface.rb', line 1126

def poll_input
  key = read_key(nonblock: true)
  @mutex.synchronize do
    if interactive_active_locked?
      if key.nil?
        resized = handle_resize_locked
        ticked = tick_interactive_locked
        render_prompt_locked if resized || ticked
        return :interactive_exited if @interactive_state[:controller].exited?
        return nil
      end

      route_interactive_key(key)
      ticked = tick_interactive_locked
      render_prompt_locked if ticked
      return :interactive_exited if @interactive_state[:controller].exited?
      return nil
    end

    if key.nil?
      resized = handle_resize_locked
      spun = tick_spinner_locked
      chrome_changed = tick_composer_chrome_locked
      completion_changed = tick_completion_locked
      preview_opened = open_pending_generated_image_locked
      render_prompt_locked if resized || spun || chrome_changed || completion_changed || preview_opened
      return nil
    end

    if modal_active_locked?
      queue_pending_keys(key)
      return nil
    end

    result = handle_key_before_redraw(key)
    render_prompt_locked unless [EXIT_INPUT, CANCEL_INPUT].include?(result) || prompt_action_result?(result)
    result
  end
ensure
  perform_pending_transcript_redraw
end

#prefill_input(value) ⇒ Object

Prefills the next composer input without submitting it.



693
694
695
696
697
# File 'lib/kward/prompt_interface.rb', line 693

def prefill_input(value)
  @mutex.synchronize do
    @composer.prefill_input = value.to_s
  end
end


1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
# File 'lib/kward/prompt_interface.rb', line 1174

def print_visual_banner(message = nil)
  @mutex.synchronize do
    width, height = screen_size
    rows = banner_rows(width, message: message)
    return if rows.empty?

    with_synchronized_output_locked do
      prepare_transcript_output_locked
      write_transcript_text_locked(rows.join("\n"))
      write_transcript_text_locked("\n")
      remember_transcript_viewport_locked(height)
      @stream_state.finish_block
      restore_composer_cursor_locked
    end
    flush_output_locked
  end
end

#record_transient_terminal_output(text, render: true) ⇒ Object



1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
# File 'lib/kward/prompt_interface.rb', line 1210

def record_transient_terminal_output(text, render: true)
  value = text.to_s
  return if value.empty?

  @mutex.synchronize do
    append_transcript_buffer(value)
    append_transcript_buffer("\n") unless value.end_with?("\n")
    @stream_state.finish_block
    width, height = screen_size
    remember_transcript_viewport_locked(height)
    next unless render

    with_synchronized_output_locked { redraw_screen_locked(width: width, height: height) }
    flush_output_locked
  end
end

#redraw ⇒ Object



1244
1245
1246
1247
1248
1249
1250
# File 'lib/kward/prompt_interface.rb', line 1244

def redraw
  @mutex.synchronize do
    width, height = screen_size
    with_synchronized_output_locked { redraw_screen_locked(width: width, height: height) }
    flush_output_locked
  end
end

#refresh_composer_status ⇒ Object



1252
1253
1254
1255
1256
1257
1258
1259
1260
# File 'lib/kward/prompt_interface.rb', line 1252

def refresh_composer_status
  @mutex.synchronize do
    @cached_footer_text = nil
    @last_footer_refresh = nil
    @cached_composer_status_text = nil
    @last_composer_status_refresh = nil
    render_prompt_locked if @started && @asking
  end
end

#restore_composer_snapshot(snapshot) ⇒ Object



955
956
957
958
959
960
961
# File 'lib/kward/prompt_interface.rb', line 955

def restore_composer_snapshot(snapshot)
  @mutex.synchronize do
    restore_composer_snapshot_locked(snapshot)
    restore_editor_snapshot_locked(snapshot)
    redraw_screen_locked if @started
  end
end

#restore_composer_snapshot_locked(snapshot) ⇒ Object



975
976
977
978
979
980
981
# File 'lib/kward/prompt_interface.rb', line 975

def restore_composer_snapshot_locked(snapshot)
  @composer = snapshot[:composer] || new_composer_state_with_history
  @completion_cycle = nil
  @completion_overlay = nil
  @prompt_label = snapshot[:prompt_label].to_s.empty? ? "You>" : snapshot[:prompt_label].to_s
  @last_composer_rows = []
end

#restore_editor_snapshot_locked(snapshot) ⇒ Object



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
# File 'lib/kward/prompt_interface.rb', line 989

def restore_editor_snapshot_locked(snapshot)
  editor_was_active = editor_active?
  cancel_editor_runner
  @editor_runner_state = nil
  @editor_runner_completion = nil
  @editor_runner_output_visible = false
  clear_editor_runner_selection
  @vibe_pane_prefix_editor = nil
  @editor_comparison = snapshot[:editor_comparison]&.then { |comparison| comparison.merge(source: comparison[:source].dup) }
  @editor_save_conflict = snapshot[:editor_save_conflict]&.dup
  @editor_state = snapshot[:editor_state]&.dup
  @editor_explorer = editor_explorer_snapshot(snapshot[:editor_explorer])
  @editor_outline_visible = snapshot[:editor_outline_visible]
  @editor_outline_focused = false
  cancel_editor_outline
  @retained_editor_buffers = snapshot[:retained_editor_buffers]&.map(&:dup)
  @editor_text_width = nil
  editor_is_active = editor_active?

  if editor_is_active
    enable_editor_mouse_reporting unless editor_was_active
  else
    disable_editor_mouse_reporting(force: true)
  end
end

#restore_tab_view_snapshot(snapshot) ⇒ Object



963
964
965
966
967
968
969
970
971
972
973
# File 'lib/kward/prompt_interface.rb', line 963

def restore_tab_view_snapshot(snapshot)
  @mutex.synchronize do
    restore_composer_snapshot_locked(snapshot)
    restore_editor_snapshot_locked(snapshot)
    @transcript_buffer = snapshot[:transcript_buffer] || TranscriptBuffer.new(limit: TRANSCRIPT_BUFFER_LIMIT)
    @transcript_viewport_rows = snapshot[:transcript_viewport_rows].to_i
    @stream_state = snapshot[:stream_state] || StreamState.new
    @last_composer_rows = []
    redraw_screen_locked if @started
  end
end

#restore_transcript ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/kward/prompt_interface.rb', line 415

def restore_transcript
  start(render: false) unless @started
  @mutex.synchronize do
    print_output_locked(SYNCHRONIZED_OUTPUT_ENABLE)
    clear_prompt_for_output_locked unless @rendered_rows.zero? && @last_composer_rows.empty?
    @transcript_buffer.clear
    @transcript_viewport_rows = 0
    @stream_state.finish_block
    @stream_state.reset
    @restoring_transcript = true
  end

  yield
ensure
  @mutex.synchronize do
    @restoring_transcript = false
    width, height = screen_size
    redraw_screen_locked(width: width, height: height)
    print_output_locked(SYNCHRONIZED_OUTPUT_DISABLE)
    flush_output_locked
  end
end

#resume_editor_for_agent(status: nil) ⇒ Object

Resumes the editor after an agent prompt turn.



536
537
538
539
540
541
542
543
544
545
# File 'lib/kward/prompt_interface.rb', line 536

def resume_editor_for_agent(status: nil)
  @mutex.synchronize do
    return false unless editor_active?

    @editor_agent_suspended = false
    @editor_state.status = status.to_s unless status.to_s.empty?
    render_prompt_locked if @started
    true
  end
end

#review_document(title:, content:, &on_save) ⇒ Object

Opens an in-memory document editor. The callback receives edited content when the user saves; return an error message to keep the editor open.

Raises:

  • (ArgumentError)


605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/kward/prompt_interface.rb', line 605

def review_document(title:, content:, &on_save)
  raise ArgumentError, "review_document requires a save callback" unless on_save

  start(render: false)
  opened = @mutex.synchronize do
    @editor_save_callback = on_save
    open_scratchpad(:markdown, content: content).tap do
      @editor_state.status = "#{title} · Ctrl+S save · Ctrl+Q cancel"
      render_prompt_locked
    end
  end
  return false unless opened

  run_editor
end

#run_editor ⇒ Object



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/kward/prompt_interface.rb', line 621

def run_editor
  loop do
    key = read_key(nonblock: true)
    action = nil
    editor_open = @mutex.synchronize do
      if key.nil?
        resized = handle_resize_locked
        chrome_changed = tick_composer_chrome_locked
        render_prompt_locked if resized || chrome_changed
      else
        result = handle_key_before_redraw(key)
        action = result if prompt_action_result?(result)
        render_prompt_locked unless result.is_a?(String) || result == EXIT_INPUT || prompt_action_result?(result)
      end
      editor_active?
    end
    return action if action
    break unless editor_open

    sleep 0.02 if key.nil?
  end
  true
end

#say(message) ⇒ Object



391
392
393
# File 'lib/kward/prompt_interface.rb', line 391

def say(message)
  write_transcript_message(message, preserve_composer: false)
end

#say_visual(message) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/kward/prompt_interface.rb', line 399

def say_visual(message)
  @mutex.synchronize do
    return if @restoring_transcript

    with_synchronized_output_locked do
      clear_prompt_for_output_locked
      text = message.to_s
      write_visual_transcript_text_locked(text)
      write_visual_transcript_text_locked("\n") unless text.end_with?("\n")
      @stream_state.finish_block
      render_prompt_after_output_locked
    end
    flush_output_locked
  end
end

#scratchpad(language = :text) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
# File 'lib/kward/prompt_interface.rb', line 591

def scratchpad(language = :text)
  start(render: false)
  opened = @mutex.synchronize do
    open_scratchpad(language).tap do
      render_prompt_locked
    end
  end
  return false unless opened

  run_editor
end

#select(message, choices, title: "Sessions", custom: false, initial_index: 0, action_keys: {}, action_handlers: {}) ⇒ Object



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/kward/prompt_interface.rb', line 721

def select(message, choices, title: "Sessions", custom: false, initial_index: 0, action_keys: {}, action_handlers: {})
  return nil if choices.empty? && !custom

  start
  @mutex.synchronize do
    prepare_modal_input_locked(message, clear_attachments: true)
    choice_labels = choices.map(&:to_s)
    selection_index = choice_labels.empty? ? 0 : [[initial_index.to_i, 0].max, choice_labels.length - 1].min
    @select_state = { choices: choice_labels, selection_index: selection_index, title: title.to_s, custom: custom, action_keys: normalized_select_action_keys(action_keys), search_active: false }
    render_prompt_locked
  end

  loop do
    key = read_key(nonblock: true)
    result = nil
    @mutex.synchronize do
      if key.nil?
        resized = handle_resize_locked
        chrome_changed = tick_composer_chrome_locked
        render_prompt_locked if resized || chrome_changed
      else
        result = handle_select_key(key)
        result = drain_pending_select_keys_locked(result)
        render_prompt_locked unless result.is_a?(String) || select_action_result?(result) || result == SELECT_CANCEL
      end
    end

    if select_action_result?(result) && select_action_handler(result, action_handlers)
      action_result = run_select_action(result, select_action_handler(result, action_handlers))
      next if action_result == SELECT_CONTINUE

      return action_result
    end

    if result.is_a?(String) || select_action_result?(result) || result == SELECT_CANCEL
      finish_select_prompt(render: !select_deferred_finish_render?(result))
      return result == SELECT_CANCEL ? nil : result
    end

    sleep 0.02 if key.nil?
  end
end

#set_queued_count(count) ⇒ Object



1065
1066
1067
1068
1069
1070
1071
# File 'lib/kward/prompt_interface.rb', line 1065

def set_queued_count(count)
  @mutex.synchronize do
    @queued_count = count.to_i
    @steered_count = 0 if @queued_count.positive?
    render_prompt_locked if @asking
  end
end

#set_steered_count(count) ⇒ Object



1073
1074
1075
1076
1077
1078
1079
# File 'lib/kward/prompt_interface.rb', line 1073

def set_steered_count(count)
  @mutex.synchronize do
    @steered_count = count.to_i
    @queued_count = 0 if @steered_count.positive?
    render_prompt_locked if @asking
  end
end

#show_completion(status) ⇒ Object



1111
1112
1113
1114
1115
1116
# File 'lib/kward/prompt_interface.rb', line 1111

def show_completion(status)
  @mutex.synchronize do
    @completion_status = completion_status(status)
    render_prompt_locked if @started && @asking
  end
end

#show_response_arrival ⇒ Object



1104
1105
1106
1107
1108
1109
# File 'lib/kward/prompt_interface.rb', line 1104

def show_response_arrival
  @mutex.synchronize do
    @response_arrival_until = monotonic_now + RESPONSE_ARRIVAL_SECONDS
    render_prompt_locked if @started && @asking && @busy
  end
end

#start(render: true) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/kward/prompt_interface.rb', line 352

def start(render: true)
  @mutex.synchronize do
    return if @started

    enter_raw_mode_locked
    @started = true
    @asking = true
    disable_editor_mouse_reporting(force: true) unless editor_active?
    print_output_locked(KEYBOARD_PROTOCOL_ENABLE)
    print_output_locked(BRACKETED_PASTE_ENABLE)
    render_prompt_locked if render
  end
end

#start_interactive(title:, rows:, fps:) ⇒ Object



891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/kward/prompt_interface.rb', line 891

def start_interactive(title:, rows:, fps:)
  snapshot = composer_snapshot
  controller = InteractiveController.new(width: interactive_canvas_width, height: rows, fps: fps)
  start
  @mutex.synchronize do
    @interactive_state = {
      title: title.to_s,
      rows: rows,
      controller: controller,
      snapshot: snapshot
    }
    @last_interactive_tick = monotonic_now
    @asking = true
    @busy = false
    @last_composer_rows = []
    render_prompt_locked
  end
  controller
end

#start_stream_block(label) ⇒ Object



1192
1193
1194
1195
1196
# File 'lib/kward/prompt_interface.rb', line 1192

def start_stream_block(label)
  @mutex.synchronize do
    write_stream_block_locked(label, "", finish: false)
  end
end

#suspend_editor_for_agent ⇒ Object

Suspends the live editor while an agent prompt is running.



498
499
500
501
502
503
504
505
506
507
# File 'lib/kward/prompt_interface.rb', line 498

def suspend_editor_for_agent
  @mutex.synchronize do
    return false unless editor_active? && !@editor_agent_suspended

    @editor_agent_suspended = true
    @editor_state.status = "Agent is working · Ctrl+C cancels"
    render_prompt_locked if @started
    true
  end
end

#tab_view_snapshot(include_transcript: true) ⇒ Object



933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
# File 'lib/kward/prompt_interface.rb', line 933

def tab_view_snapshot(include_transcript: true)
  @mutex.synchronize do
    persist_editor_explorer
    snapshot = {
      composer: @composer.dup,
      prompt_label: @prompt_label.dup,
      editor_explorer: editor_explorer_snapshot,
      editor_outline_visible: @editor_outline_visible,
      retained_editor_buffers: @retained_editor_buffers&.map(&:dup),
      editor_comparison: @editor_comparison&.merge(source: @editor_comparison[:source].dup),
      editor_save_conflict: @editor_save_conflict&.dup,
      editor_state: @editor_state&.dup
    }
    if include_transcript
      snapshot[:transcript_buffer] = @transcript_buffer.dup
      snapshot[:transcript_viewport_rows] = @transcript_viewport_rows
      snapshot[:stream_state] = @stream_state.dup
    end
    snapshot
  end
end

#update_assistant_label(label) ⇒ Object



1168
1169
1170
1171
1172
# File 'lib/kward/prompt_interface.rb', line 1168

def update_assistant_label(label)
  @mutex.synchronize do
    @assistant_label = label.to_s.empty? ? "Assistant" : label.to_s
  end
end

#update_busy_activity(activity) ⇒ Object



1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/kward/prompt_interface.rb', line 1053

def update_busy_activity(activity)
  @mutex.synchronize do
    value = normalize_busy_activity(activity)
    return false if value == @busy_activity

    @busy_activity = value
    @image_generation_started_at = value == "Generating image…" ? monotonic_now : nil
    render_prompt_locked if @started && @asking && @busy
    true
  end
end

#update_completion_provider(provider, slash_overlay: true) ⇒ Object



449
450
451
452
453
454
455
456
457
# File 'lib/kward/prompt_interface.rb', line 449

def update_completion_provider(provider, slash_overlay: true)
  @mutex.synchronize do
    @completion_provider = provider
    @completion_cycle = nil
    @completion_overlay = nil
    @slash_overlay_disabled = !slash_overlay
    render_prompt_locked if @started
  end
end

#update_overlay_settings(settings) ⇒ Object



1015
1016
1017
1018
1019
1020
# File 'lib/kward/prompt_interface.rb', line 1015

def update_overlay_settings(settings)
  @mutex.synchronize do
    @overlay_settings = normalize_overlay_settings(settings)
    render_prompt_locked if @started && @asking
  end
end

#update_project_browser_icon_theme(theme) ⇒ Object



1022
1023
1024
1025
1026
1027
# File 'lib/kward/prompt_interface.rb', line 1022

def update_project_browser_icon_theme(theme)
  @mutex.synchronize do
    @project_browser_icon_theme = normalize_project_browser_icon_theme(theme)
    render_prompt_locked if @started && @asking && project_browser_visible?
  end
end

#update_prompt_label(label) ⇒ Object

Updates the visible prompt label while the composer is active.



700
701
702
703
704
705
# File 'lib/kward/prompt_interface.rb', line 700

def update_prompt_label(label)
  @mutex.synchronize do
    @prompt_label = label.to_s
    render_prompt_locked if @started && @asking
  end
end

#update_slash_commands(commands) ⇒ Object



343
344
345
346
347
348
349
350
# File 'lib/kward/prompt_interface.rb', line 343

def update_slash_commands(commands)
  @mutex.synchronize do
    @slash_commands = normalize_slash_commands(commands)
    reset_slash_selection
    @slash_overlay_dismissed_input = nil
    render_prompt_locked if @started
  end
end

#update_tabs(labels:, active_index: 0) ⇒ Object



911
912
913
914
915
916
917
918
919
920
921
922
# File 'lib/kward/prompt_interface.rb', line 911

def update_tabs(labels:, active_index: 0)
  @mutex.synchronize do
    normalized_labels = Array(labels).map { |label| normalize_tab_label(label) }
    normalized_index = active_index.to_i
    return false if @tabs == normalized_labels && @active_tab_index == normalized_index

    @tabs = normalized_labels
    @active_tab_index = normalized_index
    render_prompt_locked if @started && @asking
    true
  end
end

#update_workspace_root(root, prompt_history: nil) ⇒ Object



551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/kward/prompt_interface.rb', line 551

def update_workspace_root(root, prompt_history: nil)
  @mutex.synchronize do
    expanded_root = File.expand_path(root.to_s.empty? ? Dir.pwd : root)
    return false if expanded_root == @workspace_root

    @workspace_root = expanded_root
    @prompt_history = prompt_history if prompt_history
    load_history(@prompt_history.values) if @prompt_history
    reset_workspace_file_state_locked
    true
  end
end

#with_completion_provider(provider, slash_overlay: true) ⇒ Object



438
439
440
441
442
443
444
445
446
447
# File 'lib/kward/prompt_interface.rb', line 438

def with_completion_provider(provider, slash_overlay: true)
  previous_provider = @completion_provider
  previous_slash_overlay_disabled = @slash_overlay_disabled
  @completion_provider = provider
  @slash_overlay_disabled = !slash_overlay
  yield
ensure
  @completion_provider = previous_provider
  @slash_overlay_disabled = previous_slash_overlay_disabled
end

#with_inline_terminal_handoff(preserve_tab_keybindings: false, &block) ⇒ Object



819
820
821
# File 'lib/kward/prompt_interface.rb', line 819

def with_inline_terminal_handoff(preserve_tab_keybindings: false, &block)
  with_terminal_handoff(inline: true, preserve_tab_keybindings: preserve_tab_keybindings, &block)
end

#with_prompt_history(history) ⇒ Object



459
460
461
462
463
464
465
466
467
# File 'lib/kward/prompt_interface.rb', line 459

def with_prompt_history(history)
  previous_history = @prompt_history
  @prompt_history = history
  load_history(@prompt_history.values) if @prompt_history
  yield
ensure
  @prompt_history = previous_history
  load_history(@prompt_history.values) if @prompt_history
end

#with_terminal_handoff(inline: false, preserve_tab_keybindings: false) ⇒ Object



823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'lib/kward/prompt_interface.rb', line 823

def with_terminal_handoff(inline: false, preserve_tab_keybindings: false)
  start
  input = nil
  output = nil
  transition = nil
  keep_view = false
  @mutex.synchronize do
    if inline
      handle_resize_locked
      reserve_composer_region_locked
      move_to_transcript_cursor_locked
    else
      clear_prompt_for_output_locked
      restore_scroll_region_locked
    end
    disable_editor_mouse_reporting(force: true)
    print_output_locked(BRACKETED_PASTE_RESTORE)
    print_output_locked(KEYBOARD_PROTOCOL_RESTORE) unless preserve_tab_keybindings
    @handoff_input_buffer.clear
    @handoff_escape_deadline = nil
    @handoff_bracketed_paste = false
    restore_editor_cursor_shape_locked
    set_cursor_visible_locked(true, force: true)
    flush_output_locked
    restore_console_mode_locked
    input = @input_io
    output = if preserve_tab_keybindings
      TerminalHandoffOutput.new(@output_io)
    else
      @output_io
    end
    transition = method(:switch_terminal_handoff_to_exclusive)
    @terminal_handoff_mode = inline ? :inline : :exclusive
    @terminal_owner = :child
  end

  yield(input, output, transition, -> { keep_view = true })
ensure
  @mutex.synchronize do
    width, height = screen_size
    keep_view &&= @terminal_handoff_mode == :inline && width == @last_width && height == @last_height
    @terminal_owner = :kward
    @terminal_handoff_mode = nil
    print_output_locked(TerminalSequences::CONTROL_SEQUENCE_CANCEL)
    print_output_locked(TerminalSequences::COMPOSER_MODES_RESTORE)
    restore_scroll_region_locked unless keep_view
    print_output_locked(TerminalSequences::SGR_RESET)
    enter_raw_mode_locked
    print_output_locked(preserve_tab_keybindings ? TerminalSequences::KEYBOARD_PROTOCOL_SET : KEYBOARD_PROTOCOL_ENABLE)
    print_output_locked(BRACKETED_PASTE_ENABLE)
    @handoff_input_buffer.clear
    @handoff_escape_deadline = nil
    @handoff_bracketed_paste = false
    disable_editor_mouse_reporting(force: true)
    restore_editor_cursor_shape_locked(force: true)
    @cursor_visible = nil
    @last_composer_rows = []
    with_synchronized_output_locked do
      if keep_view
        resume_inline_terminal_view_locked(height)
      else
        redraw_screen_locked(width: width, height: height)
      end
    end
    flush_output_locked
  end
end

#write_delta(delta) ⇒ Object



1198
1199
1200
1201
1202
# File 'lib/kward/prompt_interface.rb', line 1198

def write_delta(delta)
  @mutex.synchronize do
    write_stream_block_locked(nil, delta.to_s, finish: false)
  end
end

#write_stream_block(label, delta, finish: false) ⇒ Object



1238
1239
1240
1241
1242
# File 'lib/kward/prompt_interface.rb', line 1238

def write_stream_block(label, delta, finish: false)
  @mutex.synchronize do
    write_stream_block_locked(label, delta.to_s, finish: finish)
  end
end

#write_transcript(message) ⇒ Object



395
396
397
# File 'lib/kward/prompt_interface.rb', line 395

def write_transcript(message)
  write_transcript_message(message, preserve_composer: true)
end

#write_transcript_delta(delta) ⇒ Object



1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
# File 'lib/kward/prompt_interface.rb', line 1227

def write_transcript_delta(delta)
  @mutex.synchronize do
    with_synchronized_output_locked do
      prepare_transcript_output_locked unless @restoring_transcript
      write_transcript_text_locked(delta.to_s)
      restore_composer_cursor_locked unless @restoring_transcript
    end
    flush_output_locked unless @restoring_transcript
  end
end

#yes?(message, default: false) ⇒ Boolean

Returns:

  • (Boolean)


707
708
709
710
711
712
713
714
715
# File 'lib/kward/prompt_interface.rb', line 707

def yes?(message, default: false)
  answer = ask("#{message} #{default ? "[Y/n]" : "[y/N]"}")
  return default if answer.nil?

  answer = answer.strip.downcase
  return default if answer.empty?

  answer.start_with?("y")
end