[ANN] terminal; new cross-platform terminal library

149 views
Skip to first unread message

Thijs Schreijer

unread,
Jan 9, 2025, 3:38:30 PM1/9/25
to lu...@googlegroups.com
List,

I'm happy to announce a new Lua library; terminal.

This one has been in the making for a long time. The main difference with other libraries is that this one supports Windows, Mac and Unix. Last year I contributed the platform specific terminal functions to LuaSystem, on top of which this library builds.

Besides generating the ANSI sequences to control the terminal, it also has "stacks" which allow for better control. So you can push "foreground red" onto the stack, changing the color to red, and then pop it again, reverting it to the previous settings.

Only dev code for now, no release yet. Since this cannot be easily all tested in an automated way, I do expect some bugs. Please give it a try and report any issues.

Source and docs are on Github [1], all comments welcome.

installation is easiest using luarocks with the `--dev` flag:
> luarocks install terminal --dev

Thijs


[1] https://github.com/Tieske/terminal.lua

Rett Berg

unread,
Jan 10, 2025, 6:50:24 PM1/10/25
to lua-l
Nice, definitely a space Lua needs more libraries in.

You might be interested in some of my attempts: I've written https://lua.civboot.org#Package_asciicolor which I realize is pretty poorly documented on how to actually use it. It is meant to be instantiated like this https://github.com/civboot/civlua/blob/main/civ.lua#L25

Then you can do things like

io.fmt:styled('bold', 'this is bold text')
io.fmt:styled('comment', 'this is styled like a /*comment*/ in your personal syntax highlighting config') 

I'm still working out the kinks but the major components are:
  • asciicolor library which maps single ascii characters to the most common colors (capital means bold/underline)
  • vt100 library which handles actually tracking and outputting codes
  • fmt/styler which handles converting user-defined styles to the above
Best,
Rett

Martin Eden

unread,
Jan 10, 2025, 7:35:37 PM1/10/25
to lu...@googlegroups.com
Every real programmer should try to write VT100 library at least once in
his life.

While you are disagree how to name "[34m" - "blue" or "navy", you are
missing the fanciest modern and hipster feature - RGB colors.

Here's code snippet to set background character color attribute to color
described by RGB:

  local FukenEsc = '\027['

  local GetCommand_SetRgbBackground =
    function(RedByte, GreenByte, BlueByte)
      AssertByte(RedByte)
      AssertByte(GreenByte)
      AssertByte(BlueByte)

      local CommandFmt = FukenEsc .. '48;2;%d;%d;%dm'
      local Command = string.format(CommandFmt, RedByte, GreenByte,
BlueByte)

      return Command
    end

Works in my GNOME terminal. (Don't care about Windows or real text mode.)

Best wishes,

-- Martin

Thijs Schreijer

unread,
Jan 11, 2025, 10:13:38 AM1/11/25
to lu...@googlegroups.com


On Sat, 11 Jan 2025, at 00:50, Rett Berg wrote:
Nice, definitely a space Lua needs more libraries in.

You might be interested in some of my attempts: I've written https://lua.civboot.org#Package_asciicolor which I realize is pretty poorly documented on how to actually use it. It is meant to be instantiated like this https://github.com/civboot/civlua/blob/main/civ.lua#L25

Then you can do things like

io.fmt:styled('bold', 'this is bold text')
io.fmt:styled('comment', 'this is styled like a /*comment*/ in your personal syntax highlighting config') 
I'm still working out the kinks but the major components are:
  • asciicolor library which maps single ascii characters to the most common colors (capital means bold/underline)
  • vt100 library which handles actually tracking and outputting codes
  • fmt/styler which handles converting user-defined styles to the above
Best,
Rett

But the ASCII code is really the easy part. Platform differences and tracking settings is hardest. At least imho. 

I’d like to write a ui in the terminal at some point (cross platform), that will probably give me a better idea of the type of API that works best. Though I realise I probably need 2, one for a streaming terminal application, and one for fullscreen apps. Time will tell. 

Thijs


Thijs Schreijer

unread,
Jan 11, 2025, 10:18:25 AM1/11/25
to lu...@googlegroups.com


On Sat, 11 Jan 2025, at 01:35, 'Martin Eden' via lua-l wrote:
> Every real programmer should try to write VT100 library at least once in
> his life.
>
> While you are disagree how to name "[34m" - "blue" or "navy", you are
> missing the fanciest modern and hipster feature - RGB colors.
>
> Here's code snippet to set background character color attribute to color
> described by RGB:
>

Not sure what you mean, the library supports basic, extended and RGB colours already.

See https://tieske.github.io/terminal.lua/#textsets

Thijs

Reply all
Reply to author
Forward
0 new messages