Class: Rfd::FilterInput
- Inherits:
-
Object
- Object
- Rfd::FilterInput
- Defined in:
- lib/rfd/filter_input.rb
Instance Attribute Summary collapse
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #append(char) ⇒ Object
- #backspace ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
- #fuzzy_match?(text, pattern = @text) ⇒ Boolean
-
#handle_input(c) ⇒ Object
Handle input, return true if handled.
-
#initialize(&on_change) ⇒ FilterInput
constructor
A new instance of FilterInput.
- #render(window, row, max_width) ⇒ Object
Constructor Details
#initialize(&on_change) ⇒ FilterInput
Returns a new instance of FilterInput.
7 8 9 10 |
# File 'lib/rfd/filter_input.rb', line 7 def initialize(&on_change) @text = '' @on_change = on_change end |
Instance Attribute Details
#text ⇒ Object
Returns the value of attribute text.
5 6 7 |
# File 'lib/rfd/filter_input.rb', line 5 def text @text end |
Instance Method Details
#append(char) ⇒ Object
24 25 26 27 |
# File 'lib/rfd/filter_input.rb', line 24 def append(char) @text += char @on_change&.call end |
#backspace ⇒ Object
18 19 20 21 22 |
# File 'lib/rfd/filter_input.rb', line 18 def backspace return if @text.empty? @text = @text[0..-2] @on_change&.call end |
#clear ⇒ Object
12 13 14 15 16 |
# File 'lib/rfd/filter_input.rb', line 12 def clear return if @text.empty? @text = '' @on_change&.call end |
#empty? ⇒ Boolean
29 30 31 |
# File 'lib/rfd/filter_input.rb', line 29 def empty? @text.empty? end |
#fuzzy_match?(text, pattern = @text) ⇒ Boolean
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rfd/filter_input.rb', line 65 def fuzzy_match?(text, pattern = @text) return true if pattern.empty? pattern_chars = pattern.downcase.chars text_lower = text.downcase pattern_index = 0 text_lower.each_char do |char| if char == pattern_chars[pattern_index] pattern_index += 1 return true if pattern_index >= pattern_chars.length end end false end |
#handle_input(c) ⇒ Object
Handle input, return true if handled
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rfd/filter_input.rb', line 34 def handle_input(c) case c when 8, 127, Curses::KEY_BACKSPACE, Curses::KEY_DC # Backspace/Delete backspace true when 21 # Ctrl-U - clear clear true when String append(c) true when Integer if c >= 32 && c <= 126 # Printable ASCII append(c.chr) true else false end else false end end |
#render(window, row, max_width) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rfd/filter_input.rb', line 57 def render(window, row, max_width) window.setpos(row, 1) window.attron(Curses::A_BOLD) do prompt = "> #{@text}_" window.addstr(prompt[0, max_width].ljust(max_width)) end end |