r/lua • u/TheGameTheorys55 • Sep 26 '26
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?
10
Upvotes
18
u/GlassCommission4916 Sep 26 '26
Escape codes are a feature of terminal emulators, not languages.
5
u/Cootshk Sep 26 '26
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
5
u/AnArmoredPony Sep 26 '26 edited Sep 26 '26
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 Sep 27 '26
Just... use a graphic library, display an array of log lines and delete each by index as needed.
2
u/weregod Sep 27 '26 edited Sep 27 '26
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.
2
u/zeroxff Sep 27 '26
what you are talking about has nothing to do with Lua or any other language.
it's a terminal capability.
1
u/Spacedestructor Sep 27 '26
the escape codes you described come from the terminal, not the programming language.
as such any language of your choice can use them.
1
u/ICON_4 Sep 28 '26
Just use love2d