Class: Twterm::SearchQueryWindow

Inherits:
Object
  • Object
show all
Includes:
Curses, Singleton, 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

#initializeSearchQueryWindow

Returns a new instance of SearchQueryWindow.



14
15
16
17
18
19
20
21
# File 'lib/twterm/search_query_window.rb', line 14

def initialize
  @window = stdscr.subwin(1, stdscr.maxx, stdscr.maxy - 1, 0)
  @searching_down = true
  @str = ''
  @last_query = ''

  subscribe(Event::Screen::Resize, :resize)
end

Instance Attribute Details

#last_queryObject (readonly)

Returns the value of attribute last_query.



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

def last_query
  @last_query
end

Instance Method Details

#clearObject



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

def clear
  window.clear
  window.refresh
end

#inputObject



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
69
70
71
72
# File 'lib/twterm/search_query_window.rb', line 23

def input
  @str = ''
  render_current_string

  Curses.timeout = 10

  chars = []

  loop do
    char = 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



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

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

#searching_backward!Object



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

def searching_backward!
  @searching_forward = false
end

#searching_backward?Boolean

Returns:

  • (Boolean)


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

def searching_backward?
  !@searching_forward
end

#searching_down!Object



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

def searching_down!
  @searching_down = true
end

#searching_down?Boolean

Returns:

  • (Boolean)


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

def searching_down?
  @searching_down
end

#searching_forward!Object



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

def searching_forward!
  @searching_forward = true
end

#searching_forward?Boolean

Returns:

  • (Boolean)


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

def searching_forward?
  @searching_forward
end

#searching_up!Object



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

def searching_up!
  @searching_down = false
end

#searching_up?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/twterm/search_query_window.rb', line 111

def searching_up?
  !@searching_down
end