ifilter 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ifilter.rb +2 -2
- data/lib/ifilter/interface.rb +77 -42
- data/lib/ifilter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9509ccf24f4affbe68d0e5a0999fa72a0439feb9
|
4
|
+
data.tar.gz: 9a69c46beb85427d81a71122af6e80f0c28a091f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3141cf7cc4070d2b09c73a397d4ccd4db6afe33393fd78170df07aed0fb835dc11fd440207f01861a032d6241998fb9c08753622dc5287e27465908fb58c8a8d
|
7
|
+
data.tar.gz: 6bcc424ea886a0aa1294d987260774454f0b9684d83c3f6eb097760d141a1656565ca813ab85bb60fa9b40b3080b1bdfbfafff9d3f499d66cba9080dae92efd2
|
data/lib/ifilter.rb
CHANGED
data/lib/ifilter/interface.rb
CHANGED
@@ -2,13 +2,14 @@ require 'curses'
|
|
2
2
|
|
3
3
|
module Ifilter
|
4
4
|
class Interface
|
5
|
-
def initialize(name
|
5
|
+
def initialize(name)
|
6
6
|
@prompt = "#{name}> "
|
7
|
-
@
|
7
|
+
@target = []
|
8
8
|
|
9
9
|
@input = Input.new
|
10
10
|
@outputs = []
|
11
11
|
@cursor_position = 1
|
12
|
+
@page_number = 1
|
12
13
|
|
13
14
|
Curses.init_screen
|
14
15
|
Curses.crmode
|
@@ -17,10 +18,10 @@ module Ifilter
|
|
17
18
|
@window.keypad(true)
|
18
19
|
end
|
19
20
|
|
20
|
-
def grep
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
def grep(array)
|
22
|
+
@target = array
|
23
|
+
|
24
|
+
update_window
|
24
25
|
|
25
26
|
loop do
|
26
27
|
key = @window.getch
|
@@ -29,24 +30,18 @@ module Ifilter
|
|
29
30
|
if key.class == String
|
30
31
|
@input << key
|
31
32
|
|
32
|
-
# <BS>
|
33
|
-
elsif key == 127
|
33
|
+
elsif key == 127 # <BS>
|
34
34
|
@input.backspace!
|
35
35
|
|
36
|
-
# <CR>
|
37
|
-
|
38
|
-
if @outputs.size >= @cursor_position && @cursor_position >= 1
|
39
|
-
break
|
40
|
-
else
|
41
|
-
next
|
42
|
-
end
|
36
|
+
elsif keycode == :KEY_CTRL_J # <CR>
|
37
|
+
break if @outputs.size >= @cursor_position
|
43
38
|
|
44
39
|
elsif keycode == :KEY_DOWN
|
45
|
-
down_cursor_position
|
40
|
+
down_cursor_position unless @outputs.empty?
|
46
41
|
next
|
47
42
|
|
48
43
|
elsif keycode == :KEY_UP
|
49
|
-
up_cursor_position
|
44
|
+
up_cursor_position unless @outputs.empty?
|
50
45
|
next
|
51
46
|
|
52
47
|
# Ignore other keys
|
@@ -54,56 +49,64 @@ module Ifilter
|
|
54
49
|
next
|
55
50
|
end
|
56
51
|
|
57
|
-
|
58
|
-
@outputs = []
|
59
|
-
@window.clear
|
60
|
-
reset_cursor_position
|
61
|
-
|
62
|
-
output_line(@prompt + @input.to_s)
|
63
|
-
output_choises
|
64
|
-
reset_cursor_position
|
52
|
+
update_window
|
65
53
|
end
|
66
54
|
|
67
55
|
Curses.close_screen
|
68
56
|
@outputs[@cursor_position - 1]
|
69
57
|
|
70
|
-
# <C-c>
|
71
|
-
rescue Interrupt
|
58
|
+
rescue Interrupt # <C-c>
|
72
59
|
Curses.close_screen
|
73
60
|
end
|
74
61
|
|
75
62
|
private
|
76
63
|
|
64
|
+
def update_window
|
65
|
+
# Initialize a window
|
66
|
+
@window.clear
|
67
|
+
reset_cursor_position
|
68
|
+
|
69
|
+
start_index = (@window.maxy - 1) * (@page_number - 1)
|
70
|
+
# Update @outputs
|
71
|
+
@outputs = filtering_by_input(@target, start_index: start_index)
|
72
|
+
|
73
|
+
# Redraw the output
|
74
|
+
output_line(@prompt + @input.to_s)
|
75
|
+
@outputs.each do |output|
|
76
|
+
output_line(output)
|
77
|
+
end
|
78
|
+
reset_cursor_position
|
79
|
+
end
|
80
|
+
|
77
81
|
def get_keycode(key)
|
78
82
|
Curses.constants.select do |name|
|
79
83
|
/^KEY/ =~ name && Curses.const_get(name) == key
|
80
84
|
end.first
|
81
85
|
end
|
82
86
|
|
83
|
-
def
|
84
|
-
|
85
|
-
|
87
|
+
def filtering_by_input(array, start_index: 0)
|
88
|
+
array.each_with_object([]).with_index do |(str, outputs), index|
|
89
|
+
next if index < start_index
|
86
90
|
regex = @input.to_regex
|
87
91
|
|
88
|
-
if !regex.nil? && regex =~
|
89
|
-
|
92
|
+
if !regex.nil? && regex =~ str
|
93
|
+
outputs << str
|
90
94
|
end
|
91
95
|
end
|
92
|
-
|
93
|
-
@outputs.each { |output| output_line(output) }
|
94
96
|
end
|
95
97
|
|
96
98
|
def output_line(word)
|
97
|
-
@window.
|
98
|
-
|
99
|
+
unless @cursor_position - 1 > @window.maxy - 1
|
100
|
+
@window.setpos(@cursor_position - 1, 0)
|
101
|
+
@cursor_position += 1
|
99
102
|
|
100
|
-
|
103
|
+
@window << word
|
104
|
+
end
|
101
105
|
end
|
102
106
|
|
103
107
|
def up_cursor_position
|
104
108
|
if @cursor_position == 1
|
105
|
-
|
106
|
-
@cursor_position = @outputs.size
|
109
|
+
scroll_prev_page
|
107
110
|
else
|
108
111
|
@window.setpos(@cursor_position - 1, 0)
|
109
112
|
@cursor_position -= 1
|
@@ -111,9 +114,8 @@ module Ifilter
|
|
111
114
|
end
|
112
115
|
|
113
116
|
def down_cursor_position
|
114
|
-
if @cursor_position == @outputs.size
|
115
|
-
|
116
|
-
@cursor_position = 1
|
117
|
+
if @cursor_position == @window.maxy - 1 || @cursor_position == @outputs.size
|
118
|
+
scroll_next_page
|
117
119
|
else
|
118
120
|
@window.setpos(@cursor_position + 1, 0)
|
119
121
|
@cursor_position += 1
|
@@ -124,5 +126,38 @@ module Ifilter
|
|
124
126
|
@window.setpos(1, 0)
|
125
127
|
@cursor_position = 1
|
126
128
|
end
|
129
|
+
|
130
|
+
def scroll_prev_page
|
131
|
+
if @page_number == 1
|
132
|
+
@page_number = max_page_number
|
133
|
+
update_window
|
134
|
+
|
135
|
+
@window.setpos(last_output_position, 0)
|
136
|
+
@cursor_position = last_output_position
|
137
|
+
else
|
138
|
+
@page_number -= 1
|
139
|
+
update_window
|
140
|
+
|
141
|
+
@window.setpos(@window.maxy - 1, 0)
|
142
|
+
@cursor_position = @window.maxy - 1
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def scroll_next_page
|
147
|
+
if @page_number == max_page_number
|
148
|
+
@page_number = 1
|
149
|
+
else
|
150
|
+
@page_number += 1
|
151
|
+
end
|
152
|
+
update_window
|
153
|
+
end
|
154
|
+
|
155
|
+
def max_page_number
|
156
|
+
(filtering_by_input(@target, start_index: 0).size.to_f / (@window.maxy - 1).to_f).ceil
|
157
|
+
end
|
158
|
+
|
159
|
+
def last_output_position
|
160
|
+
filtering_by_input(@target, start_index: 0).size % (@window.maxy - 1)
|
161
|
+
end
|
127
162
|
end
|
128
163
|
end
|
data/lib/ifilter/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ifilter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kaihar4
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: curses
|