Cursor movement package

A while back I got really into making a program with a terminal based UI, and therefore wanted some solid foundational building blocks for working with the terminal. Moving the cursor is the first, saving and restoring the position is another, and clearing the screen is a third. That lead me down a rabbit hole that among other things lead me to implement this PR, which is a starting point for making existing similar functionality in base julia more available.

However, I recently stumbled upon this discourse post, which shows of the following snippet:

julia> Base.current_terminfo[:cursor_up] # Private API, do not rely on
"\e[A"

Which had me thinking: It would be really easy to wrap this functionality in a package, a starting point for which could be the following:

function cursor_up(n=1)
    print(Base.current_terminfo()[:cursor_up]^n)
end

function cursor_down(n=1)
    print(Base.current_terminfo()[:cursor_down]^n)
end

function cursor_left(n=1)
    print(Base.current_terminfo()[:cursor_left]^n)
end

function cursor_right(n=1)
    print(Base.current_terminfo()[:cursor_right]^n)
end

function save_cursor()
    print(Base.current_terminfo()[:save_cursor])
end

function restore_cursor(n)
    print(Base.current_terminfo()[:restore_cursor])
end

function clear_screen(n)
    print(Base.current_terminfo()[:clear_screen]
end

The lines above would be a useful function by itself IMO. I also want to mention that there is an alternative route to implementing cursor movement based on stuff already in base julia, which is using the functions inside Base.Terminals. This is the relavant method to the PR i linked initially:

Base.Terminals.cmove_up(Base.active_repl.t)

But no matter how the implementation is done, I keep thinking that it should be very easy to make the user-facing API a lot prettier, and that making a package that just wraps some non-user-facing internals seems like a bad and over-complicated approach, and this would be best implement in base julia itself. Could we get some support behind that idea, and possible also start a discussion here on what would be the right way to implement this in julia Base? Specifically which internal functions should be used as building blocks, and in which file should the code be added.

See also Terminal Capabilities Packages in Julia?

1 Like

Just on this:

Terminfo-jl.tar.gz.jl (203.8 KB)

(Remove the .jl extension and you can untar it)

I recommend trying

using Terminfo
Terminfo.prettyprint()