Skip to content

Instantly share code, notes, and snippets.

@tristanmorgan
Created December 19, 2018 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristanmorgan/340afe5883652d0399fc3e372b64cc6f to your computer and use it in GitHub Desktop.
Save tristanmorgan/340afe5883652d0399fc3e372b64cc6f to your computer and use it in GitHub Desktop.
lib/thor/line_editor/basic.rb - print stars for no-echo input
class Thor
module LineEditor
class Basic
attr_reader :prompt, :options
def self.available?
true
end
def initialize(prompt, options)
@prompt = prompt
@options = options
end
def readline
$stdout.print(prompt)
get_input
end
private
def get_input
if echo?
$stdin.gets
else
require 'io/console'
password = ''
loop do
character = $stdin.getch
break unless character
if ["\n", "\r"].include? character
puts ''
break
elsif ["\b", "\u007f"].include? character
password.chop!
print "\b\e[P"
elsif character == "\u0003"
exit 1
else
print '*'
password << character
end
end
password
end
end
def echo?
options.fetch(:echo, true)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment