Class: Twterm::SearchQueryWindow

Inherits:
Object
  • Object
show all
Includes:
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.



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

def initialize
  @window = Curses.stdscr.subwin(1, Curses.stdscr.maxx, Curses.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.



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

def last_query
  @last_query
end

Instance Method Details

#clearObject



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

def clear
  window.clear
  window.refresh
end

#inputObject



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

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



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

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

#searching_backward!Object



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

def searching_backward!
  @searching_forward = false
end

#searching_backward?Boolean

Returns:

  • (Boolean)


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

def searching_backward?
  !@searching_forward
end

#searching_down!Object



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

def searching_down!
  @searching_down = true
end

#searching_down?Boolean

Returns:

  • (Boolean)


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

def searching_down?
  @searching_down
end

#searching_forward!Object



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

def searching_forward!
  @searching_forward = true
end

#searching_forward?Boolean

Returns:

  • (Boolean)


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

def searching_forward?
  @searching_forward
end

#searching_up!Object



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

def searching_up!
  @searching_down = false
end

#searching_up?Boolean

Returns:

  • (Boolean)


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

def searching_up?
  !@searching_down
end