0

Backstory (You can skip)

Awhile back I was developing a console toolkit for displaying debug messages and the like: enter image description here It gives me colour coding, blinking, underlines, bold, italic, etc.

While developing this library, I quickly learned that nesting with ANSI escape sequences was impossible, but assumed that good reasons existed why this was the case.

Of course working with other document types, nesting is more or less trivial:

<foo> the <bar> quick </bar> brown </foo>

or with JSON

{
        type=foo,
        text=[
                "the",
                {type=bar, text="quick",},
                "brown",
        ]
}

But with Ansi, its something like this:

\e[1m the \e[2m quick \e[1m brown

giving an output like this:

enter image description here

Basically meaning that you would need to manually track the formatting, and explicitly construct escape sequences to represent all output moving forward. You can obviously make due, but it complicates things. Before complaining about this, I'd like to clarify what reasons exist that would necessitate an escape sequence model over say, a structured document style.

Questions:

Is it purely due to legacy reasons why displaying text on our video terminals is done with ANSI escape sequences and not another framework such as JSON, Yaml, XML, or something else?

Is ANSI escape sequences in video terminal, simply an old technology similar to say, X11 that sticks around solely due to how embedded it is within the computing paradigm?

If not, why don't developers switch from an escape sequence style to something that would support nesting?

Are there any proposals to do away with ANSI escapes in terminals and replace it with something else?

Anon
  • 3,639

2 Answers2

6

Your question is sort of like saying assembly is difficult to work with, so computers should use higher level languages instead. The ANSI format is the right level of abstraction for working with terminal hardware. You have to do the state tracking somewhere, and it's best to not do it in hardware that needs to be as cheap as possible. Even in modern hardware, some library at some point has to do the state tracking level of abstraction.

When you want something easier for a human to use, you use higher-level abstractions that have been around forever. Even something like nroff is a big improvement, and I'm sure you can find libraries and/or utilities for all the markup formats you described.

Karl Bielefeldt
  • 148,830
4

There is a simple reason. When you are typing you can only input a character at a time.

Something that is processing typed text has to deal with what in effect is invalid markup, half a json blob or half an xml document. You can't display "error" until the typing has finished and then display the result. You have to have a format where partial documents are valid and already input data wont change due to additions.

Ewan
  • 83,178