Skip to content

Commit

Permalink
change to StatusEOF approach
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Apr 14, 2021
1 parent b0efd81 commit e091aa6
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,37 +287,40 @@ greater than 0 can be provided, after which the default will be returned.
See also `Base.getpass` and `Base.winprompt` for secure entry of passwords.
"""
function prompt(input::IO, output::IO, message::AbstractString; default::AbstractString="", timeout::Union{Nothing, Int} = nothing)
function prompt(input::IO, output::IO, message::AbstractString; default::AbstractString="", timeout::Union{Nothing, Real} = nothing)
timedout = false
uinput_entered = false
if !isnothing(timeout) && timeout > 0
plural = timeout == 1 ? "" : "s"
msg = !isempty(default) ? "$message [$default] timeout $timeout second$(plural): " : "$message: "
timeout_timer = Timer(timeout)
Timer(timeout) do t
uinput_entered && return
timedout = true
lock(input.cond)
input.status = Base.StatusEOF
notify(input.cond)
unlock(input.cond)
end
else
msg = !isempty(default) ? "$message [$default]: " : "$message: "
timeout_timer = nothing
end
print(output, msg)
t = @async readline(input, keep=true)
while !istaskdone(t) && (!isnothing(timeout_timer) && isopen(timeout_timer))
sleep(0.1)
end
if !isnothing(timeout_timer) && !istaskdone(t)
try
Base.throwto(t, InterruptException())
catch
end
uinput = readline(input, keep=true)
if timedout
println(output, "timed out")
return default
else
uinput_entered = true # disable timeout action
end
uinput = fetch(t)
isempty(uinput) && return nothing # Encountered an EOF
uinput = chomp(uinput)
isempty(uinput) ? default : uinput
end

# allow new prompt methods to be defined if stdin has been
# redirected to some custom stream, e.g. in IJulia.
prompt(message::AbstractString; default::AbstractString="",timeout::Union{Nothing, Int} = nothing) = prompt(stdin, stdout, message, default=default, timeout=timeout)
prompt(message::AbstractString; default::AbstractString="",timeout::Union{Nothing, Real} = nothing) = prompt(stdin, stdout, message, default=default, timeout=timeout)

# Windows authentication prompt
if Sys.iswindows()
Expand Down

0 comments on commit e091aa6

Please sign in to comment.