Class: Twterm::SearchQueryWindow

Inherits:
Object
  • Object
show all
Includes:
Subscriber
Defined in:
lib/twterm/search_query_window.rb

Defined Under Namespace

Classes: CancelInput

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Subscriber

included, #subscribe, #unsubscribe

Constructor Details

#initialize(window) ⇒ SearchQueryWindow

Returns a new instance of SearchQueryWindow.

Parameters:



12
13
14
15
16
17
# File 'lib/twterm/search_query_window.rb', line 12

def initialize(window)
  @window = window
  @searching_down = true
  @str = ''
  @last_query = ''
end

Instance Attribute Details

#last_queryObject (readonly)

Returns the value of attribute last_query.



9
10
11
# File 'lib/twterm/search_query_window.rb', line 9

def last_query
  @last_query
end

Instance Method Details

#clearObject



70
71
72
73
# File 'lib/twterm/search_query_window.rb', line 70

def clear
  window.clear
  window.refresh
end

#inputObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/twterm/search_query_window.rb', line 19

def input
  @str = ''
  render_current_string

  Curses.timeout = 10

  chars = []

  loop do
    char = Curses.getch

    if char.nil?
      case chars.first
      when 3, 27 # cancel with <C-c> / Esc
        raise CancelInput
      when 4 # cancel with <C-d> when query is empty
        raise CancelInput if @str.empty?
      when 10 # submit with <C-j>
        @str = last_query.to_s if @str.empty?
        break
      when 127 # backspace
        raise CancelInput if @str.empty?

        @str.chop!
        render_current_string
      when 0..31 # rubocop:disable Lint/EmptyWhen
        # ignore control codes (\x00 - \x1f)
      else
        next if chars.empty?
        @str << chars
          .map { |x| x.is_a?(String) ? x.ord : x }
          .pack('c*')
          .force_encoding('utf-8')
        render_current_string
      end

      chars = []
    else
      chars << char
    end
  end

  @last_query = @str unless @str.empty?
  last_query
rescue CancelInput
  @str = ''
  clear
ensure
  Curses.timeout = -1
end

#render_last_queryObject



75
76
77
# File 'lib/twterm/search_query_window.rb', line 75

def render_last_query
  render(last_query) unless last_query.empty?
end

#searching_backward!Object



79
80
81
# File 'lib/twterm/search_query_window.rb', line 79

def searching_backward!
  @searching_forward = false
end

#searching_backward?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/twterm/search_query_window.rb', line 83

def searching_backward?
  !@searching_forward
end

#searching_down!Object



87
88
89
# File 'lib/twterm/search_query_window.rb', line 87

def searching_down!
  @searching_down = true
end

#searching_down?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/twterm/search_query_window.rb', line 91

def searching_down?
  @searching_down
end

#searching_forward!Object



95
96
97
# File 'lib/twterm/search_query_window.rb', line 95

def searching_forward!
  @searching_forward = true
end

#searching_forward?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/twterm/search_query_window.rb', line 99

def searching_forward?
  @searching_forward
end

#searching_up!Object



103
104
105
# File 'lib/twterm/search_query_window.rb', line 103

def searching_up!
  @searching_down = false
end

#searching_up?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/twterm/search_query_window.rb', line 107

def searching_up?
  !@searching_down
end