Class: Twterm::FilterQueryWindow

Inherits:
Object
  • Object
show all
Includes:
Curses, Singleton
Defined in:
lib/twterm/filter_query_window.rb

Instance Method Summary collapse

Constructor Details

#initializeFilterQueryWindow

Returns a new instance of FilterQueryWindow.



6
7
8
# File 'lib/twterm/filter_query_window.rb', line 6

def initialize
  @window = stdscr.subwin(1, stdscr.maxx, stdscr.maxy - 1, 0)
end

Instance Method Details

#clearObject



71
72
73
74
# File 'lib/twterm/filter_query_window.rb', line 71

def clear
  stdscr.setpos(stdscr.maxy - 1, 0)
  stdscr.addstr(' ' * window.maxx)
end

#inputObject



10
11
12
13
14
15
16
17
18
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
69
# File 'lib/twterm/filter_query_window.rb', line 10

def input
  clear
  stdscr.setpos(stdscr.maxy - 1, 0)
  stdscr.addch '/'

  Curses.timeout = 10
  raw

  chars = []
  str = ''

  loop do
    char = getch

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

        str.chop!
        chars = []
        clear
        stdscr.setpos(stdscr.maxy - 1, 0)
        stdscr.addstr("/#{str}")
      when 0..31
        # ignore control codes (\x00 - \x1f)
      else
        str << chars
          .map { |x| x.is_a?(String) ? x.ord : x }
          .pack('c*')
          .force_encoding('utf-8')
        chars = []
      end
    else
      chars << char
    end

    stdscr.setpos(stdscr.maxy - 1, 1)
    stdscr.addstr(str)
  end

  Curses.timeout = 0
  cbreak

  str
end

#resizeObject



76
77
78
79
# File 'lib/twterm/filter_query_window.rb', line 76

def resize
  @window.resize(1, stdscr.maxx)
  @window.move(stdscr.maxy - 1, 0)
end