Skip to content

Conversation

@MaxGyver83
Copy link
Contributor

@MaxGyver83 MaxGyver83 commented Jun 17, 2024

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/

UPDATE (for people finding this page): Since these changes (jart/bestline#33 and jart/bestline#34) have been merged into bestline's master branch, you don't need my patched version anymore. Just clone https://github.com/jart/bestline.git instead.

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:

  • No insertion of the last argument of the previous command (Alt+. in readline).
  • Up arrow doesn't consider typed characters (history-search-backward). Use Ctrl+r instead.
  • After deleting two words with Ctrl+w w, Ctrl+y inserts only the first word (with readline it inserts both words).
  • Tab completion iterates over possible completions but doesn't print a list.

Bonus in this integration:

  • Filename completion expands ~/dir to $HOME/dir.

@rakitzis
Copy link
Owner

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
Copy link
Contributor Author

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.)

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/

Then, build rc with bestline support:

cd /path/to/rc
make EDIT=bestline
The unit tests depend on acutest.h. This header file isn't included in
this repository but it will be downloaded when the new Makefile target
'run-test-bestline' is run:

make run-test-bestline
@MaxGyver83
Copy link
Contributor Author

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
Copy link
Owner

That's great, thank you!

@rakitzis rakitzis merged commit 26abfcd into rakitzis:master Jun 19, 2024
@MaxGyver83 MaxGyver83 deleted the bestline branch June 28, 2024 19:19
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