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
-
#allow_empty ⇒ TrueClass, FalseClass
readonly
The allow_empty reader returns whether the selector is allowed to be empty.
-
#default ⇒ String?
readonly
The default reader returns the default state for this selector.
-
#name ⇒ String
readonly
The name reader returns the name of the state selector.
-
#off ⇒ Array<String>
readonly
The off reader returns the list of states that are considered “off”.
-
#states ⇒ Set<String>
readonly
The states reader returns the set of valid states for this selector.
Attributes included from Utils::Chooser
#current_search_state Stores the
Instance Method Summary collapse
-
#allow_empty? ⇒ TrueClass, FalseClass
The allow_empty? method checks if the switch is allowed to be empty.
-
#choose ⇒ nil
The choose method presents an interactive menu to select from available states.
-
#off? ⇒ TrueClass, FalseClass
The off? method checks if the current state is in the off set.
-
#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.
-
#show(output: STDOUT) ⇒ Object
The show method outputs the current value of the state selector.
-
#to_s ⇒ String
The to_s method returns the string representation of the selected state.
Methods included from Utils::Chooser
#choose_entry, #choose_with_state
Instance Attribute Details
#allow_empty ⇒ TrueClass, FalseClass (readonly)
The allow_empty reader returns whether the selector is allowed to be empty.
42 43 44 |
# File 'lib/ollama_chat/state_selectors.rb', line 42 def allow_empty @allow_empty end |
#default ⇒ String? (readonly)
The default reader returns the default state for this selector.
32 33 34 |
# File 'lib/ollama_chat/state_selectors.rb', line 32 def default @default end |
#name ⇒ String (readonly)
The name reader returns the name of the state selector.
22 23 24 |
# File 'lib/ollama_chat/state_selectors.rb', line 22 def name @name end |
#off ⇒ Array<String> (readonly)
The off reader returns the list of states that are considered “off”.
37 38 39 |
# File 'lib/ollama_chat/state_selectors.rb', line 37 def off @off end |
#states ⇒ Set<String> (readonly)
The states reader returns the set of valid states for this selector.
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.
48 49 50 |
# File 'lib/ollama_chat/state_selectors.rb', line 48 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.
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.
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.
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.
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_s ⇒ String
The to_s method returns the string representation of the selected state.
115 116 117 |
# File 'lib/ollama_chat/state_selectors.rb', line 115 def to_s selected.to_s end |