Linenoise
Description
A minimal, zero-config, BSD licensed, readline replacement.
From the author's readme:
Line editing with some support for history is a really important feature for command line utilities. Instead of retyping almost the same stuff again and again it's just much better to hit the up arrow and edit on syntax errors, or in order to try a slightly different command. But apparently code dealing with terminals is some sort of Black Magic: readline is 30k lines of code, libedit 20k. Is it reasonable to link small utilities to huge libraries just to get a minimal support for line editing?
(...)
- Linux text only console ($TERM = linux)
- Linux KDE terminal application ($TERM = xterm)
- Linux xterm ($TERM = xterm)
- Mac OS X iTerm ($TERM = xterm)
- Mac OS X default Terminal.app ($TERM = xterm)
- OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen)
- IBM AIX 6.1
- FreeBSD xterm ($TERM = xterm)
This information has been collected on 2010-06-13.
Author
Salvatore Sanfilippo <antirez@gmail.com>, ported to chicken by Christian Kellermann
Repository
This egg is hosted on the CHICKEN Subversion repository:
https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/linenoise
If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.
Interface
The library is very small and exposes six procedures:
[procedure] (linenoise prompt)Prompts the user for a line of input. Returns the entered line as a string or #f if the user aborted.
[procedure] (history-add line)Adds the string line to the internal history.
[procedure] (set-history-length! number)Sets the number of maximum entries in the history. Linenoise is initialised with 100 lines of history.
[procedure] (save-history-to-file filename)Saves the current history buffer to filename.
[procedure] (load-history-from-file filename)Loads the history from file.
[procedure] (make-linenoise-port)Returns an input port connected to a linenoise prompt. Useful for inserting it into CSI (see below).
Controls
- arrow keys move the cursor, as well as ctrl-b, ctrl-f (back and forward), ctrl-p and ctrl-n (up / down)
- ctrl-d and enter will finish the input and a string containing the current line will be returned.
- ctrl-c aborts input and the linenoise procedure will return #f
- Deletion of one character is done by pressing backspace or ctrl-h
- ctrl-u deletes the whole line
- ctrl-k deletes from point (the cursor) to the end of the line
- ctrl-a moves to the beginning of the line
- ctrl-e moves to the end of the line
- ctrl-t permutes the char after the cursor with the one under the cursor
Currently missing features
- No searching of history
- No deletion of whole words (ctrl-w)
- No tab completion
Example
This example shows a minimal prompt loop echoing all user input:
(use linenoise) (set-history-length! 300) (load-history-from-file ".linenoise-history") (let loop ((l (linenoise "> "))) (cond ((equal? l "bye") (save-history-to-file ".linenoise-history") "Bye!") (else (display l) (newline) (history-add l) (loop (linenoise "> ")))))
Using linenoise in csi
Since version 0.4 linenoise can be used as editing help in csi, simply add the following to your .csirc
(import linenoise) (current-input-port (make-linenoise-port))
Changelog
- 1.0 First chicken 5 port, identical to version 0.7 for chicken 4
License
Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com> Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com> All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.