How to clear what has already been written on the moon terminal?

Asked

Viewed 437 times

2

After the execution of the command:

Print("teste")
Info = io.read()

I would like the terminal screen, with the print resulting were cleaned

2 answers

2

is almost the same problem I answer in this question by asking about the Python terminal How to clean the console in Python? -

Reason: deleting the terminal is not a language problem - neither Lua, nor C++, nor Python, nor any that is not an integrated "framework" with the terminal - like Cobol, the "Qbasic" predecessor of Visualbasic and the first versions of Delphi in the 1990s - all of them "see" the output only as a byte write file (or characters, in cases where the language automatically translates accented text and emojis into the encoding the terminal is using).

So, if you need any interface a little more sophisticated in the terminal, and in this case, very unsophisticated: just delete the screen - you have to use commands that the terminal program will understand. These commands are named "ANSI Sequences" - and in addition to being able to erase the screen, they can also position the print in any position, change the color of the text, etc... They start with a character "<ESC>" (code " x1b"), followed by some parameters and a command specifier. Instead of these parameters appearing printed on the terminal, they perform an action.

These commands work on most terminals, but just don’t work on cmd Windows - (but it will work if you download the "terminal" app from the Windows store).

Then, to erase the screen and position the cursor on line 1, column 1, just print the sequence:

print("\x1b[2J\x1b[1;1H")

To just erase the screen:

print("\x1b[2J")

Another solution, if you are on Windows CMD and have no ANSI sequences, is to print a portion of blank lines, at least one more than the terminal size. In Lua needs to make one for

for i=0,70,1 do
  print("\n")
end

Will print 70 blank lines.

what not to do:

Why avoid calling "os.execute"? As described in the other answer, the call os.execute will cynicism another process in the operating system, load a shell - and all this to print some blank lines.

It’s about 1,000 to 10,000 times slower, and it uses as much memory as it does. It’s not going to make a difference because if you’re erasing the screen, you’re in an interactive session with the user - and the computers today are so fast that it’s still going to be inconspicuous. But I always say wear os.execute("clear") to erase the screen is equivalent to call a keychain to come, and open your home door with his tools, every time you go in or out of the house (being that you have the key in your hand and just use it).

But, despite everything, it’s just inelegant and the user experience will be the same - I personally bother myself with it (and it will work differently on Linux and Windows - but until then, the ANSI sequence will also)

Other commands for the terminal:

Here is a nice page with the summary of several commands for the terminal:

http://ascii-table.com/ansi-escape-sequences.php

Just remember to exchange "ESC" for " x1b" - which prints the character "ESC" by its code (27 decimal). Another tip is after you discover sequences that work, don’t leave the "raw" sequences in the middle of your code - who’s going to see one print("\x1b[2J") there in the middle and will know that you are erasing the screen? Create or variables with the strings containing the cmandos, or functions that print these commands of type:

function cls() 
    print("\x1b[2J\x1b[1;1H") 
end

There - at the point where you need to delete the screen, you just call the function cls() - is much easier to worry only about the logic of your program and the presentation codes are in a separate part.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.