Git Product home page Git Product logo

lua-term's Introduction

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.

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

    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

print(colors.red 'hello')

As a String

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

lua-term's People

Contributors

azet avatar edouardelasalles avatar hoelzro avatar jubalh avatar mpeterv avatar n0la avatar opoplawski avatar tst2005 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  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  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

lua-term's Issues

term-cursor: Crash with lua 5.2

I just started using lua for some scripts, so I may just be doing something wrong. But I think I found some bug when using lua-term with lua 5.2. I simply added the require statements to my scripts:

local term = require 'term'                                                      
local colors = term.colors 

And that results in the following crash:

lua: error loading module 'term.cursor' from file '/usr/local/share/lua/5.2/term/cursor.lua':
    /usr/local/share/lua/5.2/term/cursor.lua:24: unexpected symbol near 'goto'
stack traceback:
    [C]: in ?
    [C]: in function 'require'
    /usr/local/share/lua/5.2/term/init.lua:43: in main chunk
    [C]: in function 'require'
    ./scripts/deploy.lua:41: in main chunk
    [C]: in ?

The presumably offending line in term/cursor.lua is:

  goto    = term.maketermfunc '%d;%dH',

Therefore, I suspect a collision of a once OKAY identifier with the 'goto' keyword, which has been added in lua 5.2.

README Usage example has little error

When initialising, "local colors = term.colors" is used but later on, colors try to access "color" (e.g. "color.red").
Thank you for the great software. : )

Issue when installing in Lua 5.3

Hi there,

I think the release which happened a couple hours ago might be making the installation of lua-term fail with Lua 5.3. I am getting this error:

gcc -O2 -fPIC -I/home/travis/build/kikito/i18n.lua/lua_install/include -c core.c -o core.o
In file included from /home/travis/build/kikito/i18n.lua/lua_install/include/lua.h:16:0,
                 from core.c:3:
/home/travis/build/kikito/i18n.lua/lua_install/include/luaconf.h:567:2: error: #error "Compiler does not support 'long long'. Use option '-DLUA_32BITS'   or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)"
In file included from core.c:3:0:
/home/travis/build/kikito/i18n.lua/lua_install/include/lua.h:93:1: error: unknown type name ‘LUA_INTEGER’
/home/travis/build/kikito/i18n.lua/lua_install/include/lua.h:96:22: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘lua_Unsigned’
Error: Failed installing dependency: https://luarocks.org/lua-term-0.5-1.rockspec - Build error: Failed compiling object core.o

Example in travis: https://travis-ci.org/kikito/i18n.lua/jobs/120504745#L191

Error loading term on Fedora 20 with lua 5.2.2

I installed lua-term 0.2-1 via luarocks on Fedora 20. If I try to load it I get:

Lua 5.2.2  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> local term = require 'term'
error loading module 'term.cursor' from file '/usr/share/lua/5.2/term/cursor.lua':
        /usr/share/lua/5.2/term/cursor.lua:24: unexpected symbol near 'goto'
stack traceback:
        [C]: in ?
        [C]: in function 'require'
        /usr/share/lua/5.2/term/init.lua:43: in main chunk
        [C]: in function 'require'
        stdin:1: in main chunk
        [C]: in ?
> 

Remove isatty: users can now get it from luaposix

I see that isatty is the only function for which you need a C module. Further, you do not #include <unistd.h> as you should, nor <stdio.h> for fileno, which is used too.

Users who want isatty should simply use luaposix, which is also available as a rock, and provides isatty, fileno &c., and goes to greater lengths to be portable, include the correct headers &c. isatty has been available since 5.1.24, which is now available from luarocks.org (admittedly it was only released 5 days ago!).

Fails to build in Windows: 'unistd.h': No such file or directory

When attempting to build the latest release with MSVC (VS 2015)

luarocks.bat make
cl /nologo /MD /O2 -c -Focore.obj -IC:/Users/raf/Dev/neovim/.deps/usr/include/luajit-2.0/ core.c

core.c
core.c(4): fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory

Error: Build error: Failed compiling object core.obj

Building with MinGW works though, so a minimal patch is to disable the unistd.h include for MSVC

diff --git a/core.c b/core.c
index 425c3e9..66e9ba8 100644
--- a/core.c
+++ b/core.c
@@ -1,7 +1,9 @@
 #include <lua.h>
 #include <lauxlib.h>
 #include <lualib.h>
+#ifndef _MSC_VER
 #include <unistd.h>
+#endif

 static int
 lua_isatty(lua_State *L)

Please release again

We're getting beg reports on downstream packaging in Arch Linux asking us to patch this library with #19.

Instead of going down that road I'd like to just request a new release that includes the known fixes and we can package that.

Thanks.

Please don't hide the colors' metatable

The line

colormt.__metatable = {}

hides the colors' metatable (getmetatable return {}), which means that stdlib's pretty-printing "tostring" fails to detect that colors have a __tostring metamethod (as it is written in Lua, it uses getmetatable to get the metatable), so it fails to call the __tostring method, and colors are not rendered as intended.

Is there a problem with simply removing the above line?

Windows cmd terminal doesn't per default enable ANSI code interpretation

If using ANSI codes inside the cmd terminal, they will simply be printed as text. However they can be enabled, either quite hackily with os.execute "" or via the official Win API. I stumbled accross this issue, when the codes were not working at all inside my VSCode integreated terminal. So I went down a rabbit whole and found this stackoverflow question which perfectly explains why os.execute "" fixes the issue and how to use the official Win API to achieve the same result.

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.