Skip to content

Commit

Permalink
Merge pull request #9192 from garrison/repl_oldpwd
Browse files Browse the repository at this point in the history
Fix #9103, implementing `cd -` in REPL shell mode
  • Loading branch information
tkelman committed Dec 15, 2014
2 parents d9d38c7 + 67802ad commit 3ff5870
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
# and pass into Base.repl_cmd for processing (handles `ls` and `cd`
# special)
on_done = respond(repl, julia_prompt) do line
Expr(:call, :(Base.repl_cmd), macroexpand(Expr(:macrocall, symbol("@cmd"),line)))
Expr(:call, :(Base.repl_cmd), macroexpand(Expr(:macrocall, symbol("@cmd"),line)), outstream(repl))
end)

################################# Stage II #############################
Expand Down
15 changes: 12 additions & 3 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exit(n) = ccall(:jl_exit, Void, (Int32,), n)
exit() = exit(0)
quit() = exit()

function repl_cmd(cmd)
function repl_cmd(cmd, out)
shell = shell_split(get(ENV,"JULIA_SHELL",get(ENV,"SHELL","/bin/sh")))
# Note that we can't support the fish shell due to its lack of subshells
# See this for details: https://github.com/JuliaLang/julia/issues/4918
Expand All @@ -50,15 +50,24 @@ function repl_cmd(cmd)
if isempty(cmd.exec)
error("no cmd to execute")
elseif cmd.exec[1] == "cd"
new_oldpwd = pwd()
if length(cmd.exec) > 2
error("cd method only takes one argument")
elseif length(cmd.exec) == 2
dir = cmd.exec[2]
cd(@windows? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
if dir == "-"
if !haskey(ENV, "OLDPWD")
error("cd: OLDPWD not set")
end
cd(ENV["OLDPWD"])
else
cd(@windows? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
end
else
cd()
end
println(pwd())
ENV["OLDPWD"] = new_oldpwd
println(out, pwd())
else
run(ignorestatus(@windows? cmd : (isa(STDIN, TTY) ? `$shell -i -c "($(shell_escape(cmd))) && true"` : `$shell -c "($(shell_escape(cmd))) && true"`)))
end
Expand Down
29 changes: 29 additions & 0 deletions test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@ write(stdin_write, '\x03')
write(stdin_write, "\\alpha\t")
readuntil(stdout_read,"α")
write(stdin_write, '\x03')
# Test cd feature in shell mode. We limit to 40 characters when
# calling readuntil() to suppress the warning it (currently) gives for
# long strings.
origpwd = pwd()
tmpdir = mktempdir()
write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
write(stdin_write, "cd $(escape_string(tmpdir))\n")
readuntil(stdout_read, "cd $(escape_string(tmpdir))"[max(1,end-39):end])
readuntil(stdout_read, realpath(tmpdir)[max(1,end-39):end])
readuntil(stdout_read, "\n")
readuntil(stdout_read, "\n")
@test pwd() == realpath(tmpdir)
write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
write(stdin_write, "cd -\n")
readuntil(stdout_read, origpwd[max(1,end-39):end])
readuntil(stdout_read, "\n")
readuntil(stdout_read, "\n")
@test pwd() == origpwd
write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
write(stdin_write, "cd\n")
readuntil(stdout_read, homedir()[max(1,end-39):end])
readuntil(stdout_read, "\n")
readuntil(stdout_read, "\n")
@test pwd() == homedir()
rm(tmpdir)
cd(origpwd)
# Close REPL ^D
write(stdin_write, '\x04')
wait(repltask)
Expand Down

0 comments on commit 3ff5870

Please sign in to comment.