ifilter 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87bc44ff9b07d7dced65ad01dc151214cf4753ff
4
- data.tar.gz: e63a537acc96986989e621d51c94b06da85168af
3
+ metadata.gz: 75be2e647b9ebaf3c9652b9891e1ad4697c0df59
4
+ data.tar.gz: 5358032cc2dd7af4e0e80488699e75d8780f80e6
5
5
  SHA512:
6
- metadata.gz: 598ffac359a6728b9189d1e4c065c1ecdb62f5a4db4d9ed7c33f901b967d2817d07c42c9282f9c8440b9d058776a3c4c579600ae2622b2566885069125445e62
7
- data.tar.gz: 682d6f1ccde77971b4166ff4c2687243da80266eb7cb236903d3195fb591f347f93438634a784fb3c8eeba742d56479744e82103ac198e5b9451b112bf955b71
6
+ metadata.gz: f910a4502e01599dd89757f3d6812ed0f966beabdcbc972c188bb475c086d025fda143218b6f4817969fa399be7fa99ae3e5a2d186cf416884be444d5418d06d
7
+ data.tar.gz: c520dc8568894a315cfac375978c4d54de9f01606988e9ab04d1fe5bd014d8b4fca0154bcd9f5ab54aa791e8c9cb6a6ca06c7478e4b793fbb1f683166c7f8b8f
@@ -25,9 +25,13 @@ module Ifilter
25
25
 
26
26
  if key.class == String
27
27
  @input << key
28
+ @filtered_result = nil
29
+ @page_number = 1
28
30
 
29
31
  elsif key == 127 # <BS>
30
32
  @input.backspace!
33
+ @filtered_result = nil
34
+ @page_number = 1
31
35
 
32
36
  elsif keycode == :KEY_CTRL_J # <CR>
33
37
  break if @outputs.size >= @cursor_position
@@ -40,8 +44,7 @@ module Ifilter
40
44
  up_cursor_position unless @outputs.empty?
41
45
  next
42
46
 
43
- # Ignore other keys
44
- else
47
+ else # Ignore other keys
45
48
  next
46
49
  end
47
50
 
@@ -64,12 +67,13 @@ module Ifilter
64
67
  # A flag of the output for paging
65
68
  start_index = (@window.maxy - 1) * (@page_number - 1)
66
69
  # Update @outputs
67
- @outputs = filtering_by_input(@target, start_index: start_index)
70
+ @outputs = filtered_result[start_index, filtered_result.size]
68
71
 
69
72
  # Redraw the output
70
- output_prompt
71
- output_current_page
73
+ output_header
72
74
  @outputs.each do |output|
75
+ break if @cursor_position > @window.maxy - 1
76
+
73
77
  @window.setpos(@cursor_position, 0)
74
78
  output_line(output)
75
79
  @cursor_position += 1
@@ -87,23 +91,7 @@ module Ifilter
87
91
  }.first
88
92
  end
89
93
 
90
- def filtering_by_input(array, start_index: 0)
91
- array.each_with_object([]).with_index do |(str, outputs), index|
92
- next if index < start_index
93
-
94
- regex = @input.to_regexp
95
-
96
- if !regex.nil? && regex =~ str
97
- outputs << str
98
- end
99
- end
100
- end
101
-
102
94
  def output_line(word)
103
- if @cursor_position > @window.maxy - 1
104
- return
105
- end
106
-
107
95
  if !@input.size.zero? && match_pos = word =~ @input.to_regexp
108
96
  @window << word[0, match_pos]
109
97
 
@@ -111,20 +99,18 @@ module Ifilter
111
99
  @window << word[match_pos, @input.size]
112
100
  @window.attrunset(Curses.color_pair(3))
113
101
 
114
- @window << word[match_pos + @input.size, word.size - match_pos + @input.size]
102
+ @window << word[match_pos + @input.size, (@window.maxx - 1) - (match_pos + @input.size)]
115
103
  @window.attrset(Curses.color_pair(1))
116
104
  else
117
- @window << word
105
+ @window << word[0, @window.maxx - 1]
118
106
  end
119
107
  end
120
108
 
121
- def output_prompt
109
+ def output_header
122
110
  @window.setpos(0, 0)
123
111
 
124
112
  @window << @prompt + @input.to_s
125
- end
126
113
 
127
- def output_current_page
128
114
  @window.setpos(0, @window.maxx)
129
115
 
130
116
  word = "[#{@page_number}/#{max_page_number.zero? ? 1 : max_page_number}]"
@@ -155,7 +141,7 @@ module Ifilter
155
141
  def scroll_prev_page
156
142
  if @page_number == 1
157
143
  @page_number = max_page_number
158
- update_window(cursor_position: last_output_position)
144
+ update_window(cursor_position: filtered_result.size % (@window.maxy - 1))
159
145
  else
160
146
  @page_number -= 1
161
147
  update_window(cursor_position: @window.maxy - 1)
@@ -174,16 +160,16 @@ module Ifilter
174
160
 
175
161
  def highlight_cursor_position
176
162
  @window.attrset(Curses.color_pair(2))
177
- output_line(@outputs[@cursor_position - 1].to_s.slice(0, @window.maxx - 1))
163
+ output_line(@outputs[@cursor_position - 1].to_s)
178
164
  @window.attrset(Curses.color_pair(1))
179
165
  end
180
166
 
181
167
  def max_page_number
182
- (filtering_by_input(@target).size.to_f / (@window.maxy - 1).to_f).ceil
168
+ (filtered_result.size.to_f / (@window.maxy - 1).to_f).ceil
183
169
  end
184
170
 
185
- def last_output_position
186
- filtering_by_input(@target).size % (@window.maxy - 1)
171
+ def filtered_result
172
+ @filtered_result ||= @input.filter(@target)
187
173
  end
188
174
  end
189
175
  end
data/lib/ifilter/input.rb CHANGED
@@ -27,5 +27,15 @@ module Ifilter
27
27
  rescue
28
28
  nil
29
29
  end
30
+
31
+ def filter(array)
32
+ array.each_with_object([]) do |str, outputs|
33
+ regexp = self.to_regexp
34
+
35
+ if !regexp.nil? && regexp =~ str
36
+ outputs << str
37
+ end
38
+ end
39
+ end
30
40
  end
31
41
  end
@@ -1,3 +1,3 @@
1
1
  module Ifilter
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaihar4
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-12 00:00:00.000000000 Z
11
+ date: 2015-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses