fancy_gets 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fancy_gets.gemspec +40 -0
- data/lib/fancy_gets/version.rb +3 -0
- data/lib/fancy_gets.rb +242 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fa82413ae8f11a2c6a6760a5d15d951d6cb77f12
|
4
|
+
data.tar.gz: 1e5e1b10db12c9b5452ff6e3996ec8fc6cac6d13
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 34acd4801dec89245f9f96c09bcebc00d177c788d1f0daa0f2ce757c8a2dd7055303961caa3ca8ea6b44c1c45090379dd44648306957463f408c3f6bb72bfd1f
|
7
|
+
data.tar.gz: 119b5775cbc26f3b67d1ca224fc052f6317640e2f1ca7b7c9d0cc33ce87469a1096a8e02cdf65b1f6ee484fd497b233adcf3072d041b0aa00041996c950a134c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at lorint@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Lorin Thwaits
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# FancyGets
|
2
|
+
|
3
|
+
This gem exists to banish crusty UX that our users endure at the command line.
|
4
|
+
|
5
|
+
For far too long we've been stuck with just gets and getc. When prompting the
|
6
|
+
user with a list of choices, wouldn't it be nice to have the feel of a <select>
|
7
|
+
in HTML? Or to auto-suggest options as they type? Or perhaps offer a password
|
8
|
+
entry with asterisks instead of just sitting silent, which confuses many users?
|
9
|
+
|
10
|
+
Read on.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Very straightforward ... this simple entry in the Gemfile, after which make sure to run "bundle":
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'fancy_gets'
|
18
|
+
```
|
19
|
+
|
20
|
+
Or have it end up in your /usr/lib/ruby/gems/... folder with:
|
21
|
+
|
22
|
+
$ gem install fancy_gets
|
23
|
+
|
24
|
+
And at the top of any CLI app do the require and include:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'fancy_gets'
|
28
|
+
include FancyGets
|
29
|
+
```
|
30
|
+
|
31
|
+
And then you can impress all manner of people accustomed to the stark limitations
|
32
|
+
of command line apps. Heck, this even makes them fun again.
|
33
|
+
|
34
|
+
## gets_list
|
35
|
+
|
36
|
+
Imagine you have this cool array of beach things. Have the user pick one.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
toys = ["Skimboard", "Volleyball", "Kite", "Beach Ball", "Water Gun", "Frisbee"]
|
40
|
+
picked_toy = gets_list(toys)
|
41
|
+
puts "\nBringing a #{picked_toy} sounds like loads of fun at the beach."
|
42
|
+
```
|
43
|
+
|
44
|
+
And perhaps a little later you'd like to ask again what they'd like, plus
|
45
|
+
give a default of what they had picked before.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
new_toy = gets_list(toys, picked_toy)
|
49
|
+
puts "\nCool! This time you've brought a #{new_toy}."
|
50
|
+
```
|
51
|
+
|
52
|
+
If you don't prefer the default > Toy Name < prompts, feel free to have your own
|
53
|
+
prefix and suffix applied to choices as the user arrows up and down, and supply
|
54
|
+
your own prompt text if you like. This is the full syntax for gets_list, and
|
55
|
+
the false indicates it's not doing multiple choice.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
another_toy = gets_list(toys, false, nil, "==>", "<== PARTY TIME!", "Use arrows to pick something awesome.")
|
59
|
+
puts "\nSo much to love about #{another_toy}."
|
60
|
+
```
|
61
|
+
|
62
|
+
Another cool thing this allows is to change the color of selected items. You may want
|
63
|
+
to check out Michał Kalbarczyk's [colorize gem](https://github.com/fazibear/colorize "Michał loves all things \033") for more info.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
another_toy = gets_list(toys, false, nil, "==>", "<== PARTY TIME!", "Use arrows to pick something awesome.")
|
67
|
+
puts "\nSo much to love about #{another_toy}."
|
68
|
+
```
|
69
|
+
|
70
|
+
Easy to have multiple choices, and bring back an array. In this case it already
|
71
|
+
has chosen the kite and water gun.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
picked_toys = gets_list(toys, true, ["Kite", "Water Gun"])
|
75
|
+
puts "\nYou've picked #{picked_toys.join(", ")}."
|
76
|
+
```
|
77
|
+
|
78
|
+
## gets_auto_suggest
|
79
|
+
|
80
|
+
Still using the same cool array of things, let's have the user see auto-suggest text
|
81
|
+
as they type. As soon as the proper term appears, they can hit ENTER and the full
|
82
|
+
string for that item is returned. The search is case and color insensitive.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
toys = ["Skimboard", "Volleyball", "Kite", "Beach Ball", "Water Gun", "Frisbee"]
|
86
|
+
picked_toy = gets_auto_suggest(toys)
|
87
|
+
puts "\nYou chose #{picked_toy}."
|
88
|
+
```
|
89
|
+
|
90
|
+
And as above, you can offer a default choice. This can be set with a full or partial
|
91
|
+
string.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
new_toy = gets_auto_suggest(toys, picked_toy[0..2])
|
95
|
+
puts "\nChanging it up for #{new_toy}."
|
96
|
+
```
|
97
|
+
|
98
|
+
## gets_password
|
99
|
+
|
100
|
+
The final bit of coolness is a simple guarded password entry. All variables used
|
101
|
+
by the gem are local, so after returning a response any plain text which was entered
|
102
|
+
does not stick around past a garbage collection.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
pwd = gets_password
|
106
|
+
puts "\nI think I heard you whisper, \"#{pwd}\"."
|
107
|
+
```
|
108
|
+
|
109
|
+
This also allows default text to be provided, although I can't easily think of a
|
110
|
+
circumstance in which that's useful. But perhaps to you it could be.
|
111
|
+
|
112
|
+
Bug reports and pull requests are welcome: https://github.com/lorint/fancy_gets.
|
113
|
+
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fancy_gets"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/fancy_gets.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fancy_gets/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fancy_gets"
|
8
|
+
spec.version = FancyGets::VERSION
|
9
|
+
spec.authors = ["Lorin Thwaits"]
|
10
|
+
spec.email = ["lorint@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Enhanced gets with listbox, auto-complete, and password support}
|
13
|
+
spec.description = %q{This gem exists to banish crusty UX that our users endure at the command line.
|
14
|
+
|
15
|
+
For far too long we've been stuck with just gets and getc. When prompting the
|
16
|
+
user with a list of choices, wouldn't it be nice to have the feel of a <select>
|
17
|
+
in HTML? Or to auto-suggest options as they type? Or perhaps offer a password
|
18
|
+
entry with asterisks instead of just sitting silent, which confuses many users?
|
19
|
+
|
20
|
+
It's all here. Enjoy!}
|
21
|
+
spec.homepage = "http://polangeles.com/gems/fancy_gets"
|
22
|
+
spec.license = "MIT"
|
23
|
+
|
24
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
25
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
26
|
+
if spec.respond_to?(:metadata)
|
27
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
28
|
+
else
|
29
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
30
|
+
end
|
31
|
+
|
32
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
spec.bindir = "exe"
|
34
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ["lib"]
|
36
|
+
|
37
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
38
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
39
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
40
|
+
end
|
data/lib/fancy_gets.rb
ADDED
@@ -0,0 +1,242 @@
|
|
1
|
+
require "fancy_gets/version"
|
2
|
+
require 'io/console'
|
3
|
+
|
4
|
+
module FancyGets
|
5
|
+
def gets_auto_suggest(words = nil, default = "")
|
6
|
+
FancyGets.gets_internal_core(false, false, words, default)
|
7
|
+
end
|
8
|
+
|
9
|
+
def gets_password(default = "")
|
10
|
+
FancyGets.gets_internal_core(false, true, nil, default)
|
11
|
+
end
|
12
|
+
|
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
|
23
|
+
end
|
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)
|
26
|
+
end
|
27
|
+
|
28
|
+
# 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)
|
30
|
+
# OK -- second parameter, is_password, means is_multiple when is_list is true
|
31
|
+
is_multiple = is_list & is_password
|
32
|
+
words.sort! unless words.nil? || is_list
|
33
|
+
string = chosen if chosen.is_a?(String)
|
34
|
+
position = 0
|
35
|
+
sugg = ""
|
36
|
+
prev_sugg = ""
|
37
|
+
|
38
|
+
# gsub causes any color changes to not offset spacing
|
39
|
+
uncolor = lambda { |word| word.gsub(/\033\[[0-9;]+m/, "") }
|
40
|
+
|
41
|
+
write_sugg = lambda do
|
42
|
+
# Find first word that case-insensitive matches what they've typed
|
43
|
+
if string.blank?
|
44
|
+
sugg = ""
|
45
|
+
else
|
46
|
+
sugg = words.select { |word| uncolor.call(word).downcase.start_with? string.downcase }.first || ""
|
47
|
+
end
|
48
|
+
extra_spaces = uncolor.call(prev_sugg).length - uncolor.call(sugg).length
|
49
|
+
extra_spaces = 0 if extra_spaces < 0
|
50
|
+
" - #{sugg}#{" " * extra_spaces} #{"\b" * ((uncolor.call(sugg).length + 4 + extra_spaces) + string.length - position)}"
|
51
|
+
end
|
52
|
+
|
53
|
+
pre_post_length = uncolor.call(prefix + postfix).length
|
54
|
+
|
55
|
+
# Used for dropdown select / deselect
|
56
|
+
clear_dropdown_info = lambda do
|
57
|
+
print "\b" * (uncolor.call(words[position]).length + pre_post_length)
|
58
|
+
print (27.chr + 91.chr + 66.chr) * (words.length - position)
|
59
|
+
info_length = uncolor.call(info).length
|
60
|
+
print " " * info_length + "\b" * info_length
|
61
|
+
end
|
62
|
+
make_select = lambda do |is_select, is_go_to_front = false, is_end_at_front = false|
|
63
|
+
word = words[position]
|
64
|
+
print "\b" * (uncolor.call(word).length + pre_post_length) if is_go_to_front
|
65
|
+
if is_select
|
66
|
+
print "#{prefix}#{word}#{postfix}"
|
67
|
+
else
|
68
|
+
print "#{" " * uncolor.call(prefix).length}#{word}#{" " * uncolor.call(postfix).length}"
|
69
|
+
end
|
70
|
+
print "\b" * (uncolor.call(word).length + pre_post_length) if is_end_at_front
|
71
|
+
end
|
72
|
+
|
73
|
+
if is_list
|
74
|
+
# Maybe confirm the height is adequate by checking out IO.console.winsize
|
75
|
+
case chosen.class.name
|
76
|
+
when "Fixnum"
|
77
|
+
chosen = [words[chosen]]
|
78
|
+
when "String"
|
79
|
+
if words.include?(chosen)
|
80
|
+
chosen = [chosen]
|
81
|
+
else
|
82
|
+
chosen = []
|
83
|
+
end
|
84
|
+
end
|
85
|
+
chosen ||= []
|
86
|
+
chosen = [words[0]] if chosen == [] && !is_multiple
|
87
|
+
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}" }
|
89
|
+
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)
|
91
|
+
# To end of text on starting line
|
92
|
+
info_length = uncolor.call(info).length
|
93
|
+
word_length = uncolor.call(words[position]).length + pre_post_length
|
94
|
+
print (info_length > word_length ? "\b" : (27.chr + 91.chr + 67.chr)) * (info_length - word_length).abs
|
95
|
+
else
|
96
|
+
position = string.length
|
97
|
+
if is_password
|
98
|
+
print "*" * string.length
|
99
|
+
else
|
100
|
+
print string + write_sugg.call
|
101
|
+
end
|
102
|
+
end
|
103
|
+
loop do
|
104
|
+
ch = STDIN.getch
|
105
|
+
code = ch.ord
|
106
|
+
case code
|
107
|
+
when 3 # CTRL-C
|
108
|
+
clear_dropdown_info.call if is_list
|
109
|
+
exit
|
110
|
+
when 13 # ENTER
|
111
|
+
if is_list
|
112
|
+
clear_dropdown_info.call
|
113
|
+
else
|
114
|
+
print "\n"
|
115
|
+
end
|
116
|
+
break
|
117
|
+
when 27 # ESC -- which means lots of special stuff
|
118
|
+
case ch = STDIN.getch.ord
|
119
|
+
when 79 # Function keys
|
120
|
+
# puts "ESC 79"
|
121
|
+
case ch = STDIN.getch.ord
|
122
|
+
when 80 #F1
|
123
|
+
# puts "F1"
|
124
|
+
when 81 #F2
|
125
|
+
when 82 #F3
|
126
|
+
when 83 #F4
|
127
|
+
when 84 #F5
|
128
|
+
when 85 #F6
|
129
|
+
when 86 #F7
|
130
|
+
when 87 #F8
|
131
|
+
when 88 #F9
|
132
|
+
when 89 #F10
|
133
|
+
when 90 #F11
|
134
|
+
when 91 #F12
|
135
|
+
# puts "F12"
|
136
|
+
when 92 #F13
|
137
|
+
end
|
138
|
+
when 91 # Arrow keys
|
139
|
+
case ch = STDIN.getch.ord
|
140
|
+
when 68 # Arrow left
|
141
|
+
if position > 0
|
142
|
+
print "\b" # 27.chr + 91.chr + 68.chr
|
143
|
+
position -= 1
|
144
|
+
end
|
145
|
+
when 67 # Arrow right
|
146
|
+
if position < string.length
|
147
|
+
print 27.chr + 91.chr + 67.chr
|
148
|
+
position += 1
|
149
|
+
end
|
150
|
+
when 66 # - down
|
151
|
+
if is_list && position < words.length - 1
|
152
|
+
make_select.call(false, true, true) unless is_multiple
|
153
|
+
w1 = uncolor.call(words[position]).length
|
154
|
+
position += 1
|
155
|
+
print 27.chr + 91.chr + 66.chr
|
156
|
+
if is_multiple
|
157
|
+
w2 = uncolor.call(words[position]).length
|
158
|
+
print (w1 > w2 ? "\b" : (27.chr + 91.chr + 67.chr)) * (w1 - w2).abs
|
159
|
+
else
|
160
|
+
make_select.call(true)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
when 65 # - up
|
164
|
+
if is_list && position > 0
|
165
|
+
make_select.call(false, true, true) unless is_multiple
|
166
|
+
w1 = uncolor.call(words[position]).length
|
167
|
+
position -= 1
|
168
|
+
print 27.chr + 91.chr + 65.chr
|
169
|
+
if is_multiple
|
170
|
+
w2 = uncolor.call(words[position]).length
|
171
|
+
print (w1 > w2 ? "\b" : (27.chr + 91.chr + 67.chr)) * (w1 - w2).abs
|
172
|
+
else
|
173
|
+
make_select.call(true)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
when 51 # - Delete forwards?
|
177
|
+
else
|
178
|
+
# puts "ESC 91 #{ch}"
|
179
|
+
end
|
180
|
+
else
|
181
|
+
# Something wacky?
|
182
|
+
# puts "code #{ch} #{STDIN.getch.ord} #{STDIN.getch.ord} #{STDIN.getch.ord} #{STDIN.getch.ord}"
|
183
|
+
end
|
184
|
+
when 127 # Backspace
|
185
|
+
if !is_list && position > 0
|
186
|
+
string = string[0...position - 1] + string[position..-1]
|
187
|
+
if words.nil?
|
188
|
+
position -= 1
|
189
|
+
print "\b#{is_password ? "*" * (string.length - position) : string[position..-1]} #{"\b" * (string.length - position + 1)}"
|
190
|
+
else
|
191
|
+
prev_sugg = sugg
|
192
|
+
position -= 1
|
193
|
+
print "\b#{string[position..-1]}#{write_sugg.call}"
|
194
|
+
end
|
195
|
+
end
|
196
|
+
when 126 # Delete (forwards)
|
197
|
+
if !is_list && position < string.length
|
198
|
+
string = string[0...position] + string[position + 1..-1]
|
199
|
+
if words.nil?
|
200
|
+
print "#{is_password ? "*" * (string.length - position) : string[position..-1]} #{"\b" * (string.length - position + 1)}"
|
201
|
+
else
|
202
|
+
prev_sugg = sugg
|
203
|
+
print "#{string[position..-1]}#{write_sugg.call}"
|
204
|
+
end
|
205
|
+
end
|
206
|
+
else # Insert character
|
207
|
+
if is_list
|
208
|
+
if is_multiple && ch == " "
|
209
|
+
# Toggle this entry
|
210
|
+
word = words[position]
|
211
|
+
does_include = chosen.include?(word)
|
212
|
+
if does_include
|
213
|
+
chosen -= [word]
|
214
|
+
else
|
215
|
+
chosen += [word]
|
216
|
+
end
|
217
|
+
make_select.call(!does_include, true)
|
218
|
+
end
|
219
|
+
else
|
220
|
+
string = string[0...position] + ch + string[position..-1]
|
221
|
+
if words.nil?
|
222
|
+
ch = "*" if is_password
|
223
|
+
position += 1
|
224
|
+
print "#{ch}#{is_password ? "*" * (string.length - position) : string[position..-1]}#{"\b" * (string.length - position)}"
|
225
|
+
else
|
226
|
+
prev_sugg = sugg
|
227
|
+
position += 1
|
228
|
+
print "#{ch}#{string[position..-1]}#{write_sugg.call}"
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
if is_list
|
235
|
+
# Put chosen stuff in same order as it's listed in the words array
|
236
|
+
chosen = words.select { |word| chosen.include?(word) }
|
237
|
+
is_multiple ? chosen : words[position]
|
238
|
+
else
|
239
|
+
sugg.empty? ? string : sugg
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fancy_gets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lorin Thwaits
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: |-
|
56
|
+
This gem exists to banish crusty UX that our users endure at the command line.
|
57
|
+
|
58
|
+
For far too long we've been stuck with just gets and getc. When prompting the
|
59
|
+
user with a list of choices, wouldn't it be nice to have the feel of a <select>
|
60
|
+
in HTML? Or to auto-suggest options as they type? Or perhaps offer a password
|
61
|
+
entry with asterisks instead of just sitting silent, which confuses many users?
|
62
|
+
|
63
|
+
It's all here. Enjoy!
|
64
|
+
email:
|
65
|
+
- lorint@gmail.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- ".gitignore"
|
71
|
+
- ".rspec"
|
72
|
+
- ".travis.yml"
|
73
|
+
- CODE_OF_CONDUCT.md
|
74
|
+
- Gemfile
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- bin/console
|
79
|
+
- bin/setup
|
80
|
+
- fancy_gets.gemspec
|
81
|
+
- lib/fancy_gets.rb
|
82
|
+
- lib/fancy_gets/version.rb
|
83
|
+
homepage: http://polangeles.com/gems/fancy_gets
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata:
|
87
|
+
allowed_push_host: https://rubygems.org
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.5.1
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Enhanced gets with listbox, auto-complete, and password support
|
108
|
+
test_files: []
|