Git Product home page Git Product logo

lua-linenoise's Introduction

lua-linenoise - Lua binding for the linenoise command line library

Linenoise (https://github.com/antirez/linenoise) is a delightfully simple command line library. This Lua module is simply a binding for it.

The main Linenoise upstream has stagnated a bit, so this binding tracks https://github.com/yhirose/linenoise/tree/utf8-support, which includes things like UTF-8 support and ANSI terminal escape sequence detection.

This repository also contains a Windows-compatible version of linenoise taken from MSOpenTech's Windows port of redis.

Compilation

If you use LuaRocks, you can run luarocks make on the latest rockspec.

You can also build with make. When building this module using make, you may use the original linenoise source included in the repository, or you may set the Makefile variable LIBLINENOISE to override it:

make LIBLINENOISE=-llinenoise
# OR:
make LIBLINENOISE=/path/to/liblinenoise.a

You may need to change the value of the LN_EXPORT macro in lua-linenoise.c to the appropriate keyword to ensure the luaopen_linenoise function is exported properly (I don't know much about C or Unix-like systems, so I may have gotten it wrong).

If you have Visual Studio 2012 (even the free Express version), you can compile this module with the Windows-compatible linenoise source using the included solution file (you'll need to edit the include paths and import library dependencies to match your configuration).

If you prefer to compile using other tools, just link lua-linenoise.c with line-noise-windows/linenoise.c and line-noise-windows/win32fixes.c to create the Windows-compatible DLL.

Usage

This library is a fairly thin wrapper over linenoise itself, so the function calls are named similarly. I may develop a "porcelain" layer in the future.

L.linenoise(prompt)

Prompts for a line of input, using prompt as the prompt string. Returns nil if no more input is available; Returns nil and an error string if an error occurred.

L.historyadd(line)

Adds line to the history list.

L.historysetmaxlen(length)

Sets the history list size to length.

L.historysave(filename)

Saves the history list to filename.

L.historyload(filename)

Loads the history list from filename.

L.clearscreen()

Clears the screen.

L.setcompletion(callback)

Sets the completion callback. This callback is called with two arguments:

  • A completions object. Use object:add or L.addcompletion to add a completion to this object.
  • The current line of input.

L.addcompletion(completions, string)

Adds string to the list of completions.

All functions return nil on error; functions that don't have an obvious return value return true on success.

L.setmultiline(multiline)

Enables multi-line mode if multiline is true, disables otherwise.

L.sethints(callback)

Sets a hints callback to provide hint information on the right hand side of the prompt. calback should be a function that takes a single parameter (a string, the line entered so far) and returns zero, one, or two values. Zero values means no hint. The first value may be nil for no hint, or a string value for a hint. If the first value is a string, the second value may be a table with the color and bold keys - color is an ANSI terminal color code (such as those provided by the lua-term colors module), whereas bold is a boolean indicating whether or not the hint should be printed as bold.

L.printkeycodes()

Prints linenoise key codes. Primarly used for debugging.

L.enableutf8()

Enables UTF-8 handling.

Example

local L = require 'linenoise'
local colors = require('term').colors -- optional
-- L.clearscreen()
print '----- Testing lua-linenoise! ------'
local prompt, history = '? ', 'history.txt'
L.historyload(history) -- load existing history
L.setcompletion(function(completion,str)
   if str == 'h' then
    completion:add('help')
    completion:add('halt')
  end
end)
L.sethints(function(str)
  if str == 'h' then
    return ' bold hints in red', { color = colors.red, bold = true }
  end
end)

L.enableutf8()

local line, err = L.linenoise(prompt)
while line do
    if #line > 0 then
        print(line:upper())
        L.historyadd(line)
        L.historysave(history) -- save every new line
    end
    line, err = L.linenoise(prompt)
end
if err then
  print('An error occurred: ' .. err)
end

lua-linenoise's People

Contributors

aleclarson avatar choonster avatar devurandom avatar fperrad avatar hoelzro avatar ncopa avatar stevedonovan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

lua-linenoise's Issues

Update linenoise

You currently include an old copy of Salvatore Sanfilippo's (antirez) linenoise [1]. He himself pushed an update with ^W support. And there is another interesting version by Steve Bennett (msteveb) [2], which includes UTF-8 support, bugfixes, code cleanups and similar improvements.

It would be nice if you could update your copy of linenoise to the latter.

Or just drop it altogether and let the user link with an arbitrary lib:

--- Makefile.orig       2012-11-02 11:02:25.087782718 +0100
+++ Makefile    2012-11-02 11:05:19.114096897 +0100
@@ -1,10 +1,15 @@
 OS=$(shell uname)
+
+ifeq ($(LIBLINENOISE),)
+$(error You need to define LIBLINENOISE)
+endif
+
 ifeq ($(OS),Darwin)
-linenoise.dylib: linenoise.o linenoiselib.o
+linenoise.dylib: linenoise.o $(LIBLINENOISE)
        gcc -o $@ -bundle -undefined dynamic_lookup $^
 else
 CFLAGS=-fPIC -I/usr/include/lua5.1
-linenoise.so: linenoise.o linenoiselib.o
+linenoise.so: linenoise.o $(LIBLINENOISE)
        gcc -o $@ -shared $^
 endif

[1] https://github.com/antirez/linenoise
[2] https://github.com/msteveb/linenoise

update linenoise

Could you please update linenoise to the latest version?
antirez added support for Home & End a while ago and i would like to use it in lua :)

Thanks in advance!

lua errors in completions callback function are not caught

lua errors in completions callback function are silently ignored.

Testcase:

local L = require 'linenoise'
print '----- Testing lua-linenoise! ------'
local prompt = '? '
L.setcompletion(function(c,s)
   error("this should abort here")
   if s == 'h' then
    L.addcompletion(c,'halt')
  end
end)
local line = L.linenoise(prompt)
while line do
    if #line > 0 then
        print(line:upper())
    end
    line = L.linenoise(prompt)
end

when pressing 'h' the lua script should abort, instead the error is silently ignored.

Memory leak of strings returned by linenoise library

I was going through the code and I think you have a memory leak.
In l_linenoise() you're invoking the linenoise() function from the library. The string that is returned needs to be freed by the caller but this is not done after having pushed it to Lua using lua_pushstring().

Keystroke capture

Some way to capture keystrokes while the prompt is active would be awesome!

Makefile-created dylib not working on macOS

git clone https://github.com/hoelzro/lua-linenoise.git
cd lua-linenoise
make

The .dylib produced by make is complaining about missing symbols.

Question: Why doesn't the Makefile produce a .so file instead?

Using tarantool v1.9.0:

error loading module 'linenoise' from file '/path/to/lua-linenoise/linenoise.dylib':
	dlopen(/path/to/lua-linenoise/linenoise.dylib, 6): Symbol not found: _lua_callk
  Referenced from: /path/to/lua-linenoise/linenoise.dylib
  Expected in: flat namespace
 in /path/to/lua-linenoise/linenoise.dylib

Using lua5.1:

lua5.1: error loading module 'linenoise' from file '/path/to/lua-linenoise/linenoise.dylib':
	dlopen(/path/to/lua-linenoise/linenoise.dylib, 2): Symbol not found: _luaL_setfuncs
  Referenced from: /path/to/lua-linenoise/linenoise.dylib
  Expected in: flat namespace
 in /path/to/lua-linenoise/linenoise.dylib
stack traceback:
	[C]: ?
	[C]: in function 'require'
	test.lua:11: in main chunk
	[C]: ?

Repro:

function add_cpath(cpath)
  package.cpath = cpath .. ';' .. package.cpath
end

add_cpath('/path/to/lua-linenoise/?.dylib')
require('linenoise')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.