Examples

Basics

Basic example in the README

Script in /home/runner/work/Replay.jl/Replay.jl/examples/readme/app.jl:

using Replay

repl_script = """
2+2
print("")
display([1])
display([1 2; 3 4])
@time 1+1
using Pkg; Pkg.activate(".")
]add Example
rm Example
st
$CTRL_C
"""

replay(
    repl_script,
    stdout,
    julia_project=@__DIR__,
    use_ghostwriter=true,
    cmd=`--color=yes`,
)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/readme/app.jl")'

The output will be like this:

Hello world

Script in /home/runner/work/Replay.jl/Replay.jl/examples/helloworld/app.jl:

using Replay

instructions = [
    """
    function greet(msg::String="Hello!")
        println(msg)
    end
    """,
    "greet()",
    """greet("Hello, World!")""",
]

replay(instructions, use_ghostwriter=true)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/helloworld/app.jl")'

The output will be like this:

@deparse macro

Script in /home/runner/work/Replay.jl/Replay.jl/examples/deparse_macro/app.jl:

using Replay

i1 = @deparse println("Hello World")
i2 = @deparse using LinearAlgebra
i3 = @deparse begin
    x = [1, 1]
    A = [1 0; 0 2]
end
i4 = @deparse @show dot(x, A, x)
i5 = @deparse function f(x)
    @comment This is a comment
    if x > 0
        @comment x is larger than 0
        @info "x is larger than 0"
    end
    @info "compute 2x + 2"
    2x + 2
end
i6 = @deparse x = 3
i7 = @deparse f(x)

replay([i1, i2, i3, i4, i5, i6, i7], use_ghostwriter=true)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/deparse_macro/app.jl")'

The output will be like this:

More in REPL

Help mode

Script in /home/runner/work/Replay.jl/Replay.jl/examples/helpmode/app.jl:

using Replay

instructions = [
    "# Help mode",
    "?1",
    "?ℯ",
]
replay(instructions, use_ghostwriter=true)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/helpmode/app.jl")'

The output will be like this:

Package mode

Script in /home/runner/work/Replay.jl/Replay.jl/examples/pkgmode/app.jl:

using Replay

instructions = [
    "# Pkg mode",
    "]st",
]

replay(instructions, use_ghostwriter=true)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/pkgmode/app.jl")'

The output will be like this:

Shell mode

Script in /home/runner/work/Replay.jl/Replay.jl/examples/shellmode/app.jl:

using Replay

instructions = [
    ";echo hello",
    CTRL_C,
    "println(\"Hello\")",
]

replay(instructions, use_ghostwriter=true)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/shellmode/app.jl")'

The output will be like this:

Tab completion

Script in /home/runner/work/Replay.jl/Replay.jl/examples/tab_completion/app.jl:

using Replay

instructions = [
    """\"\"\"
    The tab key can also be used to substitute
    LaTeX math symbols with their Unicode equivalents, 
    and get a list of LaTeX matches as well:
    \"\"\";
    """,
    "# Example:",
    "# \\sigma\\<TAB> で σ が出るよ.",
    "# <TAB> はタブキーを入力すると解釈してね",
    "# \\sigma<TAB>\\_i<TAB>\\^(j-1)<TAB> = 1 で",
    "# σᵢ⁽ʲ⁻¹⁾ = 1 が出るよ",
    "\\sigma$(TAB)\\_i$(TAB)\\^(j-1)$(TAB) = 1",
]
replay(instructions, use_ghostwriter=true)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/tab_completion/app.jl")'

The output will be like this:

CLI options

Disable color

Script in /home/runner/work/Replay.jl/Replay.jl/examples/disable_color/app.jl:

using Replay

@assert !Base.get_have_color() "please run julia with --color=no e.g. `julia --color=no examples/disable_color/app.jl`"

instructions = [
    """
    function greet(msg::String="Hello!")
        println(msg)
    end
    """,
    "greet()",
    """greet("Hello, World!")""",
]

replay(instructions, use_ghostwriter=true, cmd=`--color=no`)

Replay the script in the REPL:

julia --project=. --color=no -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/disable_color/app.jl")'

The output will be like this:

Quiet mode

Script in /home/runner/work/Replay.jl/Replay.jl/examples/quietmode/app.jl:

using Replay

instructions = """
println("julia -q")
"""

replay(instructions, cmd=`-q`)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/quietmode/app.jl")'

The output will be like this:

Working with other packages

OhMyREPL.jl

Script in /home/runner/work/Replay.jl/Replay.jl/examples/ohmyrepl/app.jl:

using Replay

instructions = raw"""
using OhMyREPL
using Printf
println("HelloWorld")
N = 10
@printf "%.2f %.2f %.2f\n" sin(pi/4) cos(pi/4) tan(pi/4)
@show N
"""

replay(instructions, use_ghostwriter=true, julia_project=@__DIR__)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/ohmyrepl/app.jl")'

The output will be like this:

UnicodeFun.jl

Script in /home/runner/work/Replay.jl/Replay.jl/examples/unicodefun/app.jl:

using Replay

instructions = ["using UnicodeFun", "\"\\\\pi\" |> to_latex"]

replay(instructions, julia_project=@__DIR__)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/unicodefun/app.jl")'

The output will be like this:

PythonCall.jl

Script in /home/runner/work/Replay.jl/Replay.jl/examples/pythoncall/app.jl:

using Replay

repl_script = """
using PythonCall
jlv = [1,2,3]
pyv = Py(jlv)
pyv.append(4)
pyv
jlv
"""

replay(
    repl_script,
    stdout,
    julia_project=@__DIR__,
    use_ghostwriter=true,
    cmd=`--color=yes`,
)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/pythoncall/app.jl")'

The output will be like this:

UnicodePlots.jl

Script in /home/runner/work/Replay.jl/Replay.jl/examples/unicodeplots/app.jl:

using Replay

instructions = """
using UnicodePlots
histogram(randn(1000) .* 0.1, nbins = 15, closed = :right, xscale=:log10)
heatmap(collect(0:30) * collect(0:30)', xfact=0.1, yfact=0.1, xoffset=-1.5, colormap=:inferno)
"""

replay(instructions, julia_project=@__DIR__)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/unicodeplots/app.jl")'

The output will be like this:

UnicodePlots.jl (animated)

Script in /home/runner/work/Replay.jl/Replay.jl/examples/unicodeplots_animated/app.jl:

using Replay

lemma = joinpath(@__DIR__, "lemma.jl")

instructions = [
    "using UnicodePlots",
    raw"""
    function clearline(; move_up::Bool = false)
        buf = IOBuffer()
        print(buf, "\x1b[2K") # clear line
        print(buf, "\x1b[999D") # rollback the cursor
        move_up && print(buf, "\x1b[1A") # move up
        print(buf |> take! |> String)
    end
    """,
    raw"""
    function clearlines(H::Integer)
        for i = 1:H
            clearline(move_up = true)
        end
    end
    """,
    raw"""
    function animate(frames::Vector{<:UnicodePlots.Plot}; duration::Float64 = 0.5)
        print("\x1b[?25l") # hidecursor
        for (n, f) in enumerate(frames)
            print(f)
            str = string(f)
            if n != length(frames)
                sleep(duration)
                clearlines(length(collect(eachmatch(r"\n", str))))
            end
        end
        print("\u001B[?25h") # unhide cursor
    end
    """,
    "frames = [lineplot(x -> sin(x - t), width = 40) for t = -2π:0.25π:2π];",
    "animate(frames)",
]

replay(instructions; julia_project=@__DIR__, use_ghostwriter=false)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/unicodeplots_animated/app.jl")'

The output will be like this:

ImageInTerminal.jl

Script in /home/runner/work/Replay.jl/Replay.jl/examples/imageinterminal/app.jl:

using Replay

instructions = """
using TestImages
using ImageInTerminal
testimage("mandril_color")
"""

replay(instructions, julia_project=@__DIR__)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/imageinterminal/app.jl")'

The output will be like this:

Sixel.jl

Script in /home/runner/work/Replay.jl/Replay.jl/examples/sixel/app.jl:

using Replay

instructions = """
using TestImages
using Sixel
sixel_encode(testimage("mandril_color"))
"""

replay(instructions, julia_project=@__DIR__)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/sixel/app.jl")'

Currently, we don't have output for this example because asciinema does not supprot SIXEL. Please try it on your envionment!

Plots with Sixel.jl

Script in /home/runner/work/Replay.jl/Replay.jl/examples/plots_with_sixel/app.jl:

using Replay

instructions = """
using FileIO, Sixel, Plots
gr()
buf = IOBuffer()
show(buf, MIME("image/png"), plot(sin, size=(1000, 750)))
buf |> load |> sixel_encode
"""

replay(instructions, julia_project=@__DIR__)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/plots_with_sixel/app.jl")'

Currently, we don't have output for this example because asciinema does not supprot SIXEL. Please try it on your envionment!

RDatasets.jl and more (Iris dataset)

Script in /home/runner/work/Replay.jl/Replay.jl/examples/iris/app.jl:

using Replay

instructions = raw"""
using OhMyREPL

using LinearAlgebra
using Printf
using Statistics

using DataFrames
using RDatasets
using StatsPlots

# use UnicodePlots.jl as backend
unicodeplots();

iris = dataset("datasets", "iris");
@show names(iris);
N = size(iris, 1)
@show names(iris);
# Execute PCA
X = Matrix(select(iris, Not(:Species)))
X .= X .- mean(X, dims=1)
Σ = (X' * X)/N # will be the covariance matrix for iris dataset
e = eigen(Σ, sortby = -);
# display  eigen values in descending order
e.values
# eigen vectors
e.vectors

@printf "1st contribution ratio = %.2f percent\n" 100e.values[1]/sum(e.values)
@printf "2nd contribution ratio = %.2f percent\n" 100e.values[2]/sum(e.values)

# Visualize the reduced data via PCA
topk = 2;
reduced_data = DataFrame(X * e.vectors[:, 1:topk], [:X, :Y]);
@df reduced_data scatter(:X, :Y, group=iris.Species)
"""

replay(instructions, use_ghostwriter=true, julia_project=@__DIR__)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/iris/app.jl")'

The output will be like this:

Have fun!

Why We Created Julia (with use_ghostwriter)

Script in /home/runner/work/Replay.jl/Replay.jl/examples/use_ghostwriter/app.jl:

using Replay

instructions = """
"# Why We Created Julia";
"# We are greedy: we want more.)";
"# We want a language that's open source, with a liberal license.";
"# We want the speed of C with the dynamism of Ruby.";
"# We want a language that's homoiconic with true macros like Lisp, ";
"# but with obvious, familiar mathematical notation like Matlab.";
"# We want something as usable for general programming as Python, ";
"# as easy for statistics as R, ";
"# as natural for string processing as Perl, ";
"# as powerful for linear algebra as Matlab, ";
"# as good at gluing programs together as the shell.";
"# Something that is dirt simple to learn, ";
"# yet keeps the most serious hackers happy.";
"# We want it interactive and we want it compiled.)";
"(Did we mention it should be as fast as C?))";
"""

replay(instructions, use_ghostwriter=true)

Replay the script in the REPL:

julia --project=. -e 'using Replay; include("/home/runner/work/Replay.jl/Replay.jl/examples/use_ghostwriter/app.jl")'

The output will be like this: