IExHistory2 - improved history, save variable bindings from the IEx shell and more...

Hello,

I wrote the initial version of this library as my first Elixir project a couple of years ago, mostly to learn Elixir, but also a desire to be able to save variable bindings between IEx sessions (especially useful when debugging). When the OTP team re-wrote the shell it caused it to break. I’ve been on a recent mission to fix it and update the code.

It’s still very much beta, but folks are more than welcome to play with it.

Here are the key features:

  • Saves IEx shell history between sessions / VM restarts.
  • Saves the shell variable bindings between sessions / VM restarts.
  • Ability to paste (most) terms into the shell without a screen full of garbage (experimental).
  • Navigation keys allow history traversal where multi-line pastes require a single key up/down (instead of up-arrowing every line of whatever was pasted). Unfortunately I had to chose different keys.
  • Shortcut functions permit search, pasting, re-evaluation and editing of items in history.
  • Editing can be done in-situ or in a text editor.
  • Shell variable bindings can be set/get outside of scope of the shell to assist in code debugging.
  • Can be enabled and state shared globally, or on individual shell sessions.

https://hexdocs.pm/iex_history2/IExHistory2.html#content

I really do some dirty hacks to get the bindings and history, it would be awesome if there was an official way to do that.

10 Likes

The best way to get some official support is to open an issue at the Erlang/OTP github and describe what functionality you would like. Maybe we can design something together that would benefit everyone trying to build fancy shell things.

10 Likes

Tried it out, great stuff. :+1:
One note, readme reads:

Alternatively the project can be added as a Mix dependancy.

It’s a little confusing because I needed to add it as dependency, otherwise it wouldn’t work, I got:

Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.16.0) - press Ctrl+C to exit (type h() ENTER for help)
Error while evaluating: /home/ken/asher/.iex.exs
** (UndefinedFunctionError) function IExHistory2.initialize/0 is undefined (module IExHistory2 is not available)
    IExHistory2.initialize()
    /home/ken/asher/.iex.exs:2: (file)

So readme maybe could read:

Installation

Add :iex_history2 to the list of dependencies in `mix.exs``:

def deps do
  [
    {:iex_history2, "~> 5.1"}
  ]
end

Enable IExHistory2 in .iex.exs:

Code.append_path("~/github/iex_history2/_build/dev/lib/iex_history2/ebin")
IExHistory2.initialize()

I tried to open a PR for this but I get remote: Permission to nhpip/iex_history2.git denied to ken-kost. :disguised_face:

1 Like

Hi,

I just updated the code to allow it to be included in mix as part of another application.

Let me know it works now

Thanks

1 Like

Definitely, let me think it through. The shell is great for 90% of what I need. This project all started because I wanted to save shell variable bindings between VM restarts. It then grew from there.

I don’t want to add work to the core OTP team that only I would ever use, but something like being able to act as a shim between user_drv and group and also between group_leader and (in the case of Elixir) iex_server.

The shim could then just snoop messages between those processes, block them or modify them.

So as an example of something I just added. If I see a certain struct getting evaluated I intercept a couple of fields and modify them with extra data. So since I rely on erlang.trace I do something like:

 def handle_info({:trace, _, :send, {:eval, _, data, _, _}, shell_pid}, process_info) do
    case Map.get(process_info, shell_pid) do
      %{pending_command: pending_command} = shell_config ->
        updated_command = modify_input(data, pending_command)
        {:noreply, %{process_info | shell_pid => %{shell_config | pending_command: updated_command}}}

Of course this has a couple of problems:

  1. I can send the new payload to the evaluator, but the original data is still going to be evaluated.
  2. Any changes to the message signature will break the code.

I am more than happy for alternative ideas :slight_smile:

Thanks

1 Like

Maybe a behaviour?

If you want to insert yourself between iex_server and group (i.e. the group_leader of iex_server, then you will have to get a hook into iex_server. The good thing here is that the protocol used between iex_server and group is documented so it should be relatively easy to insert something there if the Elixir team wants to allow it.

The protocol inbetween user_drv and group is however not documented and thus we cannot just add something that that snoops inbetween there. What exactly is it that you would like to do there?

In this example it seems like you would like to change the behaviour of the shell evaluator. Maybe it is possible to achieve by using a custom iex parser? If not, then that is yet another place in iex_server to insert yourself in. We don’t allow doing that in Erlang either, but I could see us adding such a feature if it was requested.

3 Likes

Thanks, I’ll look into the custom parser

The custom parser really helped clean up things. Thanks for the suggestion.

New version available:

4 Likes