From e84d85dcac0a9e39aff01cdc535ef56feacee017 Mon Sep 17 00:00:00 2001 From: Jim Garrison Date: Fri, 28 Nov 2014 13:16:33 -0800 Subject: [PATCH 1/4] Fix #9103, implementing `cd -` in REPL shell mode --- base/client.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/base/client.jl b/base/client.jl index 3518498795e83..2ddd60f9cd779 100644 --- a/base/client.jl +++ b/base/client.jl @@ -50,14 +50,23 @@ 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 + ENV["OLDPWD"] = new_oldpwd println(pwd()) else run(ignorestatus(@windows? cmd : (isa(STDIN, TTY) ? `$shell -i -c "($(shell_escape(cmd))) && true"` : `$shell -c "($(shell_escape(cmd))) && true"`))) From edbfd4053ccd2970789931ad56dc336c8dd7f029 Mon Sep 17 00:00:00 2001 From: Jim Garrison Date: Sat, 29 Nov 2014 13:56:32 -0800 Subject: [PATCH 2/4] repl_cmd() prints to the repl's output stream (not STDOUT) --- base/REPL.jl | 2 +- base/client.jl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/base/REPL.jl b/base/REPL.jl index a0b5d57146813..8de98ed0a7f6a 100644 --- a/base/REPL.jl +++ b/base/REPL.jl @@ -668,7 +668,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 ############################# diff --git a/base/client.jl b/base/client.jl index 2ddd60f9cd779..28cee7c17e046 100644 --- a/base/client.jl +++ b/base/client.jl @@ -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 @@ -67,7 +67,7 @@ function repl_cmd(cmd) cd() end ENV["OLDPWD"] = new_oldpwd - println(pwd()) + 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 From 3aa64ef1832aca239b372a9c3b8e5a32b5b22e0d Mon Sep 17 00:00:00 2001 From: Jim Garrison Date: Fri, 12 Dec 2014 22:11:56 -0800 Subject: [PATCH 3/4] tests for repl shell mode cd feature --- test/repl.jl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/repl.jl b/test/repl.jl index e44d82a556ce6..3996c11b327fe 100644 --- a/test/repl.jl +++ b/test/repl.jl @@ -60,6 +60,33 @@ write(stdin_write, '\x03') write(stdin_write, "\\alpha\t") readuntil(stdout_read,"α") write(stdin_write, '\x03') +# Test cd feature in shell mode +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))") +readuntil(stdout_read, realpath(tmpdir)) +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) +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()) +readuntil(stdout_read, "\n") +readuntil(stdout_read, "\n") +@test pwd() == homedir() +rm(tmpdir) +cd(origpwd) # Close REPL ^D write(stdin_write, '\x04') wait(repltask) From 67802adf50b35206512d945123949e3965c40c51 Mon Sep 17 00:00:00 2001 From: Jim Garrison Date: Sat, 13 Dec 2014 10:05:02 -0800 Subject: [PATCH 4/4] fix long string warning in repl shell mode tests --- test/repl.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/repl.jl b/test/repl.jl index 3996c11b327fe..3119109ede495 100644 --- a/test/repl.jl +++ b/test/repl.jl @@ -60,28 +60,30 @@ write(stdin_write, '\x03') write(stdin_write, "\\alpha\t") readuntil(stdout_read,"α") write(stdin_write, '\x03') -# Test cd feature in shell mode +# 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))") -readuntil(stdout_read, realpath(tmpdir)) +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) +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()) +readuntil(stdout_read, homedir()[max(1,end-39):end]) readuntil(stdout_read, "\n") readuntil(stdout_read, "\n") @test pwd() == homedir()