Module: OllamaChat::StateSelectors::Common

Includes:
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.

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

Instance Attribute Details

#allow_emptyTrueClass, 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



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

def allow_empty
  @allow_empty
end

#defaultString? (readonly)

The default reader returns the default state for this selector.

Returns:

  • (String, nil)

    the default state



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

def default
  @default
end

#nameString (readonly)

The name reader returns the name of the state selector.

Returns:

  • (String)

    the name of the state selector



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

def name
  @name
end

#offArray<String> (readonly)

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

Returns:

  • (Array<String>)

    the list of “off” states



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

def off
  @off
end

#statesSet<String> (readonly)

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

Returns:

  • (Set<String>)

    the set of valid states



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

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



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

def allow_empty?
  !!allow_empty
end

#choosenil

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.



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

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

  case chosen = choose_entry(states)
  when '[EXIT]', nil
    STDOUT.puts "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



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

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



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

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



107
108
109
# File 'lib/ollama_chat/state_selectors.rb', line 107

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

#to_sString

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

Returns:

  • (String)

    the string representation of the currently selected state



115
116
117
# File 'lib/ollama_chat/state_selectors.rb', line 115

def to_s
  selected.to_s
end