Skip to content

Commit 12d2de1

Browse files
author
KristofferC
committed
make pkgstr independent of REPL
1 parent f3b81f1 commit 12d2de1

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

src/REPLMode/REPLMode.jl

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -377,21 +377,19 @@ end
377377
#############
378378
# Execution #
379379
#############
380-
function do_cmd(repl::REPL.AbstractREPL, input::String; do_rethrow=false)
381-
if !isinteractive() && !TEST_MODE[] && !PRINTED_REPL_WARNING[]
382-
@warn "The Pkg REPL mode is intended for interactive use only, and should not be used from scripts. It is recommended to use the functional API instead."
383-
PRINTED_REPL_WARNING[] = true
384-
end
380+
function prepare_cmd(input)
381+
statements = parse(input)
382+
commands = map(Command, statements)
383+
return commands
384+
end
385+
386+
do_cmds(repl::REPL.AbstractREPL, input::String) = do_cmds(repl, prepare_cmd(input))
387+
do_cmds(input::String, io=stdout_f()) = do_cmds(prepare_cmd(input), io)
388+
389+
function do_cmds(repl::REPL.AbstractREPL, commands::Vector{Command})
385390
try
386-
statements = parse(input)
387-
commands = map(Command, statements)
388-
xs = []
389-
for command in commands
390-
push!(xs, do_cmd!(command, repl))
391-
end
392-
return TEST_MODE[] ? xs : nothing
391+
return do_cmds(commands, repl.t.out_stream)
393392
catch err
394-
do_rethrow && rethrow()
395393
if err isa PkgError || err isa Resolve.ResolverError
396394
Base.display_error(repl.t.err_stream, ErrorException(sprint(showerror, err)), Ptr{Nothing}[])
397395
else
@@ -400,9 +398,22 @@ function do_cmd(repl::REPL.AbstractREPL, input::String; do_rethrow=false)
400398
end
401399
end
402400

403-
function do_cmd!(command::Command, repl)
401+
402+
function do_cmds(commands::Vector{Command}, io)
403+
if !isinteractive() && !TEST_MODE[] && !PRINTED_REPL_WARNING[]
404+
@warn "The Pkg REPL mode is intended for interactive use only, and should not be used from scripts. It is recommended to use the functional API instead."
405+
PRINTED_REPL_WARNING[] = true
406+
end
407+
xs = []
408+
for command in commands
409+
push!(xs, do_cmd(command, io))
410+
end
411+
return TEST_MODE[] ? xs : nothing
412+
end
413+
414+
function do_cmd(command::Command, io)
404415
# REPL specific commands
405-
command.spec === SPECS["package"]["help"] && return Base.invokelatest(do_help!, command, repl)
416+
command.spec === SPECS["package"]["help"] && return Base.invokelatest(do_help!, command, io)
406417
# API commands
407418
if command.spec.should_splat
408419
TEST_MODE[] && return command.spec.api, command.arguments..., command.options
@@ -421,10 +432,9 @@ function parse_command(words::Vector{QString})
421432
return statement.spec === nothing ? statement.super : statement.spec
422433
end
423434

424-
function do_help!(command::Command, repl::REPL.AbstractREPL)
425-
disp = REPL.REPLDisplay(repl)
435+
function do_help!(command::Command, io)
426436
if isempty(command.arguments)
427-
Base.display(disp, help)
437+
show(io, MIME("text/plain"), help)
428438
return
429439
end
430440
help_md = md""
@@ -442,34 +452,23 @@ function do_help!(command::Command, repl::REPL.AbstractREPL)
442452
push!(help_md.content, cmd.help)
443453
end
444454
!isempty(command.arguments) && @warn "More than one command specified, only rendering help for first"
445-
Base.display(disp, help_md)
455+
show(io, MIME("text/plain"), help_md)
446456
end
447457

448-
######################
449-
# REPL mode creation #
450-
######################
451-
452458
# Provide a string macro pkg"cmd" that can be used in the same way
453459
# as the REPLMode `pkg> cmd`. Useful for testing and in environments
454460
# where we do not have a REPL, e.g. IJulia.
455-
struct MiniREPL <: REPL.AbstractREPL
456-
display::TextDisplay
457-
t::REPL.Terminals.TTYTerminal
458-
end
459-
function MiniREPL()
460-
MiniREPL(TextDisplay(stdout_f()), REPL.Terminals.TTYTerminal(get(ENV, "TERM", Sys.iswindows() ? "" : "dumb"), stdin, stdout_f(), stderr_f()))
461+
macro pkg_str(str::String)
462+
:(pkgstr($str))
461463
end
462-
REPL.REPLDisplay(repl::MiniREPL) = repl.display
463-
464-
const minirepl = Ref{MiniREPL}()
465464

466-
__init__() = minirepl[] = MiniREPL()
467-
468-
macro pkg_str(str::String)
469-
:($(do_cmd)(minirepl[], $str; do_rethrow=true))
465+
function pkgstr(str::String)
466+
return do_cmds(str)
470467
end
471468

472-
pkgstr(str::String) = do_cmd(minirepl[], str; do_rethrow=true)
469+
######################
470+
# REPL mode creation #
471+
######################
473472

474473
struct PkgCompletionProvider <: LineEdit.CompletionProvider end
475474

@@ -554,7 +553,7 @@ function create_mode(repl::REPL.AbstractREPL, main::LineEdit.Prompt)
554553
ok || return REPL.transition(s, :abort)
555554
input = String(take!(buf))
556555
REPL.reset(repl)
557-
do_cmd(repl, input)
556+
do_cmds(repl, input)
558557
REPL.prepare_next(repl)
559558
REPL.reset_state(s)
560559
s.current_mode.sticky || REPL.transition(s, main)

test/runtests.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ else
3333
Pkg.DEFAULT_IO[] = devnull # or stdout
3434
end
3535

36-
Pkg.REPLMode.minirepl[] = Pkg.REPLMode.MiniREPL() # re-set this given DEFAULT_IO has changed
37-
3836
include("utils.jl")
3937
Logging.with_logger((islogging || Pkg.DEFAULT_IO[] == devnull) ? Logging.ConsoleLogger(Pkg.DEFAULT_IO[]) : Logging.current_logger()) do
4038

0 commit comments

Comments
 (0)