ifilter 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7e4cde2b995806bf2a780146335ad8e94877b4e
4
- data.tar.gz: e7879569f0c8d8dca9f2b2244c88c7d929691586
3
+ metadata.gz: 9509ccf24f4affbe68d0e5a0999fa72a0439feb9
4
+ data.tar.gz: 9a69c46beb85427d81a71122af6e80f0c28a091f
5
5
  SHA512:
6
- metadata.gz: 94e89319444b5ca2c17ea5914e68b96b45facd753400007140b84454cb3a63f5104156df073fcae5a56a9af2441d336f4c3aa7b1018853f4c22d4d5c3192ce94
7
- data.tar.gz: 94b52882ecccee76ee079fab228734e917a305d1da31954da8e514b282282f346e947b763b488e42c9ce77b9d5dbfec9a67c5e885f316679c41f5e7a52065655
6
+ metadata.gz: 3141cf7cc4070d2b09c73a397d4ccd4db6afe33393fd78170df07aed0fb835dc11fd440207f01861a032d6241998fb9c08753622dc5287e27465908fb58c8a8d
7
+ data.tar.gz: 6bcc424ea886a0aa1294d987260774454f0b9684d83c3f6eb097760d141a1656565ca813ab85bb60fa9b40b3080b1bdfbfafff9d3f499d66cba9080dae92efd2
@@ -12,8 +12,8 @@ module Ifilter
12
12
  STDOUT.reopen('/dev/tty')
13
13
  end
14
14
 
15
- ifilter = Interface.new(name, array)
16
- result = ifilter.grep
15
+ ifilter = Interface.new(name)
16
+ result = ifilter.grep(array)
17
17
 
18
18
  unless isatty
19
19
  STDOUT.flush
@@ -2,13 +2,14 @@ require 'curses'
2
2
 
3
3
  module Ifilter
4
4
  class Interface
5
- def initialize(name, array)
5
+ def initialize(name)
6
6
  @prompt = "#{name}> "
7
- @targets = array
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
- output_line(@prompt)
22
- output_choises
23
- reset_cursor_position
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
- elsif keycode == :KEY_CTRL_J
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
- # Initialize a window
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 output_choises
84
- # Filtering
85
- @targets.each do |target|
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 =~ target
89
- @outputs << target
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.setpos(@cursor_position - 1, 0)
98
- @cursor_position += 1
99
+ unless @cursor_position - 1 > @window.maxy - 1
100
+ @window.setpos(@cursor_position - 1, 0)
101
+ @cursor_position += 1
99
102
 
100
- @window << word
103
+ @window << word
104
+ end
101
105
  end
102
106
 
103
107
  def up_cursor_position
104
108
  if @cursor_position == 1
105
- @window.setpos(@outputs.size, 0)
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
- @window.setpos(1, 0)
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
@@ -1,3 +1,3 @@
1
1
  module Ifilter
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
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.0.4
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-07 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses