ESC like a Vim


Using ESC in the Curses::UI data viewer.

The Curses::UI data viewer has three main sections:

  • the top one allows running a command that is supposed to print out JSON-encoded data (an array of objects or an object);
  • the middle area allows selecting stuff inside the data itself, to be shown in…
  • the bottom area contains the YAML dump of the selected part of the data.

It’s possible to focus on the three sections. When the program starts, it’s automatically set in the top entry because there’s no data to show (yet).

When there is something to show, the main area is the middle one, to allow selecting the right part. It’s possible to go back to the command area with :, as well as move to the bottom text viewer with ..

To go back to the middle area, I wanted to use the ESC button, because it’s so Vimsy. Except for two bumps in the road:

  • the Curses distribution does not include a KEY_ESC constant for getting an ESC, and

  • the Curses distribution uses the escape octet to “generate” the keys.

In my installation, it turned out that with some little patience it’s possible to bind to the ESC by using its ASCII code 0x1B:

$entry->set_binding(sub { $self->focus_selectors }, "\x1b");
$viewer->set_binding(sub { $self->focus_selectors }, KEY_ENTER, "\x1b");

The patience thing stems from the fact that hitting the ESC key puts the Curses library in a small hold time to see what comes next. If nothing comes, the ESC key (0x1B) is emitted and the binding is honored.

I’ve done a couple of tests and the delay is there but definitely OK, especially considering that moving from the botton to the middle parts can also be requested with the ENTER key, which does not suffer from the delay.

Stay safe!

Comments? Octodon, , GitHub, Reddit, or drop me a line!