Class: Doublespeak::Core
- Inherits:
-
Object
- Object
- Doublespeak::Core
- Defined in:
- lib/doublespeak/core.rb
Instance Attribute Summary collapse
-
#data_source ⇒ Object
readonly
Returns the value of attribute data_source.
-
#display ⇒ Object
readonly
Returns the value of attribute display.
-
#format_data ⇒ Object
readonly
Returns the value of attribute format_data.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#saved_candidates ⇒ Object
readonly
Returns the value of attribute saved_candidates.
-
#selected_index ⇒ Object
readonly
Returns the value of attribute selected_index.
Instance Method Summary collapse
- #back_up ⇒ Object
- #entry(c) ⇒ Object
- #find_candidates ⇒ Object
- #finish_up ⇒ Object
- #increment_selection(i) ⇒ Object
-
#initialize(data_source, options = {}) ⇒ Core
constructor
A new instance of Core.
- #render ⇒ Object
Constructor Details
#initialize(data_source, options = {}) ⇒ Core
Returns a new instance of Core.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/doublespeak/core.rb', line 6 def initialize(data_source, = {}) @data_source = data_source @format_data = [:format_data] || ->(x) { "#{x}" } @display = [:display] || Display.new() @query = [:query] || "" @selected_index = [:selected_index] || 0 @saved_candidates = [:saved_candidates] || [] end |
Instance Attribute Details
#data_source ⇒ Object (readonly)
Returns the value of attribute data_source.
3 4 5 |
# File 'lib/doublespeak/core.rb', line 3 def data_source @data_source end |
#display ⇒ Object (readonly)
Returns the value of attribute display.
3 4 5 |
# File 'lib/doublespeak/core.rb', line 3 def display @display end |
#format_data ⇒ Object (readonly)
Returns the value of attribute format_data.
3 4 5 |
# File 'lib/doublespeak/core.rb', line 3 def format_data @format_data end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
4 5 6 |
# File 'lib/doublespeak/core.rb', line 4 def query @query end |
#saved_candidates ⇒ Object (readonly)
Returns the value of attribute saved_candidates.
4 5 6 |
# File 'lib/doublespeak/core.rb', line 4 def saved_candidates @saved_candidates end |
#selected_index ⇒ Object (readonly)
Returns the value of attribute selected_index.
4 5 6 |
# File 'lib/doublespeak/core.rb', line 4 def selected_index @selected_index end |
Instance Method Details
#back_up ⇒ Object
46 47 48 |
# File 'lib/doublespeak/core.rb', line 46 def back_up query.chop! end |
#entry(c) ⇒ Object
55 56 57 58 59 |
# File 'lib/doublespeak/core.rb', line 55 def entry(c) if query.length < display.width query.concat(c) end end |
#find_candidates ⇒ Object
50 51 52 53 |
# File 'lib/doublespeak/core.rb', line 50 def find_candidates @selected_index = 0 @saved_candidates = query.strip.to_s.empty? ? [] : data_source.call(query) end |
#finish_up ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/doublespeak/core.rb', line 26 def finish_up display.move_to_origin display.clear_to_end_of_line display.set_cursor_visible display.write display.format_selected_result.call(format_data.call(candidate)) unless candidate.nil? display.write "\n" end |
#increment_selection(i) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/doublespeak/core.rb', line 35 def increment_selection(i) @selected_index = selected_index + i if saved_candidates.size == 0 @selected_index = 0 elsif selected_index < 0 @selected_index = saved_candidates.size - 1 elsif selected_index >= saved_candidates.size @selected_index = 0 end end |
#render ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/doublespeak/core.rb', line 17 def render display.move_to_origin display.clear_to_end_of_line display.write (query + status_line) display.move_to_origin end |