Skip to content

Add support for command line editing#93

Merged
feelfree69 merged 2 commits intomainfrom
cmdbuf
Feb 25, 2026
Merged

Add support for command line editing#93
feelfree69 merged 2 commits intomainfrom
cmdbuf

Conversation

@logicog
Copy link
Owner

@logicog logicog commented Jan 23, 2026

The current code for entering commands via the serial CLI is completely refactored and moved out of rtlplayground.c into a separate file cmd_editor.c putting code into BANK2.
The serial ring buffer sbuf[] is now exclusively used for receiving keys, including escape sequences (DEL will generate a 4-byte escape sequence). The command is now held in cmd_buffer. This reduces XMEM use, because the serial buffer can be much smaller.

Supported is only editing the current command line via backspace, del, cursor left and right.

Because the code cannot distinguish escape sequences which are not yet complete (because the serial isr has not yet put all characters into the serial buffer) from the dozens of unsupported ones (F-keys/home, Page up/down, cursor up/down...) they are ignored and remain in the serial buffer until the ring-pointer overwrites them. This is standard behaviour for consoles, which will print out garbage for unsupported characters. In this implementation, the command line buffer and the visible command line in the terminal are always synced, so garbage can be removed again by ediing the line.

The code makes several redundant checks in order to prevent race-conditions between the serial ISR and the comand-editing code.

@logicog logicog requested a review from vDorst January 23, 2026 07:17
@feelfree69
Copy link
Collaborator

feelfree69 commented Jan 24, 2026

Forget about my initial comment here. Tried accidentally with an old FW.

Works as expected.

Really cool would be to use cursor up to recall the last cmd from history.

@logicog
Copy link
Owner Author

logicog commented Jan 24, 2026

Really cool would be to use cursor up to recall the last cmd from history.

Yes, I know 😁
Editing commands from the history is going to come in the next step. I even have a concept for that already in mind, but it is always good to release early and often as they say. This is actually a bigger code change than it may look like from the point of the added functionality. The code for the command entering is drastically refactored, and that turned out to be vastly more difficult than I had thought: It took days of work to get rid of all the race conditions between what the serial interrupt routine puts into the serial buffer, namely multi-byte escape sequences, and what is taken out by the editor. So the concept for this needs to be really right, before we can add access to the command history.

In order to get this working, I had to first learn how command editing on serial consoles actually works, by intercepting what is sent back and forth if you connect a USB-UART cable to a Raspberry Pi Zero...

@logicog
Copy link
Owner Author

logicog commented Jan 25, 2026

I added support for also editing using the command history as requested. This means that cursor-up and cursor-down allows to go through the history of previous commands.

@logicog
Copy link
Owner Author

logicog commented Jan 25, 2026

Rebased to latest main because of merge conflicts.

@logicog
Copy link
Owner Author

logicog commented Feb 9, 2026

Rebased to latest main.

The current code for entering commands via the serial CLI is completely
refactored and moved out of rtlplayground.c into a separate file cmd_edit.c
putting code into BANK2.
The serial ring buffer sbuf[] is now exclusively used for receiving
keys, including escape sequences (DEL will generate a 4-byte escape
sequence). The command is now held in cmd_buffer. This reduces
XMEM use, because the serial buffer can be much smaller.

Supported is only editing the current command line via backspace, del,
cursor left and right.

Because the code cannot distinguish escape sequences which are not yet
complete (because the serial isr has not yet put all characters into
the serial buffer) from the dozens of unsupported ones (F-keys/home,
Page up/down, cursor up/down...) they are ignored and remain in the
serial buffer until the ring-pointer overwrites them. This is standard
behaviour for consoles, which will print out garbage for unsupported
characters. In this implementation, the command line buffer and the
visible command line in the terminal are always synced, so garbage
can be removed again by ediing the line.

The code makes several redundant checks in order to prevent race-conditions
between the serial ISR and the comand-editing code.
@logicog
Copy link
Owner Author

logicog commented Feb 24, 2026

I had to fix clearing of the command line history buffer because 5662285 left it un-initialised. I also rebased to the latest main and updated the PR.

Could someone give it a try so that it can be merged? This features is very useful when working with the CLI as it allows bash-style command line history and command-line editing.

@feelfree69
Copy link
Collaborator

Very cool feature, works without issues!

@logicog
Copy link
Owner Author

logicog commented Feb 25, 2026

Very cool feature, works without issues!

If you could also have a look at the code? I am not known to write very good quality code and 4 eyes are always better. If you then like it, you can also merge. It is kind of bad taste to merge your own PRs.

@feelfree69
Copy link
Collaborator

Well, the new code is not easy to understand and not very well documented. But hey, it's a hobby project here! I'm not applying professional measures, also I don't need to understand every line.

So I'm fine with merging that!

@feelfree69 feelfree69 merged commit e26a9e3 into main Feb 25, 2026
2 checks passed
@logicog
Copy link
Owner Author

logicog commented Feb 25, 2026

Thanks so much!
With regards to comments in the code, I try to restrict them to why something is done and not what (a learning from submissions to Linux where systematically requests were done to remove comments), and I understand that here it would have been nice to have explanations what the code does as it is rather complicated arithmetic in the buffers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants