[ANN] tty 0.0.1 – a pure OCaml library for interacting with the terminal

Hi folks :wave: just released tty v0.0.1 on opam.

tty is a pure OCaml library for directly interacting with the terminal, that I’m using to build Mint Tea:

This first release includes:

  • High-level Terminal module for controlling a terminal (with functions like cursor_up n)

  • Non-blocking reading of UTF-8 input from stdin in the Stdin module

  • Terminal Profiles for determining what color palettes are available

  • A Color module for parsing and working with RGB/ANSI/ANSI256 colors

  • A collection of 60 escape sequence functions in Escape_seq

  • a lot of room for improvement!

If you’re interested in contributing, there’s a few issues tagged with help wanted, but don’t hesitate to reach out to me on Discord/X: @leostera

Happy hacking! :tada:

19 Likes

Looks interesting! Two questions:

  1. why not use GitHub - pqwy/notty: Declarative terminal graphics for OCaml ?
  2. does it work on Windows?

Cheers,
Nicolas

2 Likes

notty tries to abstract away the terminal.

tty is a lower-level toolkit that just gives you direct access to the terminal.

For example, if you want to print colorful things you have to sequence the calls to Escape_seq.*_seq and print them out yourself.

i haven’t tested since I don’t have a windows machine at hand, but patches are welcome! :pray:

I’m curious: it doesn’t look like you’re using terminfo(5) as the basis for the escape sequences you emit ? Am I misreading the source ?

1 Like

Nope, its currently just using the ANSI escape \x1b[ and other hardcoded strings – which I based off the termenv Go project – this probably makes it a lot less portable/comprehensive but the implementation is trivial atm.

Note that Microsoft now recommends using VT sequences over the “classic” Windows console API. Almost any library using VT sequences can be made to work on Windows with a couple of calls to GetConsoleMode/SetConsoleMode and the ENABLE_VIRTUAL_TERMINAL_PROCESSING/ENABLE_VIRTUAL_TERMINAL_INPUT flags.

(Oh, how I hate Discourse’s mobile editor.)

1 Like