Class: SearchUI::Search
- Inherits:
-
Object
- Object
- SearchUI::Search
- Extended by:
- Term::ANSIColor
- Includes:
- Term::ANSIColor
- Defined in:
- lib/search_ui/search.rb
Overview
SearchUI::Search manages interactive console-based searching through an array of objects
This class provides an interactive search interface that allows users to filter and select objects from a collection based on text input patterns. It handles terminal input processing, display updates, and user navigation through the search results.
Defined Under Namespace
Classes: State
Instance Attribute Summary collapse
-
#state ⇒ SearchUI::Search::State
readonly
Reads the current internal state of the search interface, containing the current input answer and the active selection index.
Instance Method Summary collapse
-
#initialize(match:, query:, found:, output: STDOUT, prompt: 'Search? %s', state: nil) ⇒ Search
constructor
Initializes a new SearchUI::Search instance to manage an interactive console search interface.
-
#start ⇒ Object?
Starts the interactive search interface and handles user input until a selection is made or the process is cancelled.
Constructor Details
#initialize(match:, query:, found:, output: STDOUT, prompt: 'Search? %s', state: nil) ⇒ Search
Initializes a new SearchUI::Search instance to manage an interactive console search interface. This method sets up the filtering logic, display formatting, and selection criteria required to drive the search loop, as well as the output stream and prompt.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/search_ui/search.rb', line 46 def initialize( match:, query:, found:, output: STDOUT, prompt: 'Search? %s', state: nil ) @match = match @query = query @found = found @output = output @prompt = prompt @state = state || State.new('', 0) end |
Instance Attribute Details
#state ⇒ SearchUI::Search::State (readonly)
Reads the current internal state of the search interface, containing the current input answer and the active selection index.
67 68 69 |
# File 'lib/search_ui/search.rb', line 67 def state @state end |
Instance Method Details
#start ⇒ Object?
Starts the interactive search interface and handles user input until a selection is made or the process is cancelled.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/search_ui/search.rb', line 74 def start @output.print reset @matches = @match.(@state.answer) @state.selector = @state.selector.clamp(0, [ @matches.size - 1, 0 ].max) result = @query.(@state.answer, @matches, @state.selector) loop do @output.print clear_screen @output.print move_home { @prompt % @state.answer + ?\n + result } case getc when true @output.print clear_screen, move_home, reset if result = @found.(@state.answer, @matches, @state.selector) return result else return nil end when false return nil end @matches = @match.(@state.answer) @state.selector = @state.selector.clamp(0, [ @matches.size - 1, 0 ].max) result = @query.(@state.answer, @matches, @state.selector) end end |