fancy_gets 0.1.3 → 0.1.4

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: 85562fc1f81a32285fc7ba1c55fddab5c682788c
4
- data.tar.gz: 7efd1dc42d0d741651089706c1afcf2b3103dbe3
3
+ metadata.gz: 41007873e8755595379093dbcd5f1764b73148ab
4
+ data.tar.gz: 633febcbd5720208eceeef1ca32c975ec7257eac
5
5
  SHA512:
6
- metadata.gz: 3050f1d78127a725ce2fe2cd83129ca4334e215e93a76388ac1c87ac8842a874a70bda4cddc4194b4eb642598f85a46a940be38abde99bd25d773c78988476cb
7
- data.tar.gz: 72c1fa9893d5fe8021f203df3883c2beb0d9a1eafc1719f3c5a4d0a031d9ce6862661b640c05a0e3d25669632a3f0f151d69a2f9ce6f2042ac022d6afd2a52bd
6
+ metadata.gz: 2a86edaed276de4def2109e9aa9acf6f40abbb956358c8255175ba3d3a614a184a80548acf1b82f969ef6b6d238ad63cf9d418c4fa96f6cde3a6312eef75c332
7
+ data.tar.gz: aac9199698385c4724f159335aebccf5574e28eea79feac353a617f47c3b39a17b919f05818aa27dd19a7b753e79d09f61e1e8d597695802c017cbab61b8270a
@@ -1,3 +1,3 @@
1
1
  module FancyGets
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/fancy_gets.rb CHANGED
@@ -11,31 +11,49 @@ module FancyGets
11
11
  end
12
12
 
13
13
  # Show a list of stuff, potentially some highlighted, and allow people to up-down arrow around and pick stuff
14
- def gets_list(words, is_multiple = false, chosen = [], prefix = "> ", postfix = " <", info = nil)
15
- # Trying to supply parameters but left out a "true" for is_multiple?
16
- if is_multiple.is_a?(Array)
17
- chosen = is_multiple
18
- is_multiple = true
19
- end
20
- if is_multiple.is_a?(String) || is_multiple.is_a?(Fixnum)
21
- chosen = is_multiple
22
- is_multiple = false
14
+ def gets_list(words, is_multiple = false, chosen = nil, prefix = "> ", postfix = " <", info = nil, height = nil)
15
+ if words.is_a? Hash
16
+ is_multiple = words[:is_multiple] || false
17
+ chosen = words[:chosen]
18
+ prefix = words[:prefix] || "> "
19
+ postfix = words[:postfix] || " <"
20
+ info = words[:info]
21
+ height = words[:height] || nil
22
+ words = words[:words]
23
+ else
24
+ # Trying to supply parameters but left out a "true" for is_multiple?
25
+ if is_multiple.is_a?(Enumerable) || is_multiple.is_a?(String) || is_multiple.is_a?(Fixnum)
26
+ chosen = is_multiple
27
+ is_multiple = false
28
+ end
23
29
  end
24
- is_multiple = true if chosen.is_a?(Array) && chosen.length > 1
25
- height = words.length
26
- winheight = IO.console.winsize.first - 3
27
- height = winheight if height > winheight
30
+ # Slightly inclined to ditch this in case the things they're choosing really are Enumerable
31
+ is_multiple = true if chosen.is_a?(Enumerable)
28
32
  FancyGets.gets_internal_core(true, is_multiple, words, chosen, prefix, postfix, info, height)
29
33
  end
30
34
 
31
35
  # The internal routine that makes all the magic happen
32
- def self.gets_internal_core(is_list, is_password, words = nil, chosen = [], prefix = "> ", postfix = " <", info = nil, height = nil)
36
+ def self.gets_internal_core(is_list, is_password, word_objects = nil, chosen = nil, prefix = "> ", postfix = " <", info = nil, height = nil)
33
37
  # OK -- second parameter, is_password, means is_multiple when is_list is true
34
38
  is_multiple = is_list & is_password
35
- words.sort! unless words.nil? || is_list
36
- string = chosen if chosen.is_a?(String)
39
+ unless word_objects.nil? || is_list
40
+ word_objects.sort! {|wo1, wo2| wo1.to_s <=> wo2.to_s}
41
+ end
42
+ words = word_objects.map(&:to_s)
43
+ if is_multiple
44
+ chosen ||= [0] unless is_multiple
45
+ else
46
+ if chosen.is_a?(Enumerable)
47
+ # Maybe find first string or object that matches the stuff they have sequenced in the chosen array
48
+ string = chosen.first.to_s
49
+ else
50
+ string = chosen.to_s
51
+ end
52
+ end
37
53
  position = 0
38
- height ||= words.length
54
+ height = words.length unless !height.nil? && height.is_a?(Numeric) && height > 2
55
+ winheight = IO.console.winsize.first - 3
56
+ height = winheight if height > winheight
39
57
  offset = (height < words.length) ? 0 : nil
40
58
  sugg = ""
41
59
  prev_sugg = ""
@@ -79,21 +97,77 @@ module FancyGets
79
97
  print "\b" * (uncolor.call(word).length + pre_post_length) if is_end_at_front
80
98
  end
81
99
 
100
+ arrow_down = lambda do
101
+ if position < words.length - 1
102
+ is_shift = false
103
+ # Now moving down past the bottom of the shown window?
104
+ if !offset.nil? && position >= offset + (height - 3)
105
+ print "\b" * (uncolor.call(words[position]).length + pre_post_length)
106
+ print (27.chr + 91.chr + 65.chr) * (height - (offset > 0 ? 3 : 3))
107
+ offset += 1
108
+ # Add 1 if offset + height == (words.length - 1)
109
+ (offset...(offset + height - 4)).each do |i|
110
+ end_fill = max_word_length - uncolor.call(words[i]).length
111
+ puts (is_multiple && chosen.include?(i)) ? "#{prefix}#{words[i]}#{postfix}#{" " * end_fill}" : "#{" " * uncolor.call(prefix).length}#{words[i]}#{" " * (end_fill + uncolor.call(postfix).length)}"
112
+ end
113
+ is_shift = true
114
+ end
115
+ make_select.call(chosen.include?(position) && is_shift && is_multiple, true, true) if is_shift || !is_multiple
116
+ w1 = uncolor.call(words[position]).length
117
+ position += 1
118
+ print 27.chr + 91.chr + 66.chr
119
+ if is_shift || !is_multiple
120
+ make_select.call((chosen.include?(position) && is_shift) || !is_multiple)
121
+ else
122
+ w2 = uncolor.call(words[position]).length
123
+ print (w1 > w2 ? "\b" : (27.chr + 91.chr + 67.chr)) * (w1 - w2).abs
124
+ end
125
+ end
126
+ end
127
+
128
+ arrow_up = lambda do
129
+ if position > 0
130
+ is_shift = false
131
+ # Now moving up past the top of the shown window?
132
+ if position <= (offset || 0)
133
+ print "\b" * (uncolor.call(words[position]).length + pre_post_length)
134
+ offset -= 1
135
+ # print (27.chr + 91.chr + 65.chr) if offset == 0
136
+ (offset...(offset + height - 2)).each do |i|
137
+ end_fill = max_word_length - uncolor.call(words[i]).length
138
+ puts (is_multiple && chosen.include?(i)) ? "#{prefix}#{words[i]}#{postfix}#{" " * end_fill}" : "#{" " * uncolor.call(prefix).length}#{words[i]}#{" " * (end_fill + uncolor.call(postfix).length)}"
139
+ end
140
+ print (27.chr + 91.chr + 65.chr) * (height - (offset > 0 ? 3 : 3))
141
+ is_shift = true
142
+ end
143
+ make_select.call(chosen.include?(position) && is_shift && is_multiple, true, true) if is_shift || !is_multiple
144
+ w1 = uncolor.call(words[position]).length
145
+ position -= 1
146
+ print 27.chr + 91.chr + 65.chr
147
+ if is_shift || !is_multiple
148
+ make_select.call((chosen.include?(position) && is_shift) || !is_multiple)
149
+ else
150
+ w2 = uncolor.call(words[position]).length
151
+ print (w1 > w2 ? "\b" : (27.chr + 91.chr + 67.chr)) * (w1 - w2).abs
152
+ end
153
+ end
154
+ end
155
+
82
156
  if is_list
