nhpip

nhpip

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.

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.

First 8 of 8 Posts Switch mode

garazdawi

garazdawi

Erlang Core Team

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
Post #1
ken-kost

ken-kost

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:

nhpip

nhpip OP

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

nhpip

nhpip OP

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

nhpip

nhpip OP

Maybe a behaviour?

garazdawi

garazdawi

Erlang Core Team

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.

nhpip

nhpip OP

Thanks, I’ll look into the custom parser

nhpip

nhpip OP

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

New version available:

— All posts loaded —

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 14960 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement