fancy_gets 0.1.1 → 0.1.2

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: ff5d475c55794cbbeb171f984781e0e9e953f786
4
- data.tar.gz: c1ee7c43108ebdee5d411f77ce00adee5979d028
3
+ metadata.gz: a874e5d77d0776b33016f748e58ea654146a3008
4
+ data.tar.gz: a18267ee4b5ec4e0292b17545ccfebc880561466
5
5
  SHA512:
6
- metadata.gz: 55d84b9ec732bcaf7346715fc3708be30480071888dbce1b3a7072ec69c8e44022a45fee5923b0dd0707005df13055603bb0f75507c9721036c0c42e87cdbccc
7
- data.tar.gz: bfb4ba37970492e8a901cdd09621daa212b0cdae6e9805ab2bb6544c9edbe87a9cc5b3178330f29c08950d57de0f4fabf0c5025c75d8640567b2bd269d7a257f
6
+ metadata.gz: 4c1a9f82173ba9f6d3470b2ca59912b394a232a56b7c203164a0acbae3674bf2d6325cc477d1120fd64d020cf02654d0a15a03b267330775dbc763025ded5829
7
+ data.tar.gz: 8d075d38ec64b686532ac8ce1402762bc23b1c0058d58c1494035824bbadccb6309cd44d235d9acde81b40dce5bd7d55d43314a5ed5cb1da8e27dfea10547d29
@@ -1,3 +1,3 @@
1
1
  module FancyGets
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/fancy_gets.rb CHANGED
@@ -22,22 +22,29 @@ module FancyGets
22
22
  is_multiple = false
23
23
  end
24
24
  is_multiple = true if chosen.is_a?(Array) && chosen.length > 1
25
- FancyGets.gets_internal_core(true, is_multiple, words, chosen, prefix, postfix, info)
25
+ height = words.length
26
+ winheight = IO.console.winsize.first - 3
27
+ height = winheight if height > winheight
28
+ FancyGets.gets_internal_core(true, is_multiple, words, chosen, prefix, postfix, info, height)
26
29
  end
27
30
 
28
31
  # The internal routine that makes all the magic happen
29
- def self.gets_internal_core(is_list, is_password, words = nil, chosen = [], prefix = "> ", postfix = " <", info = nil)
32
+ def self.gets_internal_core(is_list, is_password, words = nil, chosen = [], prefix = "> ", postfix = " <", info = nil, height = nil)
30
33
  # OK -- second parameter, is_password, means is_multiple when is_list is true
31
34
  is_multiple = is_list & is_password
32
35
  words.sort! unless words.nil? || is_list
33
36
  string = chosen if chosen.is_a?(String)
34
37
  position = 0
38
+ height ||= words.length
39
+ offset = (height < words.length) ? 0 : nil
35
40
  sugg = ""
36
41
  prev_sugg = ""
37
42
 
38
43
  # gsub causes any color changes to not offset spacing
39
44
  uncolor = lambda { |word| word.gsub(/\033\[[0-9;]+m/, "") }
40
45
 
46
+ max_word_length = words.map{|word| uncolor.call(word).length}.max
47
+
41
48
  write_sugg = lambda do
42
49
  # Find first word that case-insensitive matches what they've typed
43
50
  if string.empty?
@@ -67,6 +74,8 @@ module FancyGets
67
74
  else
68
75
  print "#{" " * uncolor.call(prefix).length}#{word}#{" " * uncolor.call(postfix).length}"
69
76
  end
77
+ print " " * (max_word_length - uncolor.call(words[position]).length)
78
+ print "\b" * (max_word_length - uncolor.call(words[position]).length)
70
79
  print "\b" * (uncolor.call(word).length + pre_post_length) if is_end_at_front
71
80
  end
72
81
 
@@ -85,9 +94,29 @@ module FancyGets
85
94
  chosen ||= []
86
95
  chosen = [words[0]] if chosen == [] && !is_multiple
87
96
  position = words.index(chosen.first) if chosen.length > 0
88
- words.each { |word| puts chosen.include?(word) ? "#{prefix}#{word}#{postfix}" : "#{" " * uncolor.call(prefix).length}#{word}" }
97
+ # If there's more options than we can fit at once
98
+ unless offset.nil?
99
+ # ... put the chosen one a third of the way down the screen
100
+ offset = position - (height / 3)
101
+ offset = 0 if offset < 0
102
+ end
103
+ # Scrolled any amount downwards?
104
+ # was: if (offset || 0) > 0
105
+ puts "#{" " * uncolor.call(prefix).length}#{"↑" * max_word_length}" if height < words.length
106
+ # was: ((offset || 0) > 1 ? 2 : 1)
107
+ top_bottom_reserved = offset.nil? ? 0 : 2
108
+ last_word = (offset || 0) + height - top_bottom_reserved
109
+ # Maybe we can fit it all
110
+ last_word = words.length if last_word > words.length
111
+ # 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}" }
113
+ # Can't fit it all?
114
+ # was: if last_word < (words.length - top_bottom_reserved)
115
+ puts "#{" " * uncolor.call(prefix).length}#{"↓" * max_word_length}" if height < words.length
116
+
89
117
  info ||= "Use arrow keys#{is_multiple ? ", spacebar to toggle, and ENTER to save" : " and ENTER to make a choice"}"
90
- print info + (27.chr + 91.chr + 65.chr) * (words.length - position)
118
+ # %%% used to be (words.length - position)
119
+ print info + (27.chr + 91.chr + 65.chr) * (height - (position - (offset || 0)) - (last_word <= words.length ? 1 : 0))
91
120
  # To end of text on starting line
92
121
  info_length = uncolor.call(info).length
93
122
  word_length = uncolor.call(words[position]).length + pre_post_length
@@ -149,28 +178,54 @@ module FancyGets
149
178
  end
150
179
  when 66 # - down
151
180
  if is_list && position < words.length - 1
152
- make_select.call(false, true, true) unless is_multiple
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
153
195
  w1 = uncolor.call(words[position]).length
154
196
  position += 1
155
197
  print 27.chr + 91.chr + 66.chr
156
- if is_multiple
198
+ if is_shift || !is_multiple
199
+ make_select.call((chosen.include?(words[position]) && is_shift) || !is_multiple)
200
+ else
157
201
  w2 = uncolor.call(words[position]).length
158
202
  print (w1 > w2 ? "\b" : (27.chr + 91.chr + 67.chr)) * (w1 - w2).abs
159
- else
160
- make_select.call(true)
161
203
  end
162
204
  end
163
205
  when 65 # - up
164
206
  if is_list && position > 0
165
- make_select.call(false, true, true) unless is_multiple
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
166
221
  w1 = uncolor.call(words[position]).length
167
222
  position -= 1
168
223
  print 27.chr + 91.chr + 65.chr
169
- if is_multiple
224
+ if is_shift || !is_multiple
225
+ make_select.call((chosen.include?(words[position]) && is_shift) || !is_multiple)
226
+ else
170
227
  w2 = uncolor.call(words[position]).length
171
228
  print (w1 > w2 ? "\b" : (27.chr + 91.chr + 67.chr)) * (w1 - w2).abs
172
- else
173
- make_select.call(true)
174
229
  end
175
230
  end
176
231
  when 51 # - Delete forwards?
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.1
4
+ version: 0.1.2
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-08 00:00:00.000000000 Z
11
+ date: 2016-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler