# How to robustly interact with the REPL from outside a Julia process?

**URL:** <https://discourse.julialang.org/t/how-to-robustly-interact-with-the-repl-from-outside-a-julia-process/122623>\
**Category:** General Usage\
**Created:** [November 14, 2024, 9:50am UTC](https://discourse.julialang.org/t/how-to-robustly-interact-with-the-repl-from-outside-a-julia-process/122623 "2024-11-14T09:50:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 14, 2024, 9:50am UTC](https://discourse.julialang.org/t/how-to-robustly-interact-with-the-repl-from-outside-a-julia-process/122623/1 "2024-11-14T09:50:18Z")

</div>

I want to make automated recordings of REPL activity with `asciinema`. Normally one would start `julia` via `asciinema`, interact with the REPL and then exit. But I want to write out all pressed keys programmatically. My first problem is that if I had an open `julia` process, I could maybe write to its stdin to simulate interaction but I don’t really know when one REPL command is done. I don’t want to send all input at once as it’s supposed to look like a human does it. I don’t want to resort to hacks like checking the stdout for strings like `julia>` to determine if the REPL is ready for input. Can I set up a fake REPL that I can use in a worker process, for example with Malt.jl, that I can send input to and check when it returns so I can send the next command? The visual outputs of this setup should be the same as if equivalent keystrokes were sent in a normal Julia REPL session.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [November 14, 2024, 10:50am UTC](https://discourse.julialang.org/t/how-to-robustly-interact-with-the-repl-from-outside-a-julia-process/122623/2 "2024-11-14T10:50:52Z")

</div>

> [@jules](#):
>
> I want to make automated recordings of REPL activity with `asciinema`. Normally one would start `julia` via `asciinema`, interact with the REPL and then exit. But I want to write out all pressed keys programmatically.

This is only a partial solution to your request, but I once did a similar thing, using [GNU Screen](https://www.gnu.org/software/screen/) to programmatically send keystrokes to the process I wanted to record (Emacs, in my case) within asciinema.

In your case, the gist of it would be to start the Julia REPL in a named screen session:

```shell
shell$ screen -S my_julia_session julia

```

and then from anywhere else, you can send keystrokes to it via a screen’s [`stuff`](https://www.gnu.org/software/screen/manual/html_node/Paste.html) command:

```shell
shell$ screen -S my_julia_session -p 0 -X stuff "1+1\n"
shell$ sleep 1
shell$ screen -S my_julia_session -p 0 -X stuff "^D"

```

With such a technique, you can do fancier things like reading key presses from a file and sending them one by one (with as many `screen -X stuff` commands), possibly with a small delay between them to simulate human interaction.

However, this technique doesn’t give you any feedback from the julia process, so you can’t know when the REPL is ready. I don’t know of any way to get that information, but hopefully someone will chime in with a more elegant and complete solution.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 14, 2024, 11:18am UTC](https://discourse.julialang.org/t/how-to-robustly-interact-with-the-repl-from-outside-a-julia-process/122623/3 "2024-11-14T11:18:03Z")

</div>

You could try my [GitHub - ericphanson/Asciicast.jl: Easy REPL animations in READMEs, Documenter docs, and more!](https://github.com/ericphanson/Asciicast.jl)

(it does not write the input letter-by-letter though…)

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [November 14, 2024, 11:21am UTC](https://discourse.julialang.org/t/how-to-robustly-interact-with-the-repl-from-outside-a-julia-process/122623/4 "2024-11-14T11:21:53Z")

</div>

The linux utility `expect` (tcl based, from 1990 or so) is made for this kind of programmatic interactions.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 14, 2024, 2:28pm UTC](https://discourse.julialang.org/t/how-to-robustly-interact-with-the-repl-from-outside-a-julia-process/122623/5 "2024-11-14T14:28:59Z")

</div>

Huh interesting that I didn’t find that when I googled around a bit first! I guess this wouldn’t handle REPL modes like `;` or `]` though right? For docs it would sometimes be nice to show those as well. But this is already 90% of what I wanted to do I think, even the html output with inline data that I was thinking about… 😃

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [November 14, 2024, 2:47pm UTC](https://discourse.julialang.org/t/how-to-robustly-interact-with-the-repl-from-outside-a-julia-process/122623/6 "2024-11-14T14:47:12Z")

</div>

No, I don’t think pkg mode works:

````julia
julia> using Asciicast

julia> cast"""
       pkg> st
       """
ERROR: LoadError: CastExecutionException: Failed to run `cast` block:
```
pkg> st

```
ERROR: UndefVarError: `pkg` not defined in `Main. ____ 230`

````

It works the same way as Documenter’s `@repl` blocks (using their tech to do so), maybe support could be added there. Though I have my own [copy](https://github.com/ericphanson/Asciicast.jl/blob/579da86cf7eae8aa7602046292141cf910eb5b1a/src/runner.jl#L9-L11) of the `@repl` implementation in order to add the right timing info.
