Skip to content

Commit

Permalink
simpler timeout timer close
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Apr 14, 2021
1 parent 94aa3c6 commit a23470c
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,29 +309,28 @@ Proceed? y/n [n] timeout 5 seconds: timed out
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, Real} = nothing)
timedout = false
uinput_entered = false
if !isnothing(timeout) && timeout > 0
timeout_timer = if !isnothing(timeout) && timeout > 0
plural = timeout == 1 ? "" : "s"
msg = !isempty(default) ? "$message [$default] timeout $timeout second$(plural): " : "$message: "
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: "
nothing
end
print(output, msg)
uinput = readline(input, keep=true)
if timedout
println(output, "timed out")
return default
else
uinput_entered = true # disable timeout action
if !isnothing(timeout_timer)
if isopen(timeout_timer)
close(timeout_timer)
else
println(output, "timed out")
return default
end
end
isempty(uinput) && return nothing # Encountered an EOF
uinput = chomp(uinput)
Expand Down

0 comments on commit a23470c

Please sign in to comment.