Class: UI::ListSearch
- Inherits:
-
Object
- Object
- UI::ListSearch
- Defined in:
- lib/ektoplayer/ui/widgets/listwidget.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
Returns the value of attribute direction.
Instance Method Summary collapse
-
#initialize(search: '', direction: :down) ⇒ ListSearch
constructor
A new instance of ListSearch.
- #next(*a) ⇒ Object
- #prev(*a) ⇒ Object
- #search=(search) ⇒ Object
- #search_down(current_pos, source) ⇒ Object
- #search_up(current_pos, source) ⇒ Object
Constructor Details
#initialize(search: '', direction: :down) ⇒ ListSearch
Returns a new instance of ListSearch.
41 42 43 |
# File 'lib/ektoplayer/ui/widgets/listwidget.rb', line 41 def initialize(search: '', direction: :down) @search, @direction = search, direction end |
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
39 40 41 |
# File 'lib/ektoplayer/ui/widgets/listwidget.rb', line 39 def direction @direction end |
Instance Method Details
#next(*a) ⇒ Object
61 |
# File 'lib/ektoplayer/ui/widgets/listwidget.rb', line 61 def next(*a) @direction == :up ? search_up(*a) : search_down(*a) end |
#prev(*a) ⇒ Object
62 |
# File 'lib/ektoplayer/ui/widgets/listwidget.rb', line 62 def prev(*a) @direction == :up ? search_down(*a) : search_up(*a) end |
#search=(search) ⇒ Object
45 46 47 |
# File 'lib/ektoplayer/ui/widgets/listwidget.rb', line 45 def search=(search) @search = Regexp.new(search.downcase) rescue search.downcase end |
#search_down(current_pos, source) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ektoplayer/ui/widgets/listwidget.rb', line 80 def search_down(current_pos, source) start_pos = (current_pos + 1).clamp(0, source.size) # search down from current pos start_pos.upto(source.size).each do |i| return i if comp(source[i]) end # restart search from top to start_pos 0.upto(start_pos).each do |i| return i if comp(source[i]) end nil # not found end |
#search_up(current_pos, source) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ektoplayer/ui/widgets/listwidget.rb', line 64 def search_up(current_pos, source) start_pos = (current_pos - 1).clamp(0, source.size) # search up from current pos to 0 start_pos.downto(0).each do |i| return i if comp(source[i]) end # restart search from bottom to start_pos (source.size - 1).downto(start_pos).each do |i| return i if comp(source[i]) end nil # not found end |