How can I make the Julia REPL display a dynamic prompt? For example, how might the REPL print the current date and time in the prompt each time it prints a new prompt?
Try OhMyREPL. Looks like the dynamic prompt is a recent addition.
I just started using OhMyREPL.jl today, but the published documentation still shows the old method signature of input_prompt!
:
OhMyREPL.input_prompt!(str::String, color::Symbol)
Given the new input_prompt!
signature, how can I determine the signature of the method that input_prompt!
will invoke that corresponds to its function argument?
You may need to be on the master version of OhMyREPL to get that feature. You can look at methods(OhMyREPL.input_prompt!)
.
I recall encountering the Union{String, Function}
argument in input_prompt!
in the version of the package that I installed, so I think I have the latest release. It’s the published documentation that is out of date.
Short of looking at the source code of input_prompt!
, how can I determine the signature of the method that input_prompt!
expects as its first argument?
To display the current date in my Julia REPL prompt, I added the following to C:\Users\derek\.julia\config\startup.jl
:
using OhMyREPL
using Dates
input_prompt() = "julia-" * string(Dates.now()) * ">"
atreplinit() do repl
OhMyREPL.input_prompt!(input_prompt)
end
PS C:\Users\derek> julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.5.0 (2020-08-01)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia-2020-08-26T11:20:08.105>
Unfortunately, this prevents the Julia REPL from starting if package OhMyREPL.jl is not installed.
How can I use the startup example in the OhMyREPL.jl documentation with my solution to allow the Julia REPL to start when package OhMyREPL isn’t installed?
Does this work?
using Dates
input_prompt() = "julia-" * string(Dates.now()) * ">"
atreplinit() do repl
try
@eval using OhMyREPL
OhMyREPL.input_prompt!(input_prompt)
catch e
@warn "error while importing OhMyREPL" e
end
end
No, because @eval using OhMyREPL
doesn’t apparently load the module in the same context in which OhMyREPL.input_prompt!(input_prompt)
runs:
PS C:\Users\derek> julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.5.0 (2020-08-01)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
┌ Warning: Error while importing OhMyREPL
│ e =
│ MethodError: no method matching input_prompt!(::typeof(input_prompt))
│ The applicable method may be too new: running in world age 27830, while current world is 27837.
│ Closest candidates are:
│ input_prompt!(::Union{Function, String}) at C:\Users\derek\.julia\packages\OhMyREPL\j3mzy\src\prompt.jl:10 (method too new to be called from this world context.)
│ input_prompt!(::Union{Function, String}, ::Any) at C:\Users\derek\.julia\packages\OhMyREPL\j3mzy\src\prompt.jl:10 (method too new to be called from this world context.)
└ @ Main C:\Users\derek\.julia\config\startup.jl:21
julia>
This should work then:
using Dates
input_prompt() = "julia-" * string(Dates.now()) * ">"
try
using OhMyREPL
catch e
@warn e
end
atreplinit() do repl
isdefined(@__MODULE__(), :OhMyREPL) ? OhMyREPL.input_prompt!(input_prompt) : nothing
end
e.g. just separating the try
into it’s own block.
Yes, that works.
With package OhMyREPL.jl installed:
PS C:\Users\derek> julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.5.0 (2020-08-01)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia-2020-08-26T17:04:49.413>
Without package OhMyREPL.jl installed:
PS C:\Users\derek> julia -e 'using Pkg; Pkg.rm(\"OhMyREPL\");'
Updating `C:\Users\derek\.julia\environments\v1.5\Project.toml`
[5fb14364] - OhMyREPL v0.5.6
Updating `C:\Users\derek\.julia\environments\v1.5\Manifest.toml`
[a8cc5b0e] - Crayons v4.0.3
[5fb14364] - OhMyREPL v0.5.6
[0796e94c] - Tokenize v0.5.8
PS C:\Users\derek> julia
┌ Warning: ArgumentError("Package OhMyREPL not found in current path:\n- Run `import Pkg; Pkg.add(\"OhMyREPL\")` to install the OhMyREPL package.\n")
└ @ Main C:\Users\derek\.julia\config\startup.jl:19
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.5.0 (2020-08-01)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia>
I’m still curious what the author had in mind with @eval using OhMyREPL
inside the try-catch block.