All Projects → hoelzro → lua-linenoise

hoelzro / lua-linenoise

Licence: MIT license
Lua bindings for linenoise with UTF-8 support (https://github.com/yhirose/linenoise/tree/utf8-support)

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to lua-linenoise

moongl
Lua bindings for OpenGL
Stars: ✭ 85 (+97.67%)
Mutual labels:  lua-bindings
moonvulkan
Lua bindings for Vulkan
Stars: ✭ 31 (-27.91%)
Mutual labels:  lua-bindings
lcurses
Lua bindings for Curses
Stars: ✭ 60 (+39.53%)
Mutual labels:  lua-bindings
lua-fltk4lua
Lua binding to the Fast Light Tool Kit (FLTK)
Stars: ✭ 51 (+18.6%)
Mutual labels:  lua-bindings
LuaCSP
Communicating Sequential Processes in Lua
Stars: ✭ 40 (-6.98%)
Mutual labels:  lua-bindings
nim-noise
Nim implementation of linenoise command line editor
Stars: ✭ 45 (+4.65%)
Mutual labels:  linenoise
Sol2
Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
Stars: ✭ 2,791 (+6390.7%)
Mutual labels:  lua-bindings
Ip2region
Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is ONLY a few megabytes with all IP address stored. binding for Java,PHP,C,Python,Nodejs,Golang,C#,lua. Binary,B-tree,Memory searching algorithm
Stars: ✭ 9,836 (+22774.42%)
Mutual labels:  lua-bindings
luaRtAudio
RtAudio + libsndfile lua binding
Stars: ✭ 18 (-58.14%)
Mutual labels:  lua-bindings
Unity-Lua
A wrapper around MoonSharp that allows easy development of moddable Unity games
Stars: ✭ 105 (+144.19%)
Mutual labels:  lua-bindings
lua-leveldb
Lua bindings for google's leveldb library.
Stars: ✭ 50 (+16.28%)
Mutual labels:  lua-bindings

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].