terminfo-hs- A pure-Haskell (no FFI) module for accessing terminfo databases

Copyright(c) Bryan Richter (2013)
Safe HaskellSafe




This is a pure-Haskell (no FFI) module for accessing terminfo databases, which contain characteristics, or capabilities, for the various terminals such as screen, vt100, or xterm. Among other things, the capabilities include the idiosyncratic character sequences needed to send commands to the terminal. These commands include things like cursor movement.

For a deeper understanding of terminfo, consult the man pages for term(5) and terminfo(5).

There are three parts to this module: acquiring a terminfo database, querying the database, and defining the capabilities.

This module is dead simple, so a single example will hopefully suffice to demonstrate its usage.

import System.Terminfo
import System.Terminfo.Caps as C
uglyExample :: IO (Maybe Int)
uglyExample = do
    term <- fromJust <$> lookupEnv "TERM"
    db <- acquireDatabase term
    let maxColors (Right d) = queryNumTermCap d C.MaxColors
    return $ maxColors db
>>> uglyExample
Just 256


Acquiring a Database

acquireDatabase Source #


:: String

System name

-> IO (Either String TIDatabase)

A database object for the terminal, if it exists.

Querying Capabilities

For each of these three actions, the first argument is the database to query, and the second argument is the capability to look up.

I'm not super proud of this interface, but it's the best I can manage at present without requiring lots of mostly-empty case expressions. Perhaps someone will suggest a more interesting solution.

queryStrTermCap :: TIDatabase -> StrTermCap -> Maybe String Source #

As this is a dead simple module, no 'smart' handling of the returned string is implemented. In particular, placeholders for buffer characters and command arguments are left as-is. This will be rectified eventually, probably in a separate module.

The Capabilities

see System.Terminfo.Caps

There are no less than 497 capabilities specified in term.h on my Intel-based Ubuntu 12.04 notebook (slightly fewer in the terminfo(5) man page). The naive way of making these available to the user is as data constructors, and that is what I have done here.

The number of constructors absolutely crushes the namespace. I have sequestered them into their own module to try to alleviate the pain.

The Database Type