83
157
  # Maybe confirm the height is adequate by checking out IO.console.winsize
84
158
  case chosen.class.name
85
159
  when "Fixnum"
86
- chosen = [words[chosen]]
160
+ chosen = [chosen]
87
161
  when "String"
88
162
  if words.include?(chosen)
89
- chosen = [chosen]
163
+ chosen = [words.index(chosen)]
90
164
  else
91
165
  chosen = []
92
166
  end
93
167
  end
94
168
  chosen ||= []
95
- chosen = [words[0]] if chosen == [] && !is_multiple
96
- position = words.index(chosen.first) if chosen.length > 0
169
+ chosen = [0] if chosen == [] && !is_multiple
170
+ position = chosen.first if chosen.length > 0
97
171
  # If there's more options than we can fit at once
98
172
  unless offset.nil?
99
173
  # ... put the chosen one a third of the way down the screen
@@ -109,7 +183,7 @@ module FancyGets
109
183
  # Maybe we can fit it all
110
184
  last_word = words.length if last_word > words.length
111
185
  # Write all the visible words
112
- words[(offset || 0)...last_word].each { |word| puts chosen.include?(word) ? "#{prefix}#{word}#{postfix}" : "#{" " * uncolor.call(prefix).length}#{word}" }
186
+ ((offset || 0)...last_word).each { |i| puts chosen.include?(i) ? "#{prefix}#{words[i]}#{postfix}" : "#{" " * uncolor.call(prefix).length}#{words[i]}" }
113
187
  # Can't fit it all?
114
188
  # was: if last_word < (words.length - top_bottom_reserved)
115
189
  puts "#{" " * uncolor.call(prefix).length}#{"↓" * max_word_length}" if height < words.length
@@ -167,66 +241,22 @@ module FancyGets
167
241
  when 91 # Arrow keys
168
242
  case ch = STDIN.getch.ord
169
243
  when 68 # Arrow left
170
- if position > 0
244
+ if !is_list && position > 0
171
245
  print "\b" # 27.chr + 91.chr + 68.chr
172
246
  position -= 1
173
247
  end
174
248
  when 67 # Arrow right
175
- if position < string.length
249
+ if !is_list && position < string.length
176
250
  print 27.chr + 91.chr + 67.chr
177
251
  position += 1
178
252
  end
179
253
  when 66 # - down
180
- if is_list && position < words.length - 1
181
- is_shift = false
182
- # Now moving down past the bottom of the shown window?
183
- if !offset.nil? && position >= offset + (height - 3)
184
- print "\b" * (uncolor.call(words[position]).length + pre_post_length)
185
- print (27.chr + 91.chr + 65.chr) * (height - (offset > 0 ? 3 : 3))
186
- offset += 1
187
- # Add 1 if offset + height == (words.length - 1)
188
- words[offset...(offset + height - 4)].each do |word|
189
- end_fill = max_word_length - uncolor.call(word).length
190
- puts (is_multiple && chosen.include?(word)) ? "#{prefix}#{word}#{postfix}#{" " * end_fill}" : "#{" " * uncolor.call(prefix).length}#{word}#{" " * (end_fill + uncolor.call(postfix).length)}"
191
- end
192
- is_shift = true
193
- end
194
- make_select.call(chosen.include?(words[position]) && is_shift && is_multiple, true, true) if is_shift || !is_multiple
195
- w1 = uncolor.call(words[position]).length
196
- position += 1
197
- print 27.chr + 91.chr + 66.chr
198
- if is_shift || !is_multiple
199
- make_select.call((chosen.include?(words[position]) && is_shift) || !is_multiple)
200
- else
201
- w2 = uncolor.call(words[position]).length
202
- print (w1 > w2 ? "\b" : (27.chr + 91.chr + 67.chr)) * (w1 - w2).abs
203
- end
254
+ if is_list
255
+ arrow_down.call
204
256
  end
