Class: AIA::HistoryManager

Inherits:
Object
  • Object
show all
Defined in:
lib/aia/history_manager.rb

Constant Summary collapse

MAX_VARIABLE_HISTORY =
5

Instance Method Summary collapse

Constructor Details

#initialize(prompt:) ⇒ HistoryManager

prompt is PromptManager::Prompt instance



11
12
13
14
# File 'lib/aia/history_manager.rb', line 11

def initialize(prompt:)
  @prompt  = prompt
  @history = []
end

Instance Method Details

#get_variable_history(variable, value = '') ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aia/history_manager.rb', line 35

def get_variable_history(variable, value = '')
  return if value.nil? || value.empty?

  values = @prompt.parameters[variable]
  if values.include?(value)
    values.delete(value)
  end

  values << value

  if values.size > MAX_VARIABLE_HISTORY
    values.shift
  end

  @prompt.parameters[variable] = values
end

#historyObject



17
18
19
# File 'lib/aia/history_manager.rb', line 17

def history
  @history
end

#history=(new_history) ⇒ Object



22
23
24
# File 'lib/aia/history_manager.rb', line 22

def history=(new_history)
  @history = new_history
end

#request_variable_value(variable_name:, history_values: []) ⇒ Object



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
80
81
82
83
# File 'lib/aia/history_manager.rb', line 53

def request_variable_value(variable_name:, history_values: [])
  setup_variable_history(history_values) # Setup Reline's history for completion

  default_value = history_values.last || ''
  question = "Value for #{variable_name} (#{default_value}): "

  # Ensure Reline is writing to stdout explicitly for this interaction
  Reline.output = $stdout

  # Store the original prompt proc to restore later
  original_prompt_proc = Reline.line_editor.prompt_proc

  # Note: Temporarily setting prompt_proc might not be needed if passing prompt to readline works.
  # Reline.line_editor.prompt_proc = ->(context) { [question] }

  begin
    input = Reline.readline(question, true)
    return default_value if input.nil? # Handle Ctrl+D -> use default

    chosen_value = input.strip.empty? ? default_value : input.strip
    # Update the persistent history for this variable
    get_variable_history(variable_name, chosen_value)
    return chosen_value
  rescue Interrupt
    puts "\nVariable input interrupted."
    exit(1) # Exit cleanly on Ctrl+C
  ensure
    # Restore the original prompt proc
    Reline.line_editor.prompt_proc = original_prompt_proc
  end
end

#setup_variable_history(history_values) ⇒ Object



27
28
29
30
31
32
# File 'lib/aia/history_manager.rb', line 27

def setup_variable_history(history_values)
  Reline::HISTORY.clear
  history_values.each do |value|
    Reline::HISTORY.push(value) unless value.nil? || value.empty?
  end
end