lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I have written a quick and dirty Lua binding for libreadline with limited line completion support. It's provided as a FreeBSD port, but it should be easy to figure out how to build it as a standalone library.

Download at: http://rghost.ru/7wZ2xgrbT
Mirror: ftp://motoprogger.ru/distrib/lua/readline/lua-readline.tar.xz

It provides two ways of completion management:
1) A table of strings
2) A generator function
A table of strings is considered unsorted and is searched for strings beginning with the already typed-in prefix. A generator function gets the typed-in prefix and must return an iterator of matching strings. The strings are displayed in the iterator order and are not obligated to begin with the provided prefix.

It's called like:

local readline = require "readline"
result = readline.readline("Prompt: ", {"Completion1", "Completion2", "Other completions"})
-- or
result = readline.readline("Prompt: ", generator_function)

The generator function can be like:

function generator(prefix)
     local suffixes = {"a", "e", "i", "o", "u", "y"}
     local i = 0
     return function()
         i = i + 1
         return prefix..suffixes[i]
     end
end

The library's current limitations are:
1) It doesn't save or restore libreadline state. In particular it interferes with the interactive Lua shell.
2) It doesn't support libreadline key binding or functions
3) It supports only one completion generator or array and allows to type in more than one word of the same set. Probably I shall provide some usage examples in a while. I would be glad to see it converted to a more appropriate distribution form, provided with documentation and developed further.