Skip to content

Commit

Permalink
ADDED: get_user_input method.
Browse files Browse the repository at this point in the history
  • Loading branch information
delano committed Jun 3, 2009
1 parent 43ecd11 commit 9e73723
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGES.txt
@@ -1,7 +1,12 @@
ANNOY, CHANGES


#### 0.5 (2009-05-07) ###############################
#### 0.5.1 (2009-06-03) ###############################

* ADDED: get_user_input method.


#### 0.5.0 (2009-05-07) ###############################

* First public release. See commit history for solutious-rudy.

2 changes: 1 addition & 1 deletion annoy.gemspec
@@ -1,7 +1,7 @@
@spec = Gem::Specification.new do |s|
s.name = "annoy"
s.rubyforge_project = "annoy"
s.version = "0.5.0"
s.version = "0.5.1"
s.summary = "Annoy: Like your annoying friend that asks you questions all the time."
s.description = s.summary
s.author = "Delano Mandelbaum"
Expand Down
43 changes: 41 additions & 2 deletions lib/annoy.rb
Expand Up @@ -198,8 +198,47 @@ def Annoy.pose_question(msg, regexp, writer=STDOUT, period=nil)
false
end
end



# Get a response from the user. Returns the string as typed by the user
# with extraneous whitespace removed.
#
# * +msg+ (required) text to display to user
# * +echo+ (optional) character to display as user types. Used for hiding passwords.
# * +period+ (optional) amount of time to wait.
#
# NOTE: Annoy uses Highline to get user responses. If +msg+ ends with a space
# character, Highline will not print a new line. If there is no space character
# Highline will print a new line.
#
# Annoy.get_user_input("Password?") # => Password?
# # => 'user types here'
#
# Annoy.get_user_input("Password? ") # => Password? 'user types here'
#
def Annoy.get_user_input(msg, echo=nil, period=nil)
return unless STDIN.tty? # Only ask a question if there's a human
return if Annoy.skip?
response = nil
begin
success = Timeout::timeout(period || @@period) do
highline = HighLine.new
response = highline.ask(msg) { |q|
unless echo.nil?
q.overwrite = true # Erase the question afterwards
q.echo = echo # Don't display response
end
q.whitespace = :strip # Remove whitespace from the response
}
end
rescue Timeout::Error => ex
writer.puts $/, "Times up!"
end
response
end

# Display +msg+ for +period+ seconds.
# NOTE: +msg+ should be a short, single line. This is a naive approach which
# simply overwrites the current line.
def Annoy.timed_display(msg, writer, period=nil)
return true unless STDIN.tty? # Only ask a question if there's a human
if Annoy.skip?
Expand Down

0 comments on commit 9e73723

Please sign in to comment.