rakitzis / rc

rc shell -- independent re-implementation for Unix of the Plan 9 shell (from circa 1992)
Other
250 stars 23 forks source link

Support bestline for line editing #100

Closed MaxGyver83 closed 2 months ago

MaxGyver83 commented 2 months ago

Support bestline for line editing

bestline is a small line editing library (roughly 3000 lines of code) based on linenoise.

Bestline is a fork of linenoise (a popular readline alternative) that fixes its bugs and adds the missing features while reducing binary footprint (surprisingly) by removing bloated dependencies, which means you can finally have a permissively-licensed command prompt w/ a 38kb footprint that's nearly as good as gnu readline.

Installation

First, install a patched version of bestline as dynamic library:

git clone -b lib https://github.com/MaxGyver83/bestline.git
cd bestline
make libbestline.so
sudo cp libbestline.so /usr/local/lib/
sudo cp bestline.h /usr/local/include/

This patch adds a position argument to bestline's completion function. The cursor position is necessary to decide which part of the current command is to be completed. The cursor position is updated for completion that don't happen at the end of a line. It also adds a make target for building a dynamic library. (In integrate Antirez's linenoise single file zero conf line editing by rlonstein · Pull Request #5 · rakitzis/rc, it was suggested to provide linenoise as a standalone library.) I'll open a pull request with these changes to bestline.

Then, build rc with bestline support:

cd /path/to/rc
make EDIT=bestline

Features

Bestline states to be "nearly as good as gnu readline".

Some differences compared to readline:

Bonus in this integration:

rakitzis commented 2 months ago

Hi! Thanks for the contribution! In general, these edit libraries are "best effort" and I'm happy to add a new one. I'm curious if there is any automated testing that could be included with this PR. I'm happy to merge without, but I wonder how you tested the library integration.

MaxGyver83 commented 2 months ago

Yes, I have written some unit tests. They depend on mity/acutest: Simple header-only C/C++ unit testing facility. I guess you don't want to include acutest.h in the rc repository. I could omit this file and leave it to future developers/testers to download it themselves. Or I could remove this dependency and use assert's instead. (No report in this case.) In any case, I need to clean up the tests to make them work everywhere. (For example the command completion depends on the commands available on my laptop.)

MaxGyver83 commented 2 months ago

I have fixed a bug in edit-bestline.c that showed up when I cleaned-up the unit tests. And I have added the unit tests in a separate commit. They can be executed via make run-test-bestline. This will download the lastest version of acutest.h.

rakitzis commented 2 months ago

That's great, thank you!