How to get a handle to the Terminal

Let’s say I want to clear my terminal programmatically, i.e. not with Ctrl+l, but by running Julia code. I can do the following:

import REPL
julia> t = REPL.Terminals.TTYTerminal(string(), stdin, stdout, stderr)
REPL.Terminals.clear(t);   # ; to supress some integer return value


julia> REPL.Terminals.TTYTerminal <: REPL.Terminals.UnixTerminal

, which makes me wonder - would this not work on windows?

My question is: Are there no simpler ways to get a handle to the terminal, the one I bind to t, than to construct a TTYTerminal, which I don’t even know what is? I imagine a function like REPL.current_repl(), or something to that effect. It is possible this exists, because there is a lot of undocumented stuff going on inside the REPL module.

1 Like

A solution is to define something like the following

import REPL
function current_terminal()
    if Sys.isunix()
        return REPL.Terminals.TTYTerminal("TerminalType (what should go here?)", stdin, stdout, stderr)
    #elseif Sys.isapple()
        # Please help
    #elseif Sys.iswindows()

        error("`current_terminal` is not implemented for your operating system (=$(Base.BinaryPlatforms.os())).")

Would a function like this (with better messages and more platform support) be welcome into the REPL stdlib? I for one would love to have it, and it would also make it simpler to add docstrings to show usage of e.g. the family of functions starting with REPL.Terminals.cmove, which I would like to take a crack at documenting.

The function name is akin to current_figure and current_axis, inspired by Makie. Naming suggestions are also welcome.

I am also quite unsure about non-unix systems, because of the type tree below:

AbstractTerminal only has TextTerminal as subtype, which only has UnixTerminal as subtype. What about other operating systems?

I just discovered, by unrelatedly digging into the internals of RemoteREPL.jl, that there is a variable Base.active_repl, which almost is what I want. Specifically, Base.active_repl is a subtype of REPL.AbstractREPL, while I need a subtype of REPL.Terminals.AbstractTerminal. Fortunately, Base.activeREPL has a field t, which fits the bill:

julia> Base.active_repl.t isa REPL.Terminals.AbstractTerminal

So to clear the terminal, one can use


, where the semi-colon supresses the 7 returned from write.