EikaShell: one function to read a line, and an editor behind it
From the shell’s perspective, reading a command is still one function call. It asks for the next input line, gets a string, passes that string to the parser and carries on with its day. I find this immensely satisfying because the thing behind that one function has grown into a fairly serious interactive line editor, with its own cursor state, history, completion sessions, undo/redo, a kill ring and a renderer that has to negotiate with the terminal about where anything is allowed to appear.
It began as a university operating-systems assignment. I had the required process-execution parts working in something like 300 lines, then discovered that typing out filenames for every demonstration of cat file was irritating, so I added Tab completion. I have a blog post about the consequences of that decision and several other things I probably should have left alone. This page is for the machinery I eventually ended up with.
Where the line goes
The shell has a parser, builtins, pipelines, redirection and the usual Unix process-execution logic. Stateful commands like cd run in the shell process; other commands can go through the child-process path with the appropriate file descriptors. There’s plenty of shell behaviour still missing, and I have ideas for a scripting language somewhere in the future, but the input subsystem has ended up being the part I most enjoy working on.
Here’s the arrangement, roughly:
shell loop
|
v
read next command -> InputResult / string
|
v
interactive input session -> history, completion, undo, kill ring
|
v
editor state -> buffer, cursor, selection, movement
|
v
key events -> decoded terminal bytes and controls
session + editor state ---> prompt/panel renderer ---> terminal
The input decoder has to deal with the actual byte stream coming from a terminal in a raw-ish mode: ordinary characters, control keys, escape sequences, bracketed paste, all the other delightful things a terminal might send. It hands up events. The editor understands the line, cursor movement, and changes to the buffer. A session adds interactions that extend beyond one event, like history search or a completion panel. Rendering gets whatever the current state is and works out how to display it.
The boundaries matter because none of the upper layers should have to know how the lower layer got into its current state. The parser sees a command string. The session can ask the editor to move the cursor without knowing which ANSI sequence was sent by the arrow key. The renderer can draw the completion menu without completion having to remember how many terminal rows the prompt occupied. The result is a reasonably tidy interface around code that went through quite a few less tidy versions.
The input-system documentation goes into the details, including the state ownership and editing operations.
The Tab window
I love the completion menu. It’s a selectable, scrolling window which appears beneath the prompt, shows matching commands or paths, and lets you move through candidates while the input line previews what accepting them would look like. It feels like the smallest possible feature to be proud of, especially when you remember that people have been doing tab completion for ages, but I spent so much time getting the interaction right that I can’t help it.
The main complication is that looking at a candidate isn’t the same action as inserting it. Imagine typing part of a path, opening completion, moving down ten options and then hitting Escape. The editor should look exactly as it did before you opened the menu, including the cursor position and undo history. If those ten previews were implemented as ten ordinary buffer edits, undo becomes a record of things you considered doing. That’s maddening.
A completion session therefore keeps an anchor: the original input and cursor state. Each candidate preview is computed against that same anchor. Scrolling around doesn’t accumulate modifications, cancellation restores the anchor, and accepting creates one actual edit which can be undone. This also makes the user’s intentions easier to reason about. Selecting an option is an interaction with the menu. Accepting it is a change to the command.
The undo manager itself stores snapshots of the relevant editing state and groups sensible runs of typing, deletion or paste. History navigation and incremental search need to remember the input you were working on before you went looking through old commands. The kill ring has rules about consecutive kills and what yank-pop is allowed to replace. They all meet in the same tiny line of text, which is why I ended up caring so much about who owned which part of the state.
Then there’s drawing the whole thing. ANSI colour codes don’t occupy visual columns, a prompt can be longer than one terminal row, input can wrap, the completion window needs a scroll position, and after redrawing everything the cursor should return to where the user was editing rather than remain at the bottom of the candidates. Terminal resize events are very good at revealing assumptions you didn’t know you were making. This is where separating the panel’s contents from the actual terminal renderer finally felt worth all the rewrites.
For now
I use EikaShell and it does the things I wanted it to do. There’s a fairly long list of things I could add, including that scripting language I keep threatening to write in 2027, but right now I’ve got other projects taking up most of my time. I like being able to leave this one alone for a bit and come back when I have another unnecessarily specific idea.
The source repository has the implementation, and the input notes are the place to go if you’re interested in the actual abstractions. The blog post is considerably less orderly and involves a friend who set an experimental shell as his actual Fedora login shell. I’m still rather pleased the line editor survived long enough to be somebody’s terrible idea of a daily driver.