print(1) to print to the terminal. But I want clear the
1 that I just printed and on the same line in the terminal
print(2). Is that possible?
'\r' character will virtually clear the current line in most terminals.
You can try using ANSI codes (to my understanding, an ancient standard of terminal manipulation), here is a code of a function that overwrites the last line and prints something new
#source: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences # a more complete description of the codes used. function overprint(str) print("\u1b[1F") #Moves cursor to beginning of the line n (default 1) lines up print(str) #prints the new line print("\u1b[0K") # clears part of the line. #If n is 0 (or missing), clear from cursor to the end of the line. #If n is 1, clear from cursor to beginning of the line. #If n is 2, clear entire line. #Cursor position does not change. println() #prints a new line, i really don't like this arcane codes end #testing println(1) println(2) println(3) println(4) println(5) println("this is the number six") overprint(7) println(8)
1 2 3 4 5 7 8
The standard library REPL has some terminal support standardized, eg see
How you actually use it?
?REPL.Terminals.clear_line wasn’t very helpful
print("ayy") print("\e[2K") # clear whole line print("\e[1G") # move cursor to column 1 print("lmao")
You should now see just “lmao” on the current (or maybe previous, if in the REPL) line. Personally, I find using escape codes manually much easier and more predictable than trying to re-using functionality that wasn’t designed for the use-case that I have in mind.
I’d like to take offense to this statement as a terminal lover, but I can understand that these sequences appear cryptic. Note, however, that ANSI escape codes are used in a variety of modern terminal-first programs to great effect, and so the term
“archaic” is maybe not accurate. “ancient” is actually accurate, but doesn’t really matter anyway.
This is also incorrect, based on what the OP requested:
So escape code
F would not be what you want to use, since that assumes you’re already on the next line. Instead,
G is best used here, as a
print() will not necessarily advance the terminal by a newline.
I just want to point out that “ancient” and “archaic” don’t really mean the same thing.
Yes, the package cannot be accused of being overdocumented I would try something like
import REPL.Terminals terminal = Terminals.TTYTerminal("", stdin, stdout, stderr) function f(terminal) print("this message will self-destruct in 5 seconds") sleep(5) REPL.Terminals.clear_line(terminal) print("bang") end f(terminal)
but I am not an expert on this.