Class: Strata::CLI::FieldEditor

Inherits:
Object
  • Object
show all
Includes:
Terminal
Defined in:
lib/strata/cli/ui/field_editor.rb

Overview

Interactive field editor with row-by-row navigation. Displays a table of AI-generated fields and allows inline editing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Terminal

#create_spinner, #print_table, #with_spinner

Constructor Details

#initialize(fields, prompt: TTY::Prompt.new, table_context: nil, model_context: nil) ⇒ FieldEditor

Returns a new instance of FieldEditor.

Parameters:

  • fields (Array<Hash>)

    Fields from AI with :name, :description, :expression, :schema_type, :data_type

  • table_context (Hash, nil) (defaults to: nil)

    Optional context with :table_name, :columns, :datasource for prompt mode

  • model_context (Hash, nil) (defaults to: nil)

    Optional model metadata with :description



20
21
22
23
24
25
26
27
# File 'lib/strata/cli/ui/field_editor.rb', line 20

def initialize(fields, prompt: TTY::Prompt.new, table_context: nil, model_context: nil)
  @fields = fields.map { |f| f.merge(skipped: false) }
  @prompt = prompt
  @pastel = Pastel.new
  @selected_index = 0
  @model_context = model_context || {description: ""}
  @table_context = table_context
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



15
16
17
# File 'lib/strata/cli/ui/field_editor.rb', line 15

def fields
  @fields
end

#model_contextObject (readonly)

Returns the value of attribute model_context.



15
16
17
# File 'lib/strata/cli/ui/field_editor.rb', line 15

def model_context
  @model_context
end

#pastelObject (readonly)

Returns the value of attribute pastel.



15
16
17
# File 'lib/strata/cli/ui/field_editor.rb', line 15

def pastel
  @pastel
end

#promptObject (readonly)

Returns the value of attribute prompt.



15
16
17
# File 'lib/strata/cli/ui/field_editor.rb', line 15

def prompt
  @prompt
end

Instance Method Details

#runArray<Hash>?

Run the interactive editor

Returns:

  • (Array<Hash>, nil)

    Confirmed fields or nil if cancelled



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
# File 'lib/strata/cli/ui/field_editor.rb', line 31

def run
  loop do
    clear_screen
    display_table
    display_help
    show_cursor

    key = prompt.reader.read_keypress

    case key
    when "\e[A", "k" # Up arrow or k
      @selected_index = [@selected_index - 1, 0].max
    when "\e[B", "j" # Down arrow or j
      @selected_index = [@selected_index + 1, @fields.length - 1].min
    when "\r", "\n" # Enter
      handle_edit
    when "s", "S"
      toggle_skip
    when "p", "P"
      handle_prompt_mode
    when "c", "C"
      if prompt.yes?("Confirm and generate model file?")
        show_cursor
        return {fields: active_fields, model_context: @model_context}
      end
    when "q", "Q", "\e" # q or Escape
      if prompt.yes?("Quit without saving?")
        show_cursor
        return nil
      end
    end
  end
end