peterh / liner

Pure Go line editor with history, inspired by linenoise
MIT License
1.05k stars 132 forks source link

Searhcing history (ctrlR) #13

Closed rif closed 10 years ago

rif commented 10 years ago

Another (last) feature that I think it's missing from liner si searching the history. Try ctrl+r in bash or zsh to test it, can you think of a simple way to get it working?

-rif

peterh commented 10 years ago

Sorry, I can't think of a simple way to get it working. Step 1 is to fully understand the way bash's Ctrl-R works, and I haven't taken the time to do that yet. Most of the time, if the first hit on Ctrl-R isn't the one I want, I get confused. ;-)

Liner is intended to be simple, which is why I haven't researched Ctrl-R any further. That said, if you can find a simple way to implement Ctrl-R (both in code, and in the user's ability to understand what it's doing), I'd be willing to consider a pull request.

rif commented 10 years ago

Can live without it :)

ElPincheTopo commented 10 years ago

Ctrl-R search is awesome and pretty simple to implement. Basically you have 2 states: In state one when Ctrl-R is pressed, you have to change the prompt to the search prompt and enter state 2. In state 2 you just have to store, but not display the chars that the user is typing, and for each character pressed reverse-search in the history for the lines that match the input string and display it to the right of the prompt as if it was text entered by the user. In this state, pressing Ctrl-R again gives you the next matching item on the history (in reversed order). Pressing enter returns to state 1, but keeps the "found" line as if the user just typed it. Pressing ←, → or any combination of keys that moves the cursor, does the same as enter and then moves the cursor to the corresponding place depending on the pressed key combination.

This web page explain the basic usage: http://ruslanspivak.com/2010/11/20/bash-history-reverse-intelligent-search/

And this one has the gnu documentation about the usage: http://www.gnu.org/software/bash/manual/html_node/Commands-For-History.html

I think this is a MUST have for this library, it one of the key features of readline.

peterh commented 10 years ago

Thank you for volunteering to write Ctrl-R support, @ElPincheTopo. I look forward to reviewing your pull request.