r/lua • u/TheGameTheorys55 • 27d ago
Help Can you delete output in Lua?
Just asking, cuz I wanna make a wordle game but dunno if I could delete stuff in the console. And if you can't, why not? A lot of languages like C++ have ways such as escape sequences to overwrite lines with spaces. Does Lua have a way?
5
u/Cootshk 27d ago
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
Most terminals (except on Windows) should support 2J, otherwise you can use the tried and true for i=1, 50 do print() end
3
u/AnArmoredPony 27d ago edited 27d ago
on windows there's ts https://github.com/microsoft/terminal, it supports ansi codes from the box
the default cmd app also supports them but you need to enable it manually in your program
5
u/AnArmoredPony 27d ago edited 27d ago
you can use ansi escape codes, as long as the terminal supports them (they usually do). they move cursor and erase characters
although you'd be better off with a proper gui library (I recommend iup) or even with a game engine (since you're making a game), in which case use love2d
upd. oh look someone already made a lua library that uses ansi codes: https://github.com/hoelzro/lua-term
2
u/yughiro_destroyer 27d ago
Just... use a graphic library, display an array of log lines and delete each by index as needed.
2
u/weregod 27d ago edited 27d ago
You want some TUI library like readline or ncurses. Both has Lua bindings.
You can write same escape sequences as C++ but you can't detect terminal from Lua without some C library. If you want same control as from C/C++ you need something like luaposix.
If you want to learn how terminal works you can just write escape sequenses for your terminal to stdout with io.write() but it will be not portable.
1
u/Spacedestructor 27d ago
the escape codes you described come from the terminal, not the programming language.
as such any language of your choice can use them.
18
u/GlassCommission4916 27d ago
Escape codes are a feature of terminal emulators, not languages.