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 0af4206
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,10 @@ 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
Expand All @@ -324,14 +322,15 @@ function prompt(input::IO, output::IO, message::AbstractString; default::Abstrac
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
isnothing(timeout_timer) || close(timeout_timer)
end
isempty(uinput) && return nothing # Encountered an EOF
uinput = chomp(uinput)
Expand Down

0 comments on commit 0af4206

Please sign in to comment.