diff options
Diffstat (limited to 'lib/lua-term')
| -rw-r--r-- | lib/lua-term/.gitignore | 3 | ||||
| -rw-r--r-- | lib/lua-term/CHANGES | 25 | ||||
| -rw-r--r-- | lib/lua-term/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | lib/lua-term/COPYING | 19 | ||||
| -rw-r--r-- | lib/lua-term/Makefile | 38 | ||||
| -rw-r--r-- | lib/lua-term/README.md | 189 | ||||
| -rw-r--r-- | lib/lua-term/core.c | 27 | ||||
| -rw-r--r-- | lib/lua-term/lua-term-0.7-1.rockspec | 23 | ||||
| -rw-r--r-- | lib/lua-term/term/colors.lua | 83 | ||||
| -rw-r--r-- | lib/lua-term/term/cursor.lua | 35 | ||||
| -rw-r--r-- | lib/lua-term/term/init.lua | 51 |
11 files changed, 494 insertions, 0 deletions
diff --git a/lib/lua-term/.gitignore b/lib/lua-term/.gitignore new file mode 100644 index 00000000..ab0a9118 --- /dev/null +++ b/lib/lua-term/.gitignore @@ -0,0 +1,3 @@ +*.o +*.so +*.rock diff --git a/lib/lua-term/CHANGES b/lib/lua-term/CHANGES new file mode 100644 index 00000000..22b7ddb1 --- /dev/null +++ b/lib/lua-term/CHANGES @@ -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. diff --git a/lib/lua-term/CMakeLists.txt b/lib/lua-term/CMakeLists.txt new file mode 100644 index 00000000..2350152d --- /dev/null +++ b/lib/lua-term/CMakeLists.txt @@ -0,0 +1 @@ +idf_component_register(SRCS "core.c" INCLUDE_DIRS "." REQUIRES "esp-idf-lua") diff --git a/lib/lua-term/COPYING b/lib/lua-term/COPYING new file mode 100644 index 00000000..48de251d --- /dev/null +++ b/lib/lua-term/COPYING @@ -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. diff --git a/lib/lua-term/Makefile b/lib/lua-term/Makefile new file mode 100644 index 00000000..75a87f34 --- /dev/null +++ b/lib/lua-term/Makefile @@ -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 diff --git a/lib/lua-term/README.md b/lib/lua-term/README.md new file mode 100644 index 00000000..edc805b3 --- /dev/null +++ b/lib/lua-term/README.md @@ -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 diff --git a/lib/lua-term/core.c b/lib/lua-term/core.c new file mode 100644 index 00000000..7a144086 --- /dev/null +++ b/lib/lua-term/core.c @@ -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; +} diff --git a/lib/lua-term/lua-term-0.7-1.rockspec b/lib/lua-term/lua-term-0.7-1.rockspec new file mode 100644 index 00000000..ba1a2bf2 --- /dev/null +++ b/lib/lua-term/lua-term-0.7-1.rockspec @@ -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', +} diff --git a/lib/lua-term/term/colors.lua b/lib/lua-term/term/colors.lua new file mode 100644 index 00000000..76f8b2e0 --- /dev/null +++ b/lib/lua-term/term/colors.lua @@ -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 diff --git a/lib/lua-term/term/cursor.lua b/lib/lua-term/term/cursor.lua new file mode 100644 index 00000000..e37864c7 --- /dev/null +++ b/lib/lua-term/term/cursor.lua @@ -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 diff --git a/lib/lua-term/term/init.lua b/lib/lua-term/term/init.lua new file mode 100644 index 00000000..bd2024bd --- /dev/null +++ b/lib/lua-term/term/init.lua @@ -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 |
