add a cool lua repl
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
*.o
|
||||
*.so
|
||||
*.rock
|
||||
@@ -0,0 +1,25 @@
|
||||
0.07 2016-04-05 22:00:00
|
||||
- Fix compilation errors for Lua 5.3 on GCC < 5.0
|
||||
|
||||
0.06 2016-04-04 20:00:00
|
||||
- Fix compilation flags for Lua 5.3
|
||||
|
||||
0.05 2016-04-03 15:50:00
|
||||
- Don't include unistd.h on Windows
|
||||
|
||||
0.04 2016-03-06 16:25:47
|
||||
- Include missing unistd.h header file
|
||||
|
||||
0.03 2014-04-02 08:00:00
|
||||
- Fix syntax error for term.cursor.goto for Lua 5.2
|
||||
- Add jump as an alias for goto
|
||||
|
||||
0.02 2013-02-21 21:15:00
|
||||
- Add cursor functions.
|
||||
- Add clear functions.
|
||||
- Deprecate term.isatty. Use luaposix instead.
|
||||
- Add colors.default as a synonym for colors.reset.
|
||||
- The metatable for colors is no longer hidden.
|
||||
|
||||
0.01 2012-06-25 23:30:00
|
||||
- Initial release. Includes colors and isatty.
|
||||
@@ -0,0 +1 @@
|
||||
idf_component_register(SRCS "core.c" INCLUDE_DIRS "." REQUIRES "esp-idf-lua")
|
||||
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,38 @@
|
||||
#this file builds lua-term \o/
|
||||
|
||||
LUA_VER ?= 5.1
|
||||
LUA_DIR ?= /usr
|
||||
LUA_LIBDIR := $(LUA_DIR)/lib/lua/$(LUA_VER)/term
|
||||
LUA_INC := $(LUA_DIR)/include/lua$(LUA_VER)
|
||||
LUA_SHARE := $(LUA_DIR)/share/lua/$(LUA_VER)/term
|
||||
CWARNS := -Wall -pedantic
|
||||
CFLAGS += $(CWARNS) -O3 -I$(LUA_INC) -fPIC
|
||||
LIB_OPTION := -shared
|
||||
|
||||
SONAME := core.so
|
||||
SONAMEV := $(SONAME).1
|
||||
LIBRARY := $(SONAMEV).0.1
|
||||
SRC := core.c
|
||||
OBJ := $(patsubst %.c, %.o, $(SRC))
|
||||
|
||||
FILES := term/init.lua term/cursor.lua term/colors.lua
|
||||
|
||||
all: $(LIBRARY) $(SONAMEV) $(SONAME)
|
||||
|
||||
$(SONAMEV):
|
||||
ln -s $(LIBRARY) $@
|
||||
|
||||
$(SONAME):
|
||||
ln -s $(SONAMEV) $@
|
||||
|
||||
$(LIBRARY): $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LIB_OPTION) -o $(LIBRARY) $(OBJ) -lc
|
||||
|
||||
install:
|
||||
mkdir -p $(LUA_LIBDIR)
|
||||
cp $(SONAME) $(LUA_LIBDIR)
|
||||
mkdir -p $(LUA_SHARE)
|
||||
cp $(FILES) $(LUA_SHARE)
|
||||
|
||||
clean:
|
||||
$(RM) $(LIBRARY) $(SONAMEV) $(SONAME) *.o
|
||||
@@ -0,0 +1,189 @@
|
||||
Overview
|
||||
--------
|
||||
|
||||
lua-term is a Lua module for manipulating a terminal.
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
lua-term is available on Luarocks.
|
||||
|
||||
|
||||
## OpenBSD
|
||||
|
||||
lua-term is available as an OpenBSD package. Use the proper Lua flavour to
|
||||
get the package for your Lua version:
|
||||
|
||||
```
|
||||
# For Lua 5.1
|
||||
$ doas pkg_add -r lua-term
|
||||
# For Lua 5.2
|
||||
$ doas pkg_add -r lua52-term
|
||||
# For Lua 5.3
|
||||
$ doas pkg_add -r lua53-term
|
||||
```
|
||||
|
||||
Or install from ports:
|
||||
|
||||
```
|
||||
$ cd /usr/ports/devel/lua-term
|
||||
$ env FLAVOR=lua51 doas make install
|
||||
```
|
||||
|
||||
## openSUSE
|
||||
lua-term is available in the `devel:languages:lua` devel project on [OBS](https://build.opensuse.org/package/show/devel:languages:lua/lua-luaterm).
|
||||
|
||||
Add the repository and install lua-term via:
|
||||
```
|
||||
zypper addrepo http://download.opensuse.org/repositories/devel:/languages:/lua/openSUSE_Tumbleweed/devel:languages:lua.repo
|
||||
zypper refresh
|
||||
zypper in lua-luaterm
|
||||
```
|
||||
|
||||
Adjust the repository URL to your version of openSUSE by substituting `openSUSE_Tumbleweed` with your actual version eg `opensSUSE_42.2`.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
```lua
|
||||
local term = require 'term'
|
||||
local colors = term.colors -- or require 'term.colors'
|
||||
|
||||
print(term.isatty(io.stdout)) -- true if standard output goes to the terminal
|
||||
|
||||
print(colors.red 'hello')
|
||||
print(colors.red .. 'hello' .. colors.reset)
|
||||
print(colors.red, 'hello', colors.reset)
|
||||
|
||||
-- The following functions take an optional IO handle (like io.stdout);
|
||||
-- io.stdout is the default if you don't specify one
|
||||
term.clear() -- clears the screen
|
||||
term.cleareol() -- clears from the cursor to the end of the line
|
||||
--term.cursor.goto(1, 1) -- It will fail in Lua >= 5.2 because goto is a reserved word.
|
||||
term.cursor['goto'](1, 1) -- This will work on Lua >= 5.2, please use jump instead
|
||||
term.cursor.jump(1, 1) -- jump is just an alias for goto
|
||||
term.cursor.jump(io.stdout, 1, 1)
|
||||
term.cursor.goup(1)
|
||||
term.cursor.godown(1)
|
||||
term.cursor.goright(1)
|
||||
term.cursor.goleft(1)
|
||||
term.cursor.save() -- save position
|
||||
term.cursor.restore() -- restore position
|
||||
```
|
||||
|
||||
`term` Functions
|
||||
--------------
|
||||
|
||||
Some functions in lua-term take an optional file handle argument; if this is
|
||||
not provided, `io.stdout` is used.
|
||||
|
||||
### `term.clear([opt_file])`
|
||||
|
||||
Clear the terminal's contents.
|
||||
|
||||
### `term.cleareol([opt_file])`
|
||||
|
||||
Clear from the current cursor position to the end of the current line.
|
||||
|
||||
### `term.isatty(file)`
|
||||
|
||||
Returns `true` if `file` is a TTY; `false` otherwise.
|
||||
|
||||
*NOTE*: This function has been deprecated in favor of luaposix's implementation.
|
||||
If you would like this functionality in the future, please use luaposix.
|
||||
|
||||
`term.colors` Values
|
||||
------------------
|
||||
|
||||
The following values are available in `term.colors`:
|
||||
|
||||
### Terminal Attributes
|
||||
|
||||
* reset
|
||||
* clear (a synonym for reset)
|
||||
* default (a synonym for reset)
|
||||
* bright
|
||||
* dim
|
||||
* underscore
|
||||
* blink
|
||||
* reverse
|
||||
* hidden
|
||||
|
||||
### Foreground Colors
|
||||
|
||||
* black
|
||||
* red
|
||||
* green
|
||||
* yellow
|
||||
* blue
|
||||
* magenta
|
||||
* cyan
|
||||
* white
|
||||
|
||||
### Background Colors
|
||||
|
||||
* onblack
|
||||
* onred
|
||||
* ongreen
|
||||
* onyellow
|
||||
* onblue
|
||||
* onmagenta
|
||||
* oncyan
|
||||
* onwhite
|
||||
|
||||
Every value in `term.colors` may be used in several ways:
|
||||
|
||||
### As a Function
|
||||
|
||||
```lua
|
||||
print(colors.red 'hello')
|
||||
```
|
||||
|
||||
### As a String
|
||||
|
||||
```lua
|
||||
print(colors.red .. 'hello' .. colors.reset)
|
||||
print(colors.red, 'hello', colors.reset)
|
||||
```
|
||||
|
||||
`term.cursor` Functions
|
||||
---------------------
|
||||
|
||||
### `term.cursor.goto([opt_file], x, y)`
|
||||
|
||||
Place the cursor at (`x`, `y`).
|
||||
|
||||
### `term.cursor.jump([opt_file], x, y)`
|
||||
|
||||
An alias for `term.cursor.goto`.
|
||||
|
||||
### `term.cursor.goup([opt_file], nlines)`
|
||||
|
||||
Moves the cursor up `nlines` lines.
|
||||
|
||||
### `term.cursor.godown([opt_file], nlines)`
|
||||
|
||||
Moves the cursor down `nlines` lines.
|
||||
|
||||
### `term.cursor.goright([opt_file], ncols)`
|
||||
|
||||
Moves the cursor right `ncols` columns.
|
||||
|
||||
### `term.cursor.goleft([opt_file], ncols)`
|
||||
|
||||
Moves the cursor left `ncols` columns.
|
||||
|
||||
### `term.cursor.save([opt_file])`
|
||||
|
||||
Saves the cursor position.
|
||||
|
||||
### `term.cursor.restore([opt_file])`
|
||||
|
||||
Restores the cursor position.
|
||||
|
||||
Alternatives
|
||||
------------
|
||||
|
||||
If you are looking to simply provide coloration to a terminal application and would
|
||||
like to use a more "tag-like" API (ex. `colors '%{red}hello%{reset}'`), there is a Lua rock
|
||||
named ansicolors: https://github.com/kikito/ansicolors.lua
|
||||
@@ -0,0 +1,27 @@
|
||||
#define _POSIX_C_SOURCE 200112L
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
#ifndef _MSC_VER
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
static int
|
||||
lua_isatty(lua_State *L)
|
||||
{
|
||||
FILE **fp = (FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE);
|
||||
|
||||
lua_pushboolean(L, isatty(fileno(*fp)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
luaopen_term_core(lua_State *L)
|
||||
{
|
||||
lua_newtable(L);
|
||||
lua_pushcfunction(L, lua_isatty);
|
||||
lua_setfield(L, -2, "isatty");
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package = 'lua-term'
|
||||
version = '0.7-1'
|
||||
|
||||
source = {
|
||||
url = 'https://github.com/hoelzro/lua-term/archive/0.07.tar.gz',
|
||||
dir = 'lua-term-0.07',
|
||||
}
|
||||
|
||||
description = {
|
||||
summary = 'Terminal functions for Lua',
|
||||
homepage = 'https://github.com/hoelzro/lua-term',
|
||||
license = "MIT/X11",
|
||||
}
|
||||
|
||||
build = {
|
||||
modules = {
|
||||
['term'] = 'term/init.lua',
|
||||
['term.colors'] = 'term/colors.lua',
|
||||
['term.cursor'] = 'term/cursor.lua',
|
||||
['term.core'] = 'core.c',
|
||||
},
|
||||
type = 'builtin',
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
-- Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net>
|
||||
--
|
||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
-- of this software and associated documentation files (the "Software"), to deal
|
||||
-- in the Software without restriction, including without limitation the rights
|
||||
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
-- copies of the Software, and to permit persons to whom the Software is
|
||||
-- furnished to do so, subject to the following conditions:
|
||||
--
|
||||
-- The above copyright notice and this permission notice shall be included in
|
||||
-- all copies or substantial portions of the Software.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
-- THE SOFTWARE.
|
||||
|
||||
local pairs = pairs
|
||||
local tostring = tostring
|
||||
local setmetatable = setmetatable
|
||||
local schar = string.char
|
||||
|
||||
local colors = {}
|
||||
|
||||
local colormt = {}
|
||||
|
||||
function colormt:__tostring()
|
||||
return self.value
|
||||
end
|
||||
|
||||
function colormt:__concat(other)
|
||||
return tostring(self) .. tostring(other)
|
||||
end
|
||||
|
||||
function colormt:__call(s)
|
||||
return self .. s .. colors.reset
|
||||
end
|
||||
|
||||
local function makecolor(value)
|
||||
return setmetatable({ value = schar(27) .. '[' .. tostring(value) .. 'm' }, colormt)
|
||||
end
|
||||
|
||||
local colorvalues = {
|
||||
-- attributes
|
||||
reset = 0,
|
||||
clear = 0,
|
||||
default = 0,
|
||||
bright = 1,
|
||||
dim = 2,
|
||||
underscore = 4,
|
||||
blink = 5,
|
||||
reverse = 7,
|
||||
hidden = 8,
|
||||
|
||||
-- foreground
|
||||
black = 30,
|
||||
red = 31,
|
||||
green = 32,
|
||||
yellow = 33,
|
||||
blue = 34,
|
||||
magenta = 35,
|
||||
cyan = 36,
|
||||
white = 37,
|
||||
|
||||
-- background
|
||||
onblack = 40,
|
||||
onred = 41,
|
||||
ongreen = 42,
|
||||
onyellow = 43,
|
||||
onblue = 44,
|
||||
onmagenta = 45,
|
||||
oncyan = 46,
|
||||
onwhite = 47,
|
||||
}
|
||||
|
||||
for c, v in pairs(colorvalues) do
|
||||
colors[c] = makecolor(v)
|
||||
end
|
||||
|
||||
return colors
|
||||
@@ -0,0 +1,35 @@
|
||||
-- Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net>
|
||||
--
|
||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
-- of this software and associated documentation files (the "Software"), to deal
|
||||
-- in the Software without restriction, including without limitation the rights
|
||||
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
-- copies of the Software, and to permit persons to whom the Software is
|
||||
-- furnished to do so, subject to the following conditions:
|
||||
--
|
||||
-- The above copyright notice and this permission notice shall be included in
|
||||
-- all copies or substantial portions of the Software.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
-- THE SOFTWARE.
|
||||
|
||||
local term = require 'term.core'
|
||||
|
||||
local cursor = {
|
||||
['goto'] = term.maketermfunc '%d;%dH',
|
||||
goup = term.maketermfunc '%dA',
|
||||
godown = term.maketermfunc '%dB',
|
||||
goright = term.maketermfunc '%dC',
|
||||
goleft = term.maketermfunc '%dD',
|
||||
save = term.maketermfunc 's',
|
||||
restore = term.maketermfunc 'u',
|
||||
}
|
||||
|
||||
cursor.jump = cursor['goto']
|
||||
|
||||
return cursor
|
||||
@@ -0,0 +1,51 @@
|
||||
-- Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net>
|
||||
--
|
||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
-- of this software and associated documentation files (the "Software"), to deal
|
||||
-- in the Software without restriction, including without limitation the rights
|
||||
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
-- copies of the Software, and to permit persons to whom the Software is
|
||||
-- furnished to do so, subject to the following conditions:
|
||||
--
|
||||
-- The above copyright notice and this permission notice shall be included in
|
||||
-- all copies or substantial portions of the Software.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
-- THE SOFTWARE.
|
||||
|
||||
local term = require 'term.core'
|
||||
local sformat = string.format
|
||||
local iotype = io.type
|
||||
local stdout = io.stdout
|
||||
|
||||
function term.maketermfunc(sequence_fmt)
|
||||
sequence_fmt = '\027[' .. sequence_fmt
|
||||
|
||||
local func
|
||||
|
||||
func = function(handle, ...)
|
||||
if iotype(handle) ~= 'file' then
|
||||
return func(stdout, handle, ...)
|
||||
end
|
||||
|
||||
return handle:write(sformat(sequence_fmt, ...))
|
||||
end
|
||||
|
||||
return func
|
||||
end
|
||||
|
||||
term.colors = require 'term.colors'
|
||||
term.cursor = require 'term.cursor'
|
||||
|
||||
term.clear = term.maketermfunc '2J'
|
||||
term.cleareol = term.maketermfunc 'K'
|
||||
term.clearend = term.maketermfunc 'J'
|
||||
|
||||
term.maketermfunc = nil
|
||||
|
||||
return term
|
||||
Reference in New Issue
Block a user