205
257
  when 65 # - up
206
- if is_list && position > 0
207
- is_shift = false
208
- # Now moving up past the top of the shown window?
209
- if position <= (offset || 0)
210
- print "\b" * (uncolor.call(words[position]).length + pre_post_length)
211
- offset -= 1
212
- # print (27.chr + 91.chr + 65.chr) if offset == 0
213
- words[offset...(offset + height - 2)].each do |word|
214
- end_fill = max_word_length - uncolor.call(word).length
215
- puts (is_multiple && chosen.include?(word)) ? "#{prefix}#{word}#{postfix}#{" " * end_fill}" : "#{" " * uncolor.call(prefix).length}#{word}#{" " * (end_fill + uncolor.call(postfix).length)}"
216
- end
217
- print (27.chr + 91.chr + 65.chr) * (height - (offset > 0 ? 3 : 3))
218
- is_shift = true
219
- end
220
- make_select.call(chosen.include?(words[position]) && is_shift && is_multiple, true, true) if is_shift || !is_multiple
221
- w1 = uncolor.call(words[position]).length
222
- position -= 1
223
- print 27.chr + 91.chr + 65.chr
224
- if is_shift || !is_multiple
225
- make_select.call((chosen.include?(words[position]) && is_shift) || !is_multiple)
226
- else
227
- w2 = uncolor.call(words[position]).length
228
- print (w1 > w2 ? "\b" : (27.chr + 91.chr + 67.chr)) * (w1 - w2).abs
229
- end
258
+ if is_list
259
+ arrow_up.call
230
260
  end
231
261
  when 51 # - Delete forwards?
232
262
  else
@@ -260,16 +290,22 @@ module FancyGets
260
290
  end
261
291
  else # Insert character
262
292
  if is_list
263
- if is_multiple && ch == " "
264
- # Toggle this entry
265
- word = words[position]
266
- does_include = chosen.include?(word)
267
- if does_include
268
- chosen -= [word]
269
- else
270
- chosen += [word]
293
+ case ch
294
+ when " "
295
+ if is_multiple
296
+ # Toggle this entry
297
+ does_include = chosen.include?(position)
298
+ if does_include
299
+ chosen -= [position]
300
+ else
301
+ chosen += [position]
302
+ end
303
+ make_select.call(!does_include, true)
271
304
  end
272
- make_select.call(!does_include, true)
305
+ when "j" # Down
306
+ arrow_down.call
307
+ when "k" # Up
308
+ arrow_up.call
273
309
  end
274
310
  else
275
311
  string = string[0...position] + ch + string[position..-1]
@@ -288,10 +324,9 @@ module FancyGets
288
324
 
289
325
  if is_list
290
326
  # Put chosen stuff in same order as it's listed in the words array
291
- chosen = words.select { |word| chosen.include?(word) }
292
- is_multiple ? chosen : words[position]
327
+ is_multiple ? chosen.map {|c| word_objects[c] } : word_objects[position]
293
328
  else
294
- sugg.empty? ? string : sugg
329
+ sugg.empty? ? string : word_objects[words.index(sugg)]
295
330
  end
296
331
  end
297
332
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fancy_gets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-09 00:00:00.000000000 Z
11
+ date: 2016-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler