“In your satin tights,
fighting for your rights,
and the old red, white, and blue…”
—Wonder Woman theme

Another One, eh 🤔?

“First I was afraid, I was petrified…”—Gloria Gaynor

 ▏
⸝<><><>⸜
<>✶><><><><>
<><><><><><✦<>
(<><><><><><><>)
<><><><><><><>
.  <>✶><><><><> .
: .   `<><><>´    .:
 ::            .      ⭒:
.⭒.      .       .     .:.
.:.      .         ⭒     : .
.: :     :          .      ::.
.:  :     ⭒           .:     .⭒ :
:⭒. :      .              :      : ..
. .                         .      .  .

Background

“CHARLIE DON’T SURF!”—Lt. Col. Kilgore

So ANSI escape codes for terminals have been standard on UNIX with the belt-and-suspenders crowd since the late seventies, and even saw use on DOS, the Amiga, and BBSs back in the day. With the advent of macOS X (ten), a whole new generation of lumber-sexuals have exposed themselves(?) to the terminal environment and command-line and liked it.  🤔

_images/dilbert_95-06-24.png

Dilbert, on 1995-06-24

“I’m a PC”

“Oooh! Oooh! Oooh!”—Arnold Horshack

Not on Windows NT, tho’. Amazingly, with recent versions of Windows 10 the Ballmer/Suit barrier was finally breached, allowing multi-decade-late improvements to be made to its until-now pathetic “console.” Often still known as the “DOS Prompt” since it has been frozen that long. Vaguely analogous to today’s virtual terminals, as a Yugo might compare to a classic BMW. But now, it’s supercharged with VT power.

But, don’t bother with it, it’s kind of a waste of time. Why? Now that Windows Terminal <https://en.wikipedia.org/wiki/Windows_Terminal> exists, I’d recommend that instead. Even though not currently complete, it is much better than “a souped up Yugo.”

So, now all the top/extant platforms support ANSI escape sequences. Again! What’s old is new again. Add in Unicode and millions of colors and it’s now better than ever.

We need great command-line/TUI tools and that’s where console fits in.

That’s the way, uhh huh, uhh huh, I like it…

_images/kc3.png

(Cue KC and the Sunshine Band, uhh-huh, uhh-huh on Soul Train…)

Batteries Not Included

“What’chu talkin’ ‘bout, Willis?”—Arnold Jackson

In the Python world, there hasn’t been easy-to-use support for terminal sequences in the standard library, beyond curses and termios (which mildly overlap in functionality with this package and themselves).

Those are low-level interfaces however, focused on “full screen” terminal apps and tty control respectively, while continuing to abstract hardware that now only exists in museums.

The ANSI standard may have won, but styling a text snippet here and there or setting a title without a bunch of ugly C-style function calls was thought too… trivial perhaps.

Terminfo?

Besides the difficulty factor mentioned, this classic answer to this problem also suffers in that it is:

  • Not installed on Windows

  • Not up to date on older OS variants

  • Not up to date with capability support, i.e. lags the real world:

    • “True color,” lagged for almost a decade (though now has some support)

    • Extended xterm capabilities not fully supported

    • Many terminals claim xterm support but aren’t completely compatible

    • Tons of obsolete capabilities are supported, crowding the docs

    • New or experimental capabilities not supported, eg:

      • Bracketed paste

      • Clipboard

      • Curly/colored underlines

      • Hyper-links

Turns out that terminfo is a big pain in the butt and not even a full solution for all the trouble. The console package has implemented some support, but it’s a bit of a curiosity at this point. Use import console.terminfo first thing or set PY_CONSOLE_USE_TERMINFO=1 to try it out.

Meanwhile, over at the Cheeseshop…

“Not much of a cheese shop really, is it?”—Monty Python

And so, now there are ad-hoc ANSI codes being generated in every command-line app and eleventy micro-libs on “the” PyPI doing the same. Looks to be a fun exercise and somewhat of a rite of passage to create one.

(On that note: Good luck finding an appropriate name on PyPI for yours—Taken!)

·····•·····        ·····•·····   
waka waka waka   

Often Missing

“Them Dukes! Them Dukes…”—Sheriff Rosco P. Coltrane

While many of the ANSI modules in the cheeseshop have plenty going for them in areas of focus, they generally aren’t very comprehensive──usually providing 8 colors and a few styles/effects like bold and underline. Unfortunately, one or more important items are often missing:

  • Styles, cursor movements, clearing the screen, setting titles, bracketed paste, full-screen, etc.

  • Multiple Palettes:

    • 8 color - always

    • 16 color - sometimes

    • 256 extended color - rare

    • Nearest 8-bit color - rarer

    • 16M color - rarer

    • Standard color names, like X11 & Webcolors - rarest

  • Querying the terminal, auto-detection, support and deactivation.

  • Python3 support/still maintained

  • Has tests

Nice to haves

“You’ve got to, know when to hold ‘em… know when to fold ‘em…”
—Kenny Rogers

Most are relatively easy to use, but may still miss one of these nice to haves:

  • Composable objects

  • Concise names

    • Avoidance of capital, mixed, or camel-case names on instances.

    • Avoidance of extra punctuation, parens, brackets, quotes, etc.

  • Nearest neighbor downgrade for unsupported palettes.

  • Progress Bars

  • Hyperlinks

Result

Looking over at PyPI with the criteria above finds many interesting pieces but far from the full Monty. So, had some fun building my own of course. Looked at and picked out a few design cues from several of these:

  • ansi

  • ansicolors

  • blessed

  • blessings (context managers)

  • click.style and utilities (reminded me of pause)

  • colorama.ansi (palette collection objects)

  • colorful (why terminfo is a bust)

  • colorize

  • escape

  • fabric.colors

  • kolors

  • pycolor

  • pygments (nearest indexed color)

  • style

  • termcolor

  • ptpython, urwid

  • rich

  • tqdm

etc.