Skip to content

Commit

Permalink
REPL: offer to try to install package if not found
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Dec 29, 2020
1 parent 5e2a8a1 commit a6aecac
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,26 @@ end
outstream(r::BasicREPL) = r.terminal
hascolor(r::BasicREPL) = hascolor(r.terminal)

function try_pkg_add(mod::Symbol)
resp = try
Base.prompt(" \033[32m\033[1m└\033[22m\033[0m Package $(repr(String(mod))) not found, with active project $(repr(Base.active_project()))
Try adding $(repr(String(mod))) to the active environment? (y/n)",
default = "n")
catch err
if err isa InterruptException
println()
return false
end
end
if lowercase(resp) in ["y", "yes"]
let Pkg = Base.require(Base.PkgId(Base.UUID((0x44cfe95a_1eb2_52ea,0xb672_e2afdf69b78f)), "Pkg"))
Pkg.add(string(mod))
return true
end
end
return false
end

function run_frontend(repl::BasicREPL, backend::REPLBackendRef)
d = REPLDisplay(repl)
dopushdisplay = !in(d,Base.Multimedia.displays)
Expand Down Expand Up @@ -776,7 +796,14 @@ backend(r::AbstractREPL) = r.backendref

function eval_with_backend(ast, backend::REPLBackendRef)
put!(backend.repl_channel, (ast, 1))
return take!(backend.response_channel) # (val, iserr)
resp = take!(backend.response_channel) # (val, iserr)
if last(resp)
err = first(first(first(resp)))
if err isa ModuleNotFoundError
try_pkg_add(err.mod) && return Pair{Any, Bool}(nothing, false)
end
end
return resp
end

function respond(f, repl, main; pass_empty::Bool = false, suppress_on_semicolon::Bool = true)
Expand Down

0 comments on commit a6aecac

Please sign in to comment.