Module: OllamaChat::StateSelectors::Common

Includes:
Feedback, Utils::Chooser, Term::ANSIColor
Included in:
DatabaseStateSelector, StateSelector
Defined in:
lib/ollama_chat/state_selectors.rb

Overview

The Common module provides shared logic and utility methods for all state selection mechanisms within OllamaChat::StateSelectors.

It encapsulates fundamental behaviors such as state validation, terminal-based user interaction, and ANSI-colored output, ensuring consistency across both memory-based and database-backed state selectors.

Constant Summary

Constants included from Feedback

Feedback::PREFIXES

Instance Attribute Summary collapse

Attributes included from Utils::Chooser

#current_search_state Stores the

Instance Method Summary collapse

Methods included from Utils::Chooser

#choose_entry, #choose_with_state

Methods included from Feedback

#feedback

Instance Attribute Details

#allow_empty ⇒ TrueClass, FalseClass (readonly)

The allow_empty reader returns whether the selector is allowed to be empty.

Returns:

  • (TrueClass, FalseClass) —

    true if the selector can be empty, false otherwise



43
44
45
# File 'lib/ollama_chat/state_selectors.rb', line 43

def allow_empty
  @allow_empty
end

#default ⇒ String? (readonly)

The default reader returns the default state for this selector.

Returns:

  • (String, nil) —

    the default state



33
34
35
# File 'lib/ollama_chat/state_selectors.rb', line 33

def default
  @default
end

#name ⇒ String (readonly)

The name reader returns the name of the state selector.

Returns:

  • (String) —

    the name of the state selector



23
24
25
# File 'lib/ollama_chat/state_selectors.rb', line 23

def name
  @name
end

#off ⇒ Array<String> (readonly)

The off reader returns the list of states that are considered "off".

Returns:

  • (Array<String>) —

    the list of "off" states



38
39
40
# File 'lib/ollama_chat/state_selectors.rb', line 38

def off
  @off
end

#states ⇒ Array<String> (readonly)

The states reader returns the set of valid states for this selector.

Returns:

  • (Array<String>) —

    the set of valid states



28
29
30
# File 'lib/ollama_chat/state_selectors.rb', line 28

def states
  @states
end

Instance Method Details

#allow_empty? ⇒ TrueClass, FalseClass

The allow_empty? method checks if the switch is allowed to be empty.

Returns:

  • (TrueClass, FalseClass) —

    true if the switch is allowed to be empty, false otherwise



49
50
51
# File 'lib/ollama_chat/state_selectors.rb', line 49

def allow_empty?
  !!allow_empty
end

#choose ⇒ nil

The choose method presents an interactive menu to select from available states.

It displays the states with visual indicators: a door (🚪) for exiting, a green circle (🟢) for the currently selected state, and a black circle (⚫️) for others. The method updates the selected state based on user input or exits the chooser if '[EXIT]' or a cancellation is chosen.

Returns:

  • (nil) —

    This method does not return a value; it updates the instance variable @selected based on user input.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ollama_chat/state_selectors.rb', line 80

def choose
  currently_selected = selected
  states = [ '[EXIT]' ] + self.states.to_a
  states = states.map do |state|
    display_prefix =
      case state
      when '[EXIT]'           then '🚪'
      when currently_selected then '🟢'
      else                         '⚫️'
      end
    display = '%s %s' % [ display_prefix, state ]
    SearchUI::Wrapper.new(state, display:)
  end

  chosen = choose_entry(
    states,
    prompt: 'Which operational paradigm should be engaged? %s'
  )
  case chosen
  when '[EXIT]', nil
    feedback("Exiting chooser.")
  when
    self.selected = chosen.value
  end
end

#off? ⇒ TrueClass, FalseClass

The off? method checks if the current state is in the off set.

Returns:

  • (TrueClass, FalseClass) —

    true if the selected state is in the off set, false otherwise



57
58
59
# File 'lib/ollama_chat/state_selectors.rb', line 57

def off?
  off.member?(selected)
end

#on? ⇒ TrueClass, FalseClass

The on? method checks if the switch is in the on state, returning true if it is enabled and false if it is disabled.

Returns:

  • (TrueClass, FalseClass) —

    true if the switch is on, false if it is off



66
67
68
# File 'lib/ollama_chat/state_selectors.rb', line 66

def on?
  !off?
end

#show(output: STDOUT) ⇒ Object

The show method outputs the current value of the state selector.

This method displays the name of the state selector along with its currently selected state in a formatted message to standard output.

Parameters:

  • output (IO) (defaults to: STDOUT) —

    the output stream to write the message to



112
113
114
# File 'lib/ollama_chat/state_selectors.rb', line 112

def show(output: STDOUT)
  feedback("#{name} is #{bold(to_s)}.", output:)
end

#to_s ⇒ String

The to_s method returns the string representation of the selected state.

Returns:

  • (String) —

    the string representation of the currently selected state



120
121
122
# File 'lib/ollama_chat/state_selectors.rb', line 120

def to_s
  selected.to_s
end