Delete user input from terminal?


I’m working on a little project that’s a “personal assistant” to me manage my todo list.

I’m using Term.jl to make it look pretty. I’m trying to emulate sort of a chat box aesthetic.

It’s bugging me that I can’t remove the first “check_email” where I was typing before printing out the chatbox version. I tried some solutions from here, but they don’t seem to work on user text.


using REPL
import REPL.Terminals
terminal = Terminals.TTYTerminal("", stdin, stdout, stderr)
function user_input()
    print("how can I help?\n")
    response = readline()
    # I've also tried some other "clear last line" options with escape characters
    print("your response was $(response)")

I’d appreciate any ideas!

1 Like

You can try using terminal codes.

julia> n = 10;

julia> print("."^n * "\n" * "."^(2*n) * "\n" * "."^(3n));
julia> print("."^n * "\n" * "."^(2*n) * "\n" * "."^(3n));print("\e[1A\e[2K");


\e[1A moves the cursor up. \e2K deletes the entire line (although it seems to delete the next one as well). My example was tested in Julia 1.7.0 in the default Ubuntu terminal, different terminals may implement escape codes differently.

Move Cursor
ANSI Escape Codes (Wikipedia)
Terminal Codes (page does load but is very slow)

For more formatting examples try this code in Julia:

for i=0:1:10
        for j=0:1:9
                print("\033[" * string(10*i + j) * "m" * string(10*i + j) * "\033[m\t")

I would paste the results here but it wouldn’t be properly formatted. :man_shrugging:t2:


That looks amazing, looking forward to see what you’ll build with Term!

Term has some methods that can help with, for example clearing a line from the terminal. These make use of ANSI control codes printed to the terminal, allowing you to move the cursor around, delete stuff etc.

For example

using Term
using Term.Consoles

println("this will show")
println("this will be erased")
println("this will also show")

Hope this helps, get in touch if you have any questions!


Also, Term version 2.0 will have Prompt functionality: Prompt · Term.jl

If you’re brave enough you can grab the code from github and start playing around with it.


Dope! Yeah, I’ll take a look!