Class: Ifilter::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/ifilter/interface.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, array) ⇒ Interface

Returns a new instance of Interface.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ifilter/interface.rb', line 5

def initialize(name, array)
  @prompt = "#{name}> "
  @targets = array

  @input = Input.new
  @outputs = []
  @cursor_position = 1

  Curses.init_screen
  Curses.crmode
  Curses.noecho
  @window = Curses::Window.new(Curses.lines, Curses.cols, 0, 0)
  @window.keypad(true)
end

Instance Method Details

#grepObject



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
70
71
72
73
# File 'lib/ifilter/interface.rb', line 20

def grep
  output_line(@prompt)
  output_choises
  reset_cursor_position

  loop do
    key = @window.getch
    keycode = get_keycode(key)

    if key.class == String
      @input << key

    # <BS>
    elsif key == 127
      @input.backspace!

    # <CR>
    elsif keycode == :KEY_CTRL_J
      if @outputs.size >= @cursor_position && @cursor_position >= 1
        break
      else
        next
      end

    elsif keycode == :KEY_DOWN
      down_cursor_position
      next

    elsif keycode == :KEY_UP
      up_cursor_position
      next

    # Ignore other keys
    else
      next
    end

    # Initialize a window
    @outputs = []
    @window.clear
    reset_cursor_position

    output_line(@prompt + @input.to_s)
    output_choises
    reset_cursor_position
  end

  Curses.close_screen
  @outputs[@cursor_position - 1]

# <C-c>
rescue Interrupt
  Curses.close_screen